Skip to content

Code Stratification

Most programmers spend more time reading code than they do writing new code. It is important, then, to write code in such a way that it is easy to read and understand.

Consistency goes a long way to help with this, but there is more to consistency than naming conventions and bracket placement.

Have you ever seen a table of contents like the one below?

  • Chapter 1. The Beginning
  • Chapter 2. John wasn’t paying attention when he modified the class. He failed to notice that the code was organized so that details of the multistep process were abstracted into a method calls. John introduced details of a new step alongside calls to the methods that nicely summarized the other steps. Fortunately, his team caught the inconsistency during code review.
  • Chapter 3. A Lesson Learned
  • Chapter 4. The Conclusion

It appears that someone provided too much detail for Chapter 2’s description relative to the other chapter descriptions.

Now consider the code below:

public List<string> GetTopTenBooks() {
    var bookTitles = GetBookTitles();
    if(bookTitles.Count > 10)
        bookTitles = SortByRating(bookTitles).Take(10);
    return bookTitles;

A method called TakeTenHighestRatedBooks that is called in place of the if-statement would hide the details of the ranking and truncation step and keep the code in the GetTopTenBooks() method at a consistent level of detail.

This is called code stratification. It is achieved when methods and classes have a consistent level of abstraction.

No comment yet, add your voice below!

Add a Comment

Your email address will not be published. Required fields are marked *