Two Rules to Say Goodbye to Shit-Mountain Code

Every dev team has a coding standard on file, but are there simple, workable rules that actually make our code more readable and more maintainable?

Every dev team treats the coding standard as a required document, and we always hope it will solve our maintainability problems. But the ideal is plump and reality is bony.

Take the naming conventions we run into all the time. They mostly cover four points:

  1. Rules for when to use upper camel case, lower camel case, underscores and the like
  2. Rules for using special characters such as _ and $, and for numbers
  3. Variables and constants are nouns; method names are verbs
  4. Highly readable, with names whose meaning is obvious at a glance

Obviously, apart from point 1, the rest are very hard for a tool to handle. Tools (AI aside) can't do anything about list1, list2; they can't tell verbs from nouns, nor pinyin from English; and they certainly can't judge whether a name speaks for itself — in domain software that even requires some business background knowledge. Yet self-explanatory naming is the single biggest contributor to maintainability.

So you can see how hard it really is to make code maintainable. Is there a standard that can solve most of these code problems? Yes, there is:

Maximum lines per file, maximum lines per function

These two are rarely mentioned in most standards, or overlooked by most people. Yet they're the rules I personally recommend most. Obviously, these are checks a machine can complete just by scanning, which makes them very easy to enforce.

Tell a developer to modularize, to make each function do one thing, to avoid repeating themselves... and they won't even bother to listen. But once you set limits on file length and function length, all of that happens naturally.

In past projects I've set a maximum of 600 lines per file and 30 lines per function.

/* eslint */
{
  "rules": {
    "max-lines": ["error", 600],
    "max-lines-per-function": ["error", 20]
  }
}

Take the front end. When a file hits 600 lines, developers start wondering how to move some of the code out. A beginner will probably move the style file out first. The old hands may laugh at that, thinking they've moved out things that shouldn't have been moved. But it doesn't matter: as the business grows more complex, so do the pages, and they'll find that this tactic of dodging the hard part gets them nowhere.

So they start turning parts of the code into components; parent-child component interaction, global state management and so on get learned and gradually put into practice. And that's how modularization slowly gets done.

Eventually the component files get too long as well, and they start thinking about reuse — extracting the highly repeated code into shared utilities that behave differently depending on the parameters. So the rules of "one function does one thing" and "don't repeat yourself" quietly start showing up in their code too.

There's also the physical side of it: at 14px font size with 1.5 line height — 21px — 30 lines of code occupy 620px of height, so a whole function fits on a 1366×768 laptop screen. Folded up, 600 lines comes to roughly 20–40 functions, which fits in 1–3 screens. That makes it easy to scan as well.

So I recommend everyone add this simple, easy-to-enforce rule to their company's standards.

From then on, shit-mountain code is no longer a mountain — at worst it's a bunch of little turds, and those are much easier to clean up. Haha~