DEV Community

Cover image for 100 lines of code per file
Luka Peharda
Luka Peharda

Posted on • Originally published at lukapeharda.com

100 lines of code per file

It has gotten to my attention that one of the biggest dev agencies 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 ๐Ÿ˜†

Top comments (0)