Two super simple examples, achieving the same thing.
The first example has fewer lines of code compared to the second. But the amount of domain knowledge that we made implicit (and thus lost since we failed to communicate it) when optimizing for that metric is disproportional.
In the second case we get to understand that:
firstname.lastname@example.org a sender
- that the receiver is the same as the sender (we send the email to ourselves). I’d even extract that assignment into
receiver_email = same_as(sender_email)to make that knowledge explicit
- we get to understand the
sendmailmethod call better since we don’t have to jump into the method body to understand which parameter is which
- this also helps us when changing the code on this client’s side and reduce chances for swapping the parameter unintentionally
My advice: prefer optimizing for explicitness and readability.
There’s a box full of great opportunities awaiting you from that point on. Opportunities for testability, better design, readability, and domain insights.
Note: If we’re in fact sending an email to ourselves, I’d go ahead and extract this method into
send_email_to_ourselves and just pass a single email address.
But this design decision became way more obvious because I made the previous intentions explicit in the first place.