Make your code consistent

They are all different, but they all look alike.

They are all different, but they all look alike.

We all have our rules, our preferences. Even beliefs. Sometimes, you can talk with people who have the same principles. Sometimes, they can have thoughts that diverge from yours, completely.

Discussing different points of view is often interesting. You can learn a lot, and it opens your minds to new ideas. So, do not close the door to constructive debates. Moreover, you can use these discussions to improve group cohesion and build a shared vision.

Write readable and consistent code

Sometimes, I see myself as a writer. Not as a code writer, but as a story writer. I try to tell a story with my code in order to make it easier to understand. I do my best to make it as readable as possible, and to make it consistent. I think that a well-written story is a story that reads well. The sentence may seem funny, but as a writer, you will be read.

Your code can be read by a coworker during a code review, or when adding a new feature. It can also be read by you, later. Oh, I just said coworker. Your code may be a part of someone else’s code, or even several people. Different people writing the same story? Will it be easy to read? It can be, and one of the way is to make it consistent.

By having consistent code, you can focus on the functionality and not on the style.

You will not be distracted by a different syntax of a coworker or an old code. Furthermore, it will be easier for a new member to join your team. The story reads as if it was written by only one author.

Writing consistent code can be more than just having the same style. It can also be sharing good practices. Consider for example force unwrapping, function body length, or the preference to use first(where:) or filter{}.first. All this works, but, depending on your thought, they can be improved. If the same way is adopted by the team and used everywhere, it will be easier to write, and to read.

About that, if you want to share some rules, it can be interesting to put it down in a code convention.

Make your code convention

A code convention can be helpful for a team to ensure the code quality and the readability by listing various rules, good practices and code style. Making things explicit will reduce doubt, misunderstanding, and lack of knowledge. Collaboration will be easier.

If your team follows the same convention, it will make your project more consistent. It can help you to write code, and to review others’ code. For a new developer joining your team, this is a quick way to learn your practices.

When doing a code review, you can focus on important changes and structure. You will avoid having to debate again about the use or not use of whatever thing. You already did it, and the team agreed at the end. You will discuss and put effort into new things. But this is not a problem if there is any violation, it can happen, and there will be less with time.

While we are on this subject, some people can felt hurt during code review. They can take it personally. It can be helpful to refer to your code convention when you are discussing about their code. You are not attacking the person. You are underlining a piece of code that does not respect a code convention. Your team convention. And you are helping this person to improve that.

The code convention concerns the whole team. You should write it together to get everyone involved. Discuss, listen, and be open.

This code convention may not be the best, nor yours, but it will be the one for your team.

This convention will be adapted to your context, your project and your team. Build the convention together, and make it live with time. You may not need to be exhaustive immediately, and you may want to change it later, don’t be stuck.