DEV Community

Cover image for Why I love functional programming
Andrei Dascalu
Andrei Dascalu

Posted on

2 1

Why I love functional programming

Not long ago I wrote a bit of rant about things that annoy me about OOP. They were largely conceptual issues, but on the practical side it boiled down to how OOP concepts are largely unnecessary. In development, it's way too easy to write OOP spaghetti and people fight to keep things testable while spending most of their lives debugging the mutable global state.

Why functional programming (FP) can be better?

  1. Functions are functions, regardless of language. If you develop around functions, keeping them idempotent, then moving from language to language is simple. Functions are functions everywhere and as long as the languages support FP style development (aka functions are first class citizens) you won't run into surprises. No more 'interfaces mean just slightly different things in that language' or 'inheritance comes with a couple of gotchas' or classes mean slightly different things and they miss stuff. Sure, in Haskell you have real immutability and you may or may not have 'map' as a built-in tool but otherwise, it's a matter of whether or not there are curly braces or whether you need semicolons at line's end or not.
  2. Everything's testable - no side effects, it's all about transforming input to output with no surprises. No worries about mocking instances, whether member visibility comes in the way or not.
  3. Composition is at the core - yeah, OOP adopts it as well. No more entangled code that you can't really reuse in different projects.
  4. Immutability - sadly it's mostly up to developers to respect immutability but when you live by FP you know that there won't be surprises. Things don't change from under you in unexpected ways and code is mostly self documented (particularly if you design for totality)
  5. Composition, immutability and idempotency go a long way to make sure that FP programs are concurrency-friendly. Whether the language itself offers concurrency constructs (Golang, Erlang, Rust, etc) your approach already allows you to identify potential race conditions and to manage them in various ways.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay