The first rule of swiftui internal functions is should they be small?

Knowledge fat2022-05-14 19:28:52

The cleaning code tells us :

“ The first rule for functions is that they should be small .”

Almost every developer is familiar with Do One Thing (DOT) guide , Also known as Curly The laws of . This guide tells us that functions should only do one thing .Clean Code There are more explanations for this :

“ A function should do something or answer something , But not both .”

This is a good difference , Because if we have to take action and answer something , We define it as more than one thing .

Final , We have to piece together these functions and do something . According to the definition , A high-level function that calls multiple low-level functions will eventually do more than one thing .Bob Uncle is Clean Code with the Same Level of Abstraction Principle (SLAP) This point is clarified again in :

“ The idea behind this principle is , All code in a method should be at the same level of abstraction . This makes it easier to read and understand the code . If the code is represented at different levels of abstraction in the same method , This will lead to confusion and incomprehension .”

DOT The guide actually means what we're doing “ One thing ” It's the same level . Consider a simple function , It will update the phone number on the user record . First , The function must find a record . Then it will update the record , Last save record . It is reasonable to return updated user records . It may call functions for each activity , These functions may call other functions , These functions drill down to the actual hardware where the data is written .

Find-Update-Save All three steps are at the same level , And can be contained in a single function , What this function does “ One thing ” yes UpdateUser(). Think about it in UpdateUser() The horror of reading code at the beginning of low-level disk reading . perhaps , If UpdateUser() At the same time UpdateCompany() and UpdateAccount().

Similar articles