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
After the college entrance examination, you can r...
01 What is WeChat Moments Advertising? WeChat Mom...
Seeing the world through our eyes is an innate ab...
This article is transferred from: Netease Technol...
The second category of e-commerce studied in this...
According to a report on the U.S. News Weekly web...
This year, my country experienced the hottest sum...
With less than two months left, the embarrassing ...
Review: Experts from the National Health Science ...
What are the reasons why there are no users after...
Every industry takes two things into consideratio...
Let’s take a detailed look at the data performanc...
Recently, some people have been asking, “Why is T...
Chinanews.com Beijing, July 6 (Reporter Wu Tao) R...