Imagine a book in which there is no division into chapters, and everything goes without a logical and semantic breakdown, a book with no paragraphs, no points and commas, a book where The first line tells about one thing, the second about the other, and the third again about the first.
Could you understand what the book is about?
How quickly could you find the passage you are interested in?
Your code, like the content of the book, needs to be structured so that the code is easy to read and conveys the meaning of it.
In this article, I will show examples on the organization of code in which classes will have the same sequence of basic blocks and their breakdown.
For convenience, I will use the word class (class), but imply any kind of type (class, struct, enum).
Thanks to the use of these tips, your code will become readable, which will further ensure the convenience and speed of working with it.
Of course, the described tips can be modernized to your liking, observing the basic principles.
First, let's compare the same code in two versions.
Example of a messy class:
This code is similar to a dump of methods, variables and outlets, in which everything merges together, it is difficult to understand what is related to what and in what place.
Pure Class Example:
Empty line 38 - indent one line from the last method so that you can see where the last closing method bracket ends and where the class ends.
In both examples, the same functionality is given, but the difference is that the second option has a clear structure, due to which the logic is more obvious, the code is easy to read, you can quickly find what you are looking for, and besides, it is just nice to watch.
Basic principles for the formation of a pure class structure:
- Always use //MARK: -
- Give names of tags and set their order
- We bring the logic from the life cycle methods to separate methods
- Use extension to implement the protocols
- Select the logically related elements
- Remove unused
- Automate the routine
1. Always use //MARK: -
For readability, the book is divided into chapters, and so it will be more comfortable for us to work if we create a table of contents for the class using //MARK: -
This label is not only well distinguished from the entire code, but also automatically creates a table of contents - it highlights the sections in the code in bold type in the list of elements of this file.
You can view the file's contents by clicking on the button after the arrow to the right (& gt;) at the very top of the file after the name of this file or on the ctr + 6 (document items menu).
2. We give the names of the tags and set their order
Below are the main labels for breaking the code into logically related blocks and their sequence:
When using this method of grouping, you can easily navigate the code of any class.
3. Moving logic out of lifecycle methods to individual methods
The logic inside the ViewController'a life cycle methods must be moved to separate methods, even if you have to create a method with one line of code. Today is one, and tomorrow is ten.
Due to the fact that the implementation details are made to third-party methods, the logic of the life cycle becomes clearer.
4. Use extension to implement protocols
Take out the implementation of the protocols in the extension marked //MARK: - SomeProtocol
On this label everything that is connected with this protocol will lie - all that is only here and there is no need to go anywhere else, otherwise the methods and properties of the protocol will be scattered throughout the class.
5. Select the logically related elements
To increase visibility, it is necessary to select logically related elements using an empty string:
6. Remove unused
Do not leave unnecessary comments (default), empty methods or dead functionality - this is littering the code. Pay attention to the class AppDelegate, most likely you will find there empty methods with comments inside.
7. Automating routine
In order not to write manually in each class //MARK: - SomeMark
, use Code Snippet
We write the label, select it, then Editor - & gt; Create Code Snippet, give it a name and call on the shortcut.
//MARK: - Bonus
- Mark a class with the final keyword if this class does not have any heirs — the project compiles faster and the code runs faster.
- Mark properties, outlets and methods with the keyword private - they will be available only inside the class and will not be in the public list of properties and methods if they are not needed there.
I wish you all success in developing applications and let your class become cleaner!
//MARK: - Help with writing an article
Alexey Pleshkov medium.com/@AlekseyPleshkov
//MARK: - Links
Ray Wenderlich code style