Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 19, 2020 05:43 am GMT

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.

Alt Text

Let's stay connected on Instagram


Original Link: https://dev.to/codingunicorn/reveal-intent-hide-implementation-42lc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To