Cross-language Best Practices
Every programming language has its Best Practices, but some of them seem to have crossed boundaries and have found a place in other languages.
Sometimes they’re exactly the same; sometimes they’re a bit different.
Here are are some interesting facts:
1. TODO comments
There seems to be a tendency to write comments with TODO, FIXME, BUG and the likes in many different languages.
Here are three different patterns integrated at Codacy regarding this behavior:
- Java (from Checkstyle) — A check for TODO comments
- Scala — Prohibit todo/fixme tags in comments
The patterns refer to the same thing but they each have their own intricacies:
- in Java the pattern look for the terms FIXME and TODO
- in Scala the pattern has an option for case sensitivity and the default terms are FixMe and ToDo
I still remember when a tab was always 8 spaces and everybody used tabs for indentation.
Times changed: tabs started having different lengths depending on the editor being used (and on the configuration in place) and, eventually, bits of code got a bit difficult to read. The result was a general distaste for the tab character.
Lots of static analysis tools now have patterns related to this. Here are a few that are integrated at Codacy:
- Java — Checks to see if a file contains a tab character
- PHP — Enforce space indentation (Code must use an indentation of 4 spaces without tabs)
- CoffeeScript — No Tabs
- Python — Prohibits a mix of spaces and tabs
- Python — Prohibits an unexpected number of indentation’s spaces
- Scala — Prohibit tabs in a file (Prohibits from using tabs for indentation)
- Ruby — Indentation Width
A few interesting points regarding these patterns:
- Java, Scala, PHP and CoffeeScript’s patterns reject tabs
The names differ, but they mostly focus on the same kind of things: prohibiting tabs, enforcing that indentation is done with a specific number of spaces or that spaces and tabs are not both used for indentation purposes.
As a side note, it should be said that there are several Ruby patterns regarding Indentation.
3. LINE LENGTH AND OTHER LENGTHS
Long lines make it difficult to read and understand code. Some of the patterns present at Codacy regarding line length:
- Ruby — Line Length (default value is 80)
- Python — Enforce line max length (default value is 120)
- Java — Check long lines (default value is 80)
- Scala — Enforce line max length (default value is 160)
It’s interesting to see that the default values differ from language to language, and out of these only Ruby and Java have the historic 80 as default.
In any case, if you think about it and if you analyze a couple more languages, there might be some sort of correlation between the original creation of the language and the maximum line length recommended, which seems to imply that each language is still a bit tied to the screens available back when it was created.
Maximum line length vs. Year of creation (please try not to take this chart too seriously)
There are also other places where length is taken into account:
- Ruby — Method Length
- Java — Enforces the care you should take when looking at class file lengths
- Java — The Check validate abbreviations (consecutive capital letters) length in identifier name, it also allow in enforce camel case naming
Some best practices are best practices everywhere, albeit with a little tweak here and there.
Default values differ from language to language, but some things are simply good ideas everywhere (conversely, some other things are bad ideas everywhere).
Not all of the patterns presented are enabled by default at Codacy, but the most relevant ones definitely are.
Add your project and start building better code today.
Edit: We just published an ebook: “The Ultimate Guide to Code Review” based on a survey of 680+ developers. Enjoy!
Codacy is used by thousands of developers to analyze billions of lines of code every day!
Getting started is easy – and free! Just use your GitHub, Bitbucket or Google account to sign up.