I was reading a book today called "Clean code" and I came across a paragraph were the author was talking about the levels of abstraction per a function, he classified some code as low/intermediate/high level of abstraction.
My question is what is the criteria for determining the level of abstraction?
I quote the paragraph from the book:
In order to make sure our functions are doing “one thing,” we need to make sure that the
statements within our function are all at the same level of abstraction. It is easy to see how
Listing 3-1 violates this rule. There are concepts in there that are at a very high level of
abstraction, such as getHtml(); others that are at an intermediate level of abstraction, such
as: String pagePathName = PathParser.render(pagePath); and still others that are remarkably
low level, such as: .append("\n").
Best Answer
The author explains that in the "Reading Code from Top to Bottom" subsection of the part that talks about abstractions (hierarchical indentation mine):
The code that'd go along with this would be something like this:
And so on. Every time you go deeper down the function hierarchy, you should be changing levels of abstraction. In the example above,
IncludeSetups
,IncludeTestPageContent
andIncludeTeardowns
are all at the same level of abstraction.In the example given in the book, the author's suggesting that the big function should be broken up into smaller ones that are very specific and do one thing only. If done right, the refactored function would look similar to the examples here. (The refactored version is given in Listing 3-7 in the book.)