100 lines of code per file

Tue Oct 25 2022

It has gotten to my attention that one of the biggest dev companies in the region has a pull-request requirement that checks whether each file committed is under 100 lines long.

It seems like a pretty stupid requirement, right?

Then I tried it.

I had a class that was 133 lines long and I want to see whether I can get it under 100. As the class was already “doing one thing” it didn’t make sense to refactor it and split it into two files.

First to go were the comments 😀 Method docblocks were replaced with proper type declarations and return types. To preserve the readability, variable and method names had to be changed to be self-explanatory although a bit longer. Conditionals were extracted into separate methods.

Multiple simple IFs were combined into one, which did hurt readability a bit but it saved 5 lines 😀

Somewhat duplicated code was extracted and parametrized which allowed me to reuse methods in several locations.

And I got to 99 lines 🚀

I was skeptical about this requirement when I started but now I see its benefits. It makes you think about your code a bit more and forces you to make it more readable while also removing clutter from your files. And as strange as this may sound, it was fun doing it 😆

If you like this article consider tweeting or check out my other articles.