Introduction
The biggest differentiator between a Senior Developer and a Junior Developer is understanding and implementing the “non-functional” requirements of software. Any developer can write code that meets the functional requirements, but the difference comes into play when the developer reads between the lines of the functional requirements and codes for the non-functional requirements.
Ok, hang on, contrary to popular belief, the non-functional requirement isn’t only about “performance” numbers, neither is it about optimal memory usage alone. It also boils down to three major non-functional aspects,
- Maintainability
- Readability
- Testability
And clean code principles come to the rescue for these three core non-functional requirements of code.
The topic for this blog is related to code readability. It is often observed that in enterprise software development, a piece of code might be written only once, but it is read and maintained maybe 100 times in its lifetime. Hence understanding a code that humans can read is of utmost importance.
Martin Fowler’s quote – one of my favorites – reads: “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.”
The "Single Level of Abstraction" Principle
Single Level of Abstraction (SLAB) – as the name suggests, recommends writing a method/function in a single level of abstraction.
Let us first quickly understand what is abstraction and what is level of abstraction is.
Abstraction is a fundamental concept in OOPS. In brief and layman’s terms, it talks about hiding the “how” part and only exposing “what” to the outer world. Maybe in some other blog, I will elaborate on this and explain its true meaning.
The level of abstraction comes to the point where our mental grouping comes into effect. It’s best imagined as our cognitive ability to continue being abstracted from details of operations. In general, different “blocks” of code inside a method a classic indicators of different levels of abstraction. This means, the reader of the code now has to create a “branch” in their mental grouping to read that condition or loop block and merge back to the same level where the block ended.
In my opinion, this also could be correlated with the cognitive and cyclomatic complexities of code. A higher number of these in a code is a direct indication that the code is violating the principle of “SLAB – Single Level of Abstraction”.
OK, enough talk -- let’s look at a simple example and see how in C#, we can achieve this principle.
Let us assume we need to read a JSON array from a file that contains weather report data of different cities across the world. The JSON file may look like this.
[
{
"city": "Bangalore",
"day": "Monday",
"time": "Morning",
"temp": "15",
"humidity": "60",
"reportedBy": "IndiaToday"
},
{
"city": "London",
"day": "Tuesday",
"time": "Afternoon",
"temp": "10",
"humidity": "85",
"reportedBy": "BBC"
},
{
"city": "New York",
"day": "Sunday",
"time": "Evening",
"temp": "12",
"humidity": "70",
"reportedBy": "CNN"
},
{
"city": "Berlin",
"day": "Thursday",
"time": "Night",
"temp": "5",
"humidity": "65",
"reportedBy": "DW"
}
]


Join the conversation! Your thoughts help the community grow.