Sunday , May 16 2021

The 80/24 Rule, Hacker News


Write small blocks of code. How small? Here’s how small.

One of the most common questions I get is this:

If you could give just one advice to programmers, what would it be?

That’s easy:

Write small blocks of code.

Small methods. Small functions. Small procedures.

How small?

Few lines of code#

You can’t give a universally good answer to that question. Among other things, it depends on the programming language in question. Some languages ​​are much denser than others. The densest language I’ve ever encountered isAPL.

Most mainstream languages, however, seem to be verbose to approximately the same order of magnitude. My experience is mostly with C #, so I’ll use that (and similar languages ​​like Java) as a starting point.

When I write C # code, I become uncomfortable when my method size approaches fifteen or twenty lines of code. C # is, however, a fairly wordy language, so it sometimes happens that I have to allow a method to grow larger. My limit is probably somewhere around 25 lines of code.

That’s an arbitrary number, but if I have to quote a number, it would be around that size. Since it’s arbitrary anyway, let’s make it24, for reasons that I’ll explain later.

The maximum line count of a C # (or Java, or JavaScript, etc.) method, then, should be 24.

To repeat the point from before, this depends on the language. I’d consider a 24 – lineHaskellorF #function to be so huge that if I received it as a pull request, I’d reject iton the grounds of sizealone.

Narrow line width#

Most languages ​​allow for flexibility in layout. For example, C-based languages ​​use the;character as a delimiter. This enables you to write more than one statement per line:

varfoo=32;var(bar)=foo 10;Console.WriteLine(bar);

You could try to avoid the 24 – line-height rule by writing wide lines. That would, however, be to defeat the purpose.

The purpose of writing small methods is to nudge yourself towards writing readable code; code that fits in your brain. The smaller, the better.

For completeness sake, let’s institute a maximum line width as well. If there’s any accepted industry standard for maximum line width, it’s 80 characters. I’ve used that maximum for years, and it’s a good maximum.

Like all other programmers, other people’s code annoys me. The most common annoyance is that people write too wide code.

This is probably because most programmers have drunk the Cool Aid that bigger screens make you more productive. When you code on a big screen, you don’t notice how wide your lines become.

There’s many scenarios where wide code is problematic:

  • When you’re comparing changes to a file side-by-side. This often happens when you review pull requests. Now you have only half of your normal screen width.
  • When you’re looking at code on a smaller device.
  • When you’re getting old, or are otherwise visually impaired. After I turned 40, I discovered that I found it increasingly difficult to see small things. I still use a 10 – point font for programming, but I foresee that this will not last much longer.
  • When you’remob programmingyou’re limited to the size of the shared screen.
  • When you’re sharing your screen via the web, for remote pair programming or similar.
  • When you’re presenting code at meetups, user groups, conferences, etc.

What most programmers need, I think, is just anudge. In Visual Studio, for example, you can install theEditor Guidelinesextension, which will display one or more vertical guidelines. You can configure it as you’d like, but I’ve mine set to 80 characters, and bright red:

Screen shot of editor with code, showing red vertical line at 80 characters.

Notice the red dotted vertical line that cuts throughuniverse. It tells me where the 80 character limit is.

Terminal box#

The 80 – character limit has a long and venerable history, but what about the 24 – line limit? While both are, ultimately, arbitrary, both fit the size of the popularVT 100terminal, which had a display resolution of (x) characters.

A box of 80 x 24 characters thus reproduces the size of an old terminal. Does this mean that I suggest that you should write programs on terminals? No, people always misunderstand this. That should be the maximum size of a method. On larger screens, you’d be able to see multiple small methods at once. For example, you could view a unit test and its target in a split screen configuration.

The exact sizes are arbitrary, but I think that there’s something fundamentally right about such continuity with the past.

I’ve been using the 80 – character mark as a soft limit for years. I tend to stay within it, but I occasionally decide to make my code a little wider. I haven’t paid quite as much attention to the number of lines of my methods, but only for the reason that I know that I tend to write methods shorter than that. Both limits have served me well for years.

Example#

Consider this example:

publicActionResultPost(ReservationDto)  DTO) {var(validationMsg)=Validator.Validate(dto);if(validationMsg!="")returnBadRequest(validationMsg);  varreservation=Mapper.Map(dto);varreservations=Repository.ReadReservations(reservation. Date);  var(accepted)=maîtreD.  CanAccept(reservations,reservation);if(!accepted)return(StatusCode) ******************** ()StatusCodes.  InternalServerError,"Couldn't accept.");  var(id)=Repository.Create(reservation);return(Ok)  (id); }

This method is 18 lines long, which includes the method declaration, curly brackets and blank lines. It easily stays within the 80 – character limit. Note that I’ve deliberately formatted the code so that it behaves. You can see it in this fragment:

returnStatusCode(StatusCodes.  InternalServerError,"Couldn't accept.");

Most people write it like this:

returnStatusCode(StatusCodes. Status 500 InternalServerError,"Couldn't accept.");

That doesn’t look bad, but I’ve seen much worse examples.

Another key to writing small methods is to call other methods. The abovePostmethod doesn’t look like much, but significant functionality could be hiding behindValidator.Validate,Repository.ReadReservations, ormaîtreD.CanAccept. I hope that you agree that each of these objects and methods are named well enough to give you an idea about their purpose.

Code that fits in your brain#

As I describe in myHumane Codevideo, the human brain can only keep track ofabout seven things. I think that this rule of thumb applies to the way we read and interpret code. If you need to understand and keep track of more than seven separate things at the same time, the code becomes harder to understand.

This could explain why small methods are good. They’re only good, however, if they’re self-contained. When you look at a method like the abovePostmethod, you ‘ll be most effective if you don’t need to have a deep understanding of how each of the dependencies work. If this is true, the method only juggles about five dependencies:Validator,Mapper,Repository, (maîtreD) , and its own base class (which provides the methodsBadRequest,StatusCode, andOk). Five dependencies is fewer than seven.

Another way to evaluate the cognitive load of a method is to measure itscyclomatic complexity. ThePostmethod’s cyclomatic complexity is3, so that should be easily within the brain’s capacity.

These are all heuristics, so read this for inspiration, not as law. They’ve served me well for years, though.

Conclusion#

You’ve probably heard about the80 / 20 rule, also known as thePareto principle. Perhaps the title lead you to believe that this article was a misunderstanding. I admit that I went for an arresting title; perhaps a more proper name is the(x) rule.

The exact numbers can vary, but I’ve found a maximum method size of 80 x 24 characters to work well for C #.

Brave Browser
(Read More)
Payeer

About admin

Check Also

financial district san francisco

Financial District San Francisco, hilton

financial district san francisco , hilton Financial District San Francisco is tourist attraction place it is …

Leave a Reply

Your email address will not be published. Required fields are marked *