Since the guard statement appeared in Swift, it has caused a lot of discussion. To be honest, guard does simplify the code and improve the readability of the code, but is it a panacea? Small Functions There is also a lot of discussion about the size of functions. Obviously, function bodies should be short, and the shorter the better. No one wants to read, understand, or refactor large functions. But how big should a function be? The first criterion for a function is that it should be short. The second criterion is that it should be even shorter. ---Robert C. Martin More specifically, Martin believes that functions should be less than six lines in length and never more than ten lines. Although the rules are simple, the effect is significant. You can see that the code immediately becomes much easier to understand. Before, you needed to remember a function with 30 lines of code, several indentation levels, and several intermediate variables. Now you only need to remember ten functions with clear names. Single Responsibility Single responsibility has been talked about for a long time. This rule applies not only to objects, but also to functions. Obviously, each function should do only one thing, but people break this rule again and again, mostly because of the size of the function. If you refactor a 30-line function into ten 3-line functions, then the single responsibility rule will naturally be followed at the function level. Single layer of abstraction People don't talk much about single-level abstraction in functions. It is a tool that helps to write single-responsibility functions. What is single-level abstraction? Simply put, code at a high level of abstraction, such as controlling a process, should not be mixed with small details, such as variable increments or Boolean value checks. For example. The following example is from The Swift Programming Language book.
Of course, vend(itemNamed:) is just one example of using the gaured statement, but you’ll often see similar functions in production code. This function exhibits the three problems mentioned above:
What does this function look like after refactoring?
Although the total number of lines has increased, you should remember that the number of lines of code is not its ultimate goal. The refactored code has several advantages over the old version:
in conclusion Guard statements are handy for reducing nesting of structures and functions, but the problem is not guard itself, but its use. Guard statements will force you to write large functions that do several things and have multiple levels of abstraction. As long as you keep your functions small and clear, you don't need guard statements at all. Related Reading
|
>>: Agile development is good, but don’t follow it blindly
Meizu brought the Meizu Blue Note 2, which has al...
Recently, Rocket Lab, an American company, reveal...
In early March of this year, Wang Xing mentioned ...
As the Spring Festival approaches, many children ...
Introduction to Smali Smali is a disassembly lang...
Microsoft Bing is developing very well in the Uni...
[[170834]] Introduction Retrofit is an HTTP frame...
After finishing the renovation of their new house...
Many friends often complain to me: " Informa...
Soy sauce is a must-have for every household Ther...
"The earth is the cradle of mankind, but man...
Apple is a fruit known for its health benefits, a...
Preface With the development of social economy, I...
Recently, with the arrival of the dog days of sum...
The rise of local self-media is something that is...