bradteller.com

Code Meaningfully

I see a lot of code like this.

var p = repo.GetBy(personId);

if (p.Children.Count > 0 && p.Children.Where(c => c.Children.Count() > 0).Count() > 0)
{
    return "somebody old";
}
else
{
    return "maybe old, but maybe no kids";
}

What does this code do? Maybe in this case it jumps right out at you and you say “Oh! They are trying to find out if this person has any grandchildren” and that would be right. But there are problems with this code.

Add Meaning

We can make this code much more readable, meaningful, and flexible, with a very simple refactor. Start by doing this.

var p = repo.GetBy(personId);

if (p.HasGrandchildren())
{
    return "somebody old";
}
else
{
    return "maybe old, but maybe no kids";
}

And then push that logical check into IsGrandparent() off of Person.

public bool HasGrandchildren()
{
    return this.Children.Count > 0 && this.Children.Where(c => c.Children.Count() > 0).Count() > 0;
}

Now we’ve taken what it means to have grandchildren and encapsulated it in a single function. So if we were to ever change this logic we only have to do so in one place. Not repeating yourself is great, especially in code, but there is another benefit.

Code Readability

Unless we are working on a personal project that nobody else will ever use or modify we are sharing the source code with those we are working with today, and those who will come after us. Take some time when you write your code and make sure your code is readable. Everyone will benefit from the extra bit of effort.