How many times have you encountered a function with a return statement deep within the code? Or failed to notice a break or a continue in a loop? While I have not been blindsided by return recently, I have still been caught off guard by continue and break.
Shortly after undergrad, I suggested changes to C’s break statement to Dennis Ritchie. While I no longer desire the changes, Dennis’s response holds wisdom that applies more broadly:
Your suggestion that the ‘break’ statement should be augmented with a number to indicate the number of levels to escape from, has arisen before. It is an idea that both I and the X3J11 committee thought about carefully, and it is attractive in several ways. Nevertheless, the committee standardizing the language decided to reject it. One of the inherent problems with the idea is that it exacerbates a difficulty already present with ‘break’, namely making it hard to determine where control goes. Very small changes in the source program result in dramatic changes in the control flow, and the confusion increases with the number of break levels. [emphasis mine]
I believe the issue is not with break and other control flow statements. The issue is how we indent those statements. The indentation should not be from the level where the control goes from. Instead, indentation should be to the level the control goes to. So, instead of
func(....) {
…
for (...) {
…
if (...) {
break;
}
…
}
…
}
I propose that we indent as follows
func(....) {
…
for (...) {
…
if (...) {
break;
}
…
}
…
}
The above code looks ugly, and that is precisely the point.
By indenting as usual, we potentially mask unexpected behavior. By indenting as proposed, we make the unexpected expected. This will result in a better understanding of the code, and better night sleep for all.
I do not know what Dennis would have thought of this. Sadly I never got to run it by him. However, if you like the idea, as a tribute to Dennis, I would like to have it named after him. K&R style of indentation is already attributed to him. Calling this Ritchie indentation will be confusing. So, I propose we call this Ritchie flow-apparent (RFA) option. There are many indentation standards, and this would apply to most. Code editors, repository code viewers could toggle the indentation on click of the RFA button. And if folks like, this option could be added to code formatters too.
Please let me know what you think about this?
I wrote this article originally on medium. It has a scanned copy of the original letter from Dennis Ritchie.
Would love to hear what folks think of this? Also, if folks like it and support it, are they in position to help create tooling around it?