Last Updated: February 25, 2016
·
1.816K
· mcansky

Stay sane and improve your code with software metrics

Some time ago I was known for writing complex code, following my inspiration. Old timers know that this is bad practice and many people pushed me to TDD and other good practices to improve the general quality of my code.

TDD, refactoring and other good practice will help you get better at writing code. Yet some tools do help to push you in that direction and even further.

Among the tools I discovered on this quest were :

Flog is great but since I discovered cane and codeclimate I tend to use the later ones. Yet Flog is great to really get a massive slap in the face sometimes : the scores can be quite high sometimes (the higher the worse).

Cane is nicer, it's not exactly the same idea : it does check code complexity but it also checks your code against some rules (think 80 chars long lines, comments at the top of the file etc ...). To me it's easier to read the output of cane than the one of flog. Cane can be easily added to the build process too.

CodeClimate is a SaaS, it's not free for your private code and it might look expensive. Yet if you have 3, 5 or more projects going on in your company it doesn't look too expensive I believe.

There is plenty of other tools, I will use Cane and CodeClimate as examples in the following part, but it's quite a general advice so you can use any other tool.

How

I use Cane and CodeClimate at the moment. Cane is just here for me to check that I respect some basic writing rules. CodeClimate well, it's another thing : it pushes me to be a better developer.

CodeClimate rate your code using several things :

  • complexity
  • duplication
  • security

You get a clear rating of :

  • each file (with a letter)
  • the whole project (a number)

With this you get a big slap because your code smell and it's quite visible ! Nothing hidden under the carpet !

If you have complex code CC will show you the lines and tell you how bad it looks.

If you have code duplication (or similarities) CC will show you the lines and the files.

It will not tell you what to do but where you need to improve things. It's up to you then to use some nifty tricks, look around for solutions, ask for advice from your peers etc ...

I believe flog and cane can do that too, but CC is making it quite easy with a clean, simple web interface.

Why

If you don't already have a good discipline in your code you might want to have a look at those tools. It will help you get motivated and improve.

If you do have a good discipline already well .. you might still want to keep an eye on the score and have this automated eye checking all your code, just in case, to help you keep the score high.

The score thing make those tools great motivators in the end :

  • you get happier to work on improving your code
  • you get saner code
  • you get better at your craft

Videos and docs

You might want to check those three links to get some more ideas.

And yes my code did get better. Thanks to all those tips, advices and code I have received.