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

Posted on by:

codingunicorn profile

Coding Unicorn ðŸĶ„

@codingunicorn

Hi!✌(◕â€ŋ-)✌ My name is Julia (also known as coding_unicorn on Instagram). I'm a full-stack developer specializing in Java and JavaScript.

Discussion

markdown guide
 

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

 

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

 
 

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

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

 

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

 

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