Readable Programming

Branching and looping are widely used in computer programming and they may be some of the basics of coding. Have you ever reached such a high level of branching and looping that your code just became too hard to read? How about someone else taking a look at the code, how easy would it be for him or her to understand your intent? What about testing the code? 

Recently, I’ve been studying some of the best practices in making understandable code and I have reached the conclusion that good code should be written in a “human language” manner.

Example. Given a set of person

  1. class Adress {  
  2.     public string City {  
  3.         get;  
  4.         set;  
  5.     }  
  6. }  
  7. class Person {  
  8.     public string Name {  
  9.         get;  
  10.         set;  
  11.     }  
  12.     public Adress Adress {  
  13.         get;  
  14.         set;  
  15.     }  
  16.     public int Age {  
  17.         get;  
  18.         set;  
  19.     }  
  20. }  
  21. static Adress NY = new Adress {  
  22.     City = "New York"  
  23. };  
  24. static Adress Delhi = new Adress {  
  25.     City = "Delhi"  
  26. };  
  27. static List < Person > persons = new List < Person > {  
  28.     new Person {  
  29.         Name = "John", Age = 22, Adress = NY  
  30.     },  
  31.     new Person {  
  32.         Name = "Bob", Age = 88, Adress = Delhi  
  33.     },  
  34.     new Person {  
  35.         Name = "Maria", Age = 21, Adress = null  
  36.     },  
  37. };  

Demand

Let’s say you want the names of all the persons that are in Delhi with an age over 22.

  1. A common way of writing this would be.
    1. List < string > NamesOfPersonsInDelhiOver22 = new List < string > ();  
    2. foreach(Person person in persons) {  
    3.     if (person.Age > 22) {  
    4.         if (person.Adress != null) {  
    5.             if (person.Adress.City == "Delhi") {  
    6.                 NamesOfPersonsInDelhiOver22.Add(person.Name);  
    7.             }  
    8.         }  
    9.     }  
    10. }  

The problems in the above code are,

  1. Readability which is obviously not so great.
  2. Branching. It’s a simple exercise but think what it would look like if you were asked to add new features like “get only males, sort by age” etc.
  3. Each new branch would add more complex tests to validate the solution.
LINQ provides a very good framework that would help you write this like:
  1. public static bool PersonAgeIsOver22(Person person) => person.Age > 22;  
  2. public static bool PersonAdressIsNotNull(Person person) => person.Adress == null;  
  3. public static bool PersonCityIsDelhi(Person person) => person.Adress.City == "Delhi";  
  4. public static string PersonName(Person person) => person.Name;  
  5. NamesOfPersonsInDelhiOver22 = persons.Where(PersonAgeIsOver22).Where(PersonAdressIsNotNull).Where(PersonCityIsDelhi).Select(PersonName).ToList();  

Does this solution look more readable? Please comment below.