Categories
iOS Development

Clean Code for Swift – Chapter 3: Functions

What the func?

Functions!! They do exactly what you tell them to do. They are the building blocks for modern day applications. If you try to make an app without using a function, good luck.

How do we make functions clean?

Keep Them Small

According to “Uncle Bob” he says that function should rarely ever be 20 lines long. His rule used to be that they shouldn’t be more than a “screenful,” but this was in the ’80s when screens were 24 lines by 80 columns. His rule now is that functions should not be 150 characters long (width) and rarely every be more than 20 lines, and definitely less than 100 lines.

This isn’t always possible in practice, but if you can make sure your function only does one thing, it will inherently be a small function.

Do One Thing

The most important goal is to make sure the function does ONE thing. What does one thing mean?

A single functionality. Read from a database and return a value, convert a value, filter data, etc. Having your function only do a single operation or have a single goal ensures that your function does not have any side effects.

When your function only does one thing, it’s easier to name, understand, and debug.

Descriptive Names

If your function does one thing, it’ll already be easier to try to name that function.

The name should be “pretty much as expected.” If you name a function fetchFirstNames() there’s no question what that function does. It won’t fetch last names or full names, you can clearly understand, even without knowing any context, that this function will only fetch the first names.

Function Arguments

Ideally we’d like to have ZERO arguments, but that’s not always how it works.

If we can’t have zero arguments, then 1 to 2 would suffice, but 3 or more should be avoided. If you have that many arguments, how are they all related? You should start thinking about creating a new object and have all those arguments instead be properties of the object, and then pass the single object to the function.

Avoid output arguments. A majority of the time function arguments are inputs to the function, having an output as an argument can make developers do a “double-take.”

Anything that forces you to check the function signature is equivalent to a double-take. It’s a cognitive break and should be avoided.

Clean Code, Chapter 3

Command Query Separation

Functions should either do something or answer something, but not both.

Clean Code, Chapter 3

This means that a function can change the state of an object, or return information about the object. If an object did more than one of these, it would break the rule of doing only one thing.

Don’t Repeat Yourself (DRY)

Functions are already created to help reduce code and make it easier to read. Be sure to not duplicate functions either! When creating functions make sure you don’t end up having two functions that do the similar things, or repeating the same code inside multiple functions. Make a new function!

Otherwise you end up in the case where two functions look similar and it gets confusing about which functions is called when and why.

Conclusion

Be sure that your functions are clean otherwise why did you make functions in the first place? Having functions that do a single job ensure that you know exactly what that function does, even if you’re new to the codebase. Doing one thing means it’s easier to name and understand, and this will inherently make the function smaller.

If you follow the rules herein, your functions will be short, well named, and nicely organized.

Clean Code, Chapter 3