DEV Community

loading...
Cover image for Reveal intent, hide implementation

Reveal intent, hide implementation

codingunicorn profile image Coding Unicorn ðŸĶ„ ãƒŧ1 min read

Reveal intent, hide implementation

When writing code, it's important to distinguish between INTENT (what is happening) and IMPLEMENTATION (how things work).

❌ In the example below, intent is mixed with implementation:

users
  .filter(user -> user.registrationDate.isBefore(dayjs().minus('years', 1)) && user.hasPurchases())
  .filter(user -> authenticationService.isAuthenticated(user));

In order to understand WHAT this code is doing, your brain has to discern INTENT from IMPLEMENTATION. You need to spend 5-10 seconds on deliberate thinking. It consumes your limited and precious brainfuel. And that's only a single method.

✅ Now, let's improve the code. Let's leave the intent visible, but implementation hidden:

users
  .filter(isLoyal)
  .filter(isAuthenticated);

It's immediately clear WHAT the code is doing. The code is not polluted with implementation details; details are hidden. Leaving only essential information and removing the noise is called abstraction. A well-abstracted code is easy to scan. A poorly abstracted code requires a lot of deliberate thinking and attention.

🧠 Remember: good code reveals intent, but hides implementation details until they are needed. Make the essence visible; hide the rest.


Every day, I post coding tips on my Instagram. Let's stay connected! ðŸĶ„

Alt Text

Discussion (7)

pic
Editor guide
Collapse
nombrekeff profile image
Keff

Is there any reason why you published this post again? Is it different or just so it reaches more people?

Collapse
lifelongthinker profile image
Sebastian

You're right . I even commented on that one. Didn't cross my mind today 🙈🙈. But at least I am consistent in my praise ðŸĪŠ

Collapse
codingunicorn profile image
Collapse
nombrekeff profile image
Keff

I think so... If not I've had a massive dejavou lol

Thread Thread
codingunicorn profile image
Coding Unicorn ðŸĶ„ Author

Holy cows! 🐄 😂 Didn't notice the first post when scanning through the list of posts. Now the question is – which one should I remove? ðŸĪŠ

Collapse
nombrekeff profile image
Keff

Yup, here it is, on May 19... nothing wrong with that, was just curious
dev.to/codingunicorn/reveal-intent...

Collapse
lifelongthinker profile image
Sebastian

Great example 👍 As always, precise and to the point.