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
Wang Yaojun Recently, the text generation video l...
The ChatGPT and GPT-4 large language models recen...
Hanekawa - Douyin Merchant Self-broadcasting Oper...
As early as the Qin and Han dynasties more than 2...
TikTok short video sales have become a new force ...
Recently, Apple pushed iOS 10.2 Beta 2 to develop...
Introduction: In the growth analysis of a product...
Similar to social performance marketing platforms...
As mobile phone functions become increasingly pow...
In less than two days, at 1:00 a.m. Beijing time ...
Tavistock Short-Term Dynamic Therapy 12-Lecture O...
In this article, I highlight some of his findings...
1. How to increase the quantity and conversion ra...
In the past two years, with Tesla's vigorous ...
Since Foxconn acquired Sharp, Sharp has undergone...