My Authors
Read all threads
I created a @MATLAB function to help me write better code.

A thread.

github.com/Remi-Gau/check…

1/16
A lot of us (at least in neuroscience) learn @MATLAB by aping scripts we have borrowed from someone.

One day you were given some .m files, a tap on the shoulder, a "good luck" and you have been reverse engineering the wheel of what it means to write good code since then.

2/16
One of the reason for this is that people, whose code we are learning from, are as likely as you to have taken 'How to write good code 101'.
i.e very unlikely but don't feel bad those classes are usually not proposed in most neuroscience departments anyway.

3/16
So we don't improve because we are rarely exposed to "good" code and bad habits are passed down from one PhD generation to the next.

4/16
At an individual and at a lab level all of this is the fastest way to end with a codebase of @MATLAB matlab scripts and function of 1000 lines or above that that have more loose ends than a bowl of spaghetti.

5/16
Because we all need pointers when it comes to make our codebase better, I wanted to be able to type 'check_my_code` into @MATLAB and know if things were getting out of hand and where I should focus my attention to start cleaning my code.

6/16
The idea for this is partly inspired by this "equivalent" in #Python.

github.com/PyCQA/mccabe

7/16
The good thing is that the @MATLAB linter gives you the the McCabe complexity of each function.

mathworks.com/help/matlab/re…

8/16
The what you ask?

The McCabe complexity of a function is roughly how many paths one can take while navigating through all the conditional statements of the function (`if`, `switch`, ...).

en.wikipedia.org/wiki/Cyclomati…

9/16
If it gets above 10 you enter the danger zone.

If you are above 15, you want to seriously consider refactoring your code (with sub-functions for example).

en.wikipedia.org/wiki/Code_refa…
refactoring.com
refactoring.guru/refactoring

10/16
The function will also roughly check if there are enough comments in the files to make sure things are not turning into a nightmare to debug or to read, for others or for yourself in 6 months.

11/16

tiktok.com/@delaina00moor…
I also wanted to find a way to automate this and make sure this function would run regularly so that I would not "forget" to check the quality of my code.

12/16
I mean we don't brush our teeth only on the day before we go to the dentist.

So why do we say that we will clean our code when we are ready to submit our paper? or after it has been published 😱😱😱 ?

And then we wonder why our code stink.

13/16
Because setting up a continuous integration to do that with @MATLAB is turning out to be...

14/16
So I created a git hook template that just needs to be added into your local repository that will check the quality of your code every time you push your code.

github.com/Remi-Gau/check…

15/16
Obviously you could also add extra checks into this hook to run your unit tests to make sure you have not broken anything: not as good as real CI but hey...

THE END

16/16
Shamelessly tagging here people who might be interested:
@CyrilRPernet @Ch_Phillips @fmrwhy @cassgvp @cmaumet @cMadan @Giga_CRCivi @agahkarakuzu @_mattboud @BattalCeren
Addendum:
In case anyone wonders why I did not mention Octave or why this does not work with octave.
Well it is because this octave does not have a linter equivalent to the matlab one and that this whole function relies on the matlab internal linter.
Some quick search showed me that there is actually an attempt at creating a style checker for matlab AND octave and it is written in... python.

github.com/florianschanda…
And in case want to know more about some of the idiosyncrasies of the matlab 'language':
github.com/florianschanda…
github.com/florianschanda…
@o_guest In case someone says you're biased you can send them to those and say

'SEEE!! Even matlab users says it's weird.' :-p
Missing some Tweet in this thread? You can try to force a refresh.

Enjoying this thread?

Keep Current with Remi Gau

Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

Twitter may remove this content at anytime, convert it as a PDF, save and print for later use!

Try unrolling a thread yourself!

how to unroll video

1) Follow Thread Reader App on Twitter so you can easily mention us!

2) Go to a Twitter thread (series of Tweets by the same owner) and mention us with a keyword "unroll" @threadreaderapp unroll

You can practice here first or read more on our help page!

Follow Us on Twitter!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3.00/month or $30.00/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!