Code readability hypotheses by Mark Seemann
This post proposes a scientific experiment about code readability
Once in a while someone tries to drag me into one of many so-called 'religious' debates over code readability:
- Tabs versus spaces?
- Where should the curly braces go?
- Should class members be prefixed (e.g. with an underscore)?
- Is the
thisC# keyword redundant?
In most cases, the argument revolves around what is most readable.
Let's look at the C# keyword
this as an example. Many programmers feel that it's redundant because they can omit it without changing the program. The compiler doesn't care. However, as Martin Fowler wrote in Refactoring: "Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Code is read much more than written, so I don't buy the argument about redundancy.
There are lots of circumstances when code is read. Often, programmers read code from inside their IDE. This has been used as an argument against Hungarian notation. However, at other times, you may be reading code as part of a review process - e.g. on GitHub or CodePlex. Or you may be reading code on a blog post, or in a book.
Often I feel that the
this keyword helps me because it provides information about the scope of a variable. However, I also preach that code should consist of small classes with small methods, and if you follow that principle, variable scoping should be easily visible on a single screen.
Another argument against using the
this keyword is that it's simply noise and that it makes it harder to read the program, simply because there's more text to read. Such a statement is impossible to refute with logic, because it depends on the person reading the code.
Ultimately, the readability of code depends on circumstances and is highly subjective. For that reason, we can't arrive at a conclusion to any of those 'religious' debates by force of logic. However, what we could do, on the other hand, is to perform a set of scientific experiments to figure out what is most readable.
A science experiment idea for future computer scientists
Here's an idea for a computer science experiment:
- Pick a 'religious' programming debate (e.g. whether or not the
thiskeyword enhances or reduces readability).
- Form a hypothesis (e.g. predict that the
thiskeyword enhances readability).
- Prepare a set of simple code listings, each with two alternatives (with and without
- Randomly select two groubs of test subjects.
- Ask each group to explain to you what the code does (what is the result of calling a method, etc.). One group gets one alternative set, and the other group gets the other set.
- Measure how quickly members of each group arrives at a correct conclusion.
- Compare results against original hypothesis.
Code readability hypotheses by Mark Seemann is licensed under a Creative Commons Attribution 3.0 Unported License.