3 ms·
I know this is just a throwaway example, but feel just looking at the code layer this kind of elegance is comes at the cost of clarity and conveying intent, and
by BoorishBears 6y ago
I know this is just a throwaway example, but feel just looking at the code layer this kind of elegance is comes at the cost of clarity and conveying intent, and I see that happen a lot.
The second method explicitly checking for null is great because it's telling me so many things very plainly:
1. There can be no address for a user
2. There is otherwise one address per one user
3. If there is no address we explicitly do nothing (if clause with no else)
If we do the same analysis for the first method it actually feels like it's conveying false conclusions. It's implying there's multiple addresses per user, for example.
And the fact that if a user has no address we don't do anything is still true, but the intent feels buried in the behavior of the loop.
By default I try to value writing code in a way that conveys the most intent possible over how it feels to write
For example, another language with nullable types (Kotlin) has
user.address()?.let { it.sendMail() }
I've seen people write code like this, and it just feels so unnecessary, how much longer does it take to parse? And it still doesn't have the clarity of intent that a simple null check has.