Ripe bananas completely filling the frame

A whimsical (but useful!) code style guideline

Software engineers have lots of opinions about code style. Pick a number between 50 and 150 and somebody on the internet will adamantly argue that a line of code can have that many characters but no more!

Instead of a rigid numeric limit, consider adopting the delicious fruit-based Banana Rule:

🍌 A line of code should not be longer than a banana

I can already hear the detail oriented readers cringing at the imprecision of this rule.

This fuzziness is intentional, to dissuade excessive discussion. Agreeing on the perfect line length is less important than generally writing readable code. You probably won’t agree, anyway. Under the Banana Rule, if a line is plausibly shorter than a banana then everything is peachy!

The Linux Kernel adopted a similar guideline in 2020. Their guideline was unfortunately fruitless (as in it did not involve fruit, not that was ineffective).

The Banana Rule is just so fun (and delicious) to use. Notice a long line of code in a PR? Grab a handy banana, hold it up, take a picture, add it to a Github comment, eat banana.

A picture of a banana sitting on a MacBook Pro screen with some code open in Github in the background.
The banana rule in action. Most of this code looks pretty good, although the import on line 7 is getting pretty long.

A natural corollary of the banana rule is:

🍌 A line of code that is shorter than a banana will probably be less clear if shortened further

Shorter isn’t strictly better with line length; go too short and the lower vertical information density leads to more scrolling.

(Refactoring to shorter lines leads to longer methods. I’ve found if you can’t see a whole method at once on screen it’s a lot harder to understand it. The banana rule can be used vertically too!)

Why a banana, anyway?

Our thorough testers tried a variety of options:

  • Pickles (too wet)
  • Durian (too big, smells weird)
  • Blueberries (99% of the codebase failed)

Happy April Fools’ Day!