Anyone who reads the title of this article and knows a bit about the subject may tap their forehead: what do you mean, "is it necessary"? Of course it is! True, the question could be phrased differently, for example "how necessary is it". But let's take it step by step.
Software code can be written in many ways, and this is something that evolves throughout a programmer's career. It goes from the simplest constructs a coder learns the trade on, through writing bigger applications in an inefficient and unstructured way, all the way to overengineered constructions and, finally, finding the golden mean. If we talk not only about whether the code works but also about how good its quality is, several factors come into play, and they gain particular weight in teamwork. You may not believe it, but one of those factors is... aesthetics. Just as a user interface can be pretty or ugly, usable or not, code can be written beautifully or badly.
That is what we will focus on today because, unlike in many areas of life, the aesthetics of source code can be described by a standard. Of course, it is not the only thing that decides whether we consider code to be of good quality. Architecture matters a lot more, including class design and how easily the code can be extended. That is a very, very broad topic, but you have to start somewhere, preferably with something simple. So let me invite you to some thoughts on the beauty of the lines that make up software source code.
What is a coding standard?
It is a document or a set of guidelines describing the rules for writing individual statements of code in a given programming language or technology. I understand that this can still be read in two ways and at different levels of detail. So, based on examples, we can say that a coding standard can define things such as:
- where the opening brace of a method or class body goes,
- what spacing to use, both when nesting code and when writing statements,
- what rules to follow when naming classes or methods,
- what the order of methods and fields in classes is.
These rules therefore define both the aesthetics of the code and the naming rules. However, the main goal is not to make the "guts" of the software look pretty so that art historians of the future marvel at the wealth of little delights. The main reason for agreeing on a shared code standard is the need to unify code written by different programmers. I mentioned that ever broader teamwork requires additional agreements and order. Imagine one program implemented by three people, each of whom writes in a completely different way: one puts braces here, the second starts method names with a capital letter, and the third indents with spaces instead of tabs. The result is chaos that not only looks ugly but, above all, doesn't let you quickly find your way around the program and learn the implementation details. We can experience this ourselves when we try to read someone else's amateur code, or even our own from a few years ago, when we had less experience. With naming there is an additional aspect: the idea of self-documenting code. That is a separate topic, but the key point is that without any rules, every programmer will deviate significantly from the others in how they "coin words" ("I understand what this says, so others will figure it out too"). Of course, this cannot be standardized completely, but it is valuable to state when we use PascalCase and when camelCase, kebab-case or snake_case, which prefixes we use (and whether we use them at all), and so on.
What are the risks of having no coding standard?
We touched on this in the previous paragraph: without a standard, everyone writes code "their own way", full of faith in good will and in the idea that everybody reads the text the way he or she does. To be clear, of course it is possible to achieve full agreement in a team and a similar style without any standards. It may also turn out that all team members write code the same way, adapting naturally. But that is a particularly lucky coincidence, and it also assumes an equal level of experience across the whole team. Just as contracts are said to be there "for hard times", the same can be said about a coding standard.
There is nothing wrong with a team having different writing styles, as long as the differences are not significant and understandable only to one particular programmer. At the same time, it is fairly easy to find good reasons why the style should be unified after all. If it isn't, we can end up with:
- more time needed to understand such code and use it,
- longer code reviews, which disrupts work on new functionality,
- misunderstandings and misuse of each other's work, or even duplicating it (for example by writing code that already exists in the program, but in another module and under a different name),
- problems with merging code and reported conflicts that don't really exist (for example whitespace problems),
- a longer onboarding time for new developers who have only just started working on the project.
In the longer run, problems with unifying work can lead to bad software that, on top of that, takes more time to build than planned. But that's not all. In extreme cases there are conflicts in the team, a need for alignment meetings or for writing additional documentation and code comments, and above all frustration.
When is a coding standard worth having and how do you create one?
Of course, coding standards are found in environments where teamwork takes place. This applies above all to software companies, but also to communities that develop the code of a particular piece of software together. I mean especially groups maintaining open source projects, where the randomness of the code delivered can be even greater, unless it is kept in check by something like a "committee". It is more doubtful whether a separate coding standard is needed by individual developers (for example freelancers working on their own) and by groups that collaborate for a short period on university or school projects. That doesn't mean that in these cases you can do whatever you want. It is worth sticking to standards, but external ones, which we will get to later.
The question remains how to create such a standard. Earlier I used the word "document", and indeed what comes to mind is a specific text file describing and containing all the rules. At Wilda Software we also approached it that way once, but after years we started to have more and more doubts about whether that was a good approach. Creating a proper document is labor-intensive and doesn't necessarily mean everyone will easily find in it what they need. And you have to admit that a coding standard is not a book with a good plot that you devour. People aren't necessarily interested in the history of how individual rules came about (unless as trivia, which helps them remember the details even better). They want concrete guidelines they can look up when needed and find quickly. That's why, for some time now, our standard has been created in the form of examples: a few larger fragments of code that make no functional sense but show ideal code in terms of style. We have the impression that writing guidelines in this form is not only faster but also easier to absorb, because you can visually compare individual elements with your own code. Still, every team has to work this out for itself.
Whether we write a full text or an example, we can't do without additional descriptions, especially for naming rules. Where possible, it is also worth briefly commenting on the reasons why individual guidelines were introduced. On the one hand, because young programmers in particular like to ask "but why, and what for" when they check their theoretical knowledge against practice. On the other hand, as I wrote, it sometimes makes certain things easier to remember. Think back to when learning history was easier: when we relied on dates and dry descriptions alone, or when we heard various anecdotes about the Battle of Grunwald. Here such an anecdote can be the reason why we recommend wrapping the body of every loop or conditional statement in curly braces, even when it is just one line. Remember the story of the SSL bug in Apple's code. It's also a good example showing that a proper standard can spare us various misadventures (even if the situation described is simply an ordinary, though serious, bug).
It is also worth remembering that code editors have mechanisms that let you control to some extent whether you write according to the standard. These are usually configuration files to load, or specific settings. In some cases this can also serve as appropriate "documentation" of how code is written in a given team.
One last thing worth noting in this section: as I suggested earlier, a coding standard is not universal for every programming language. It can be, but usually isn't, because every technology comes with some internal standards, which your own standard, adapted to the given team, should be based on.
How strictly should you stick to the standard?
That's a good question. Of course, the answer "fully" suggests itself, and for some time that was our approach in the team during code reviews. But like everything in life, it pays to be moderate.
It's not that a person who doesn't stick to the standard is immediately condemned and should go jump into a volcano (greetings to Maciek and Tymon). As I wrote, sometimes stylistic differences aren't big enough to be judged negatively. From the point of view of the project schedule, it also makes no sense to do a meticulous code review for this alone. You can assume that remarks about aesthetics should be either immediately noticeable or a clear threat to the later development of the software. It is worth paying more attention to reviews of young programmers who are still finding their feet, so that they build good habits that won't be a ball and chain in later collaboration.
In addition, don't forget that a standard doesn't have to be set in stone. It's natural that with the development of technology, programmers' skills or simply new experience, some guidelines may change. That's why deviations from the imposed rules shouldn't always be treated as mistakes and pointed out like on an exam. It is good to look at them from time to time and ask whether certain changes to the standard could help the team or the project.
The standard standards for programming languages
I mentioned earlier that your own standard should be based on external guidelines specific to a given technology. Some will probably say that you should rely only on worldwide standards, because... they are worldwide standards. Whichever school of thought we belong to, it is worth getting familiar with the "official" guidelines.
For the PHP language that is PSR, or PHP Standards Recommendations, which is not one document but a collection of them with various guidelines. In places you can see that it is strongly rooted in its origins in C/C++, and those are the elements we adapted to our needs at Wilda Software. Still, as I wrote, it is worth a look at such proposals that are considered generally binding.
There isn't always a single set of official guidelines for one language. Java is such a popular and "corporate" programming language that it has become the object of interest for various companies, such as Google or Oracle, each of which created its own standard (although the latter is already years old, even ages old). On the web you can also find standards developed by, for example, the University of Hawaii and many other institutions.
It's no different with any other language: Python, JavaScript and the rest. You don't necessarily have to follow them fully, but it is good to know that by staying as close as possible to the "baseline", you also stay closer to global standards.
On the internet you can look for information on this topic under the terms coding standards or coding guidelines.
Is coding the only thing that needs a standard?
Most often people talk about a coding standard, but it isn't the only thing that should be regulated, as the links to worldwide standards above show. At Wilda Software we also have, for example, a standard for creating database schemas, which may be even more important, because modifying a schema often causes more trouble than refactoring code. Besides, the ability to quickly find your way around large relational databases and queries against them is just as valuable as the ability to navigate code.
Another example is guidelines for writing documentation, although here it is worth remembering that each document may need a specific approach depending on the project or audience it describes. Still, it doesn't hurt to work out at least a shared document template, for example for writing requirements or for software architecture.
Finally, there are also not so much standards as procedures for various cases: technical (deploying a new version to a server), organizational (the procedure for welcoming a new person to the team) or business (guidelines for correspondence with the client).
Summary
Coding standards are an interesting topic, though basically a simple one. They are meant to prevent code written by different people from drifting apart and thus make building software easier. This area is also linked to writing code documentation, which is a much broader subject. But you have to start somewhere and improve your craft in small steps, because standards serve that purpose too.
Best regards and thank you, Jakub Rojek.


