An Interest In:
Web News this Week
- April 20, 2024
- April 19, 2024
- April 18, 2024
- April 17, 2024
- April 16, 2024
- April 15, 2024
- April 14, 2024
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.
Original Link: https://dev.to/codingunicorn/reveal-intent-hide-implementation-42lc
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To