I was reviewing a pull request today when came across something along some complicated boolean logic and it reminded me of the sections of Robert C Martin's Clean Code: A Handbook of Agile Software Craftsmanship. In it, he discusses Bad Comments as well as Using Descriptive Naming.
When looking at boolean logic, I like to combine to two principals to give context to why I'm doing something.
Let's look at a contrived example. Some best practices with time retrieval and hard coded values are left for clarity.
if(person.IsBornInUsa &&
person.DateOfBirth >= DateTime.Now.AddYears(-35) &&
person.ResidencyDate >= DateTime.Now.AddYears(-14)
{
person.SetQualifications(true)
}
We can add context to our actions by modifying things slightly:
var meetsPresidentialQualifications = person.IsBornInUsa &&
person.DateOfBirth <= DateTime.Now.AddYears(-35) &&
person.ResidencyDate <= DateTime.Now.AddYears(-14);
if(meetsPresidentialQualifications)
{
person.SetQualifications(true)
}
It's a small change, but it lets us focus on what's important with less mental gymnastics.
Software Development Nerd