Keep Methods Short & Stop with the Curly

Technorati Tags: ,,

There are two major annoyances when it comes to good code and readable code.  Here are 2 things that really every team should ensure happens (at least in my opinion ).

1) Any method should not be more than 50 lines but preferably < 25 if possible.  I cannot stand huge methods.  The point of OOP is to abstract logic out into manageable pieces.  Methods are meant to mainly do one unit of work.  Not 5 units to make up 1 unit.  Put your logic in reusable methods so that they are not only reusable, but READABLE!  I’d much rather see a bunch of case statements in a method but in between calls to other methods to make the entire thing readable.

2) Put the damn curly on the next line:

   1: if(something){
   2:    //do something
   3: }

   that is the most irritating thing to read.  Every time I see that curly on the right, it just irritates me and I move it back down for readability when I see this. 

   Why doesn’t the person just put it on the next line to make it much more readable:

   1: if(something)
   2: {
   3:   // do something
   4: }

I mean are we that lazy?  Do you REALLY think that the first one is actually more readable?  If so I think you were born cross eyed and must see that way.  You get an entire page of if statements throughout and nested, you start to see how that becomes very unreadable.  If you are fine with it, more power to you but I think it looks like a pile.

I wish programmers would pay more attention to formatting, because it makes code so much more readable and saves so much time overall for anyone who has to read it.

I am kinda open ended on the one-statement after the if decision on whether to include a {}.  You are not required in C# to include {} if there is only one statement after the next line in a single if statement without an else.  If you are going to make the decision to include the {} then for readability, make sure the first is on a next line and keep it consistent like the last }. 

Thank goodness I’m not coding in VB, I’d hang myself with the Subs, End Sub, and all the other extra verbose crap that I’d have to type. 

This may seem like something not worth mentioning to some, but really, it makes a big difference and I just don’t get the lazy #2.  You can’t debate #1 though and I wish though people would stop making 50+ line methods.

My rant for today.


Print | posted on Monday, December 08, 2008 8:53 PM

Comments on this post

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
While I agree that methods should be concise and adhere to the SRP (Single Responsibility Principle), isn't sticking an arbitrary number of lines on it a bit, um, arbitrary?

Left by Lee Dumond on Dec 09, 2008 12:17 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
I'm certainly not saying you have to stick with an arbritray number exactly. Obviously programming does not work that way and each method is relative dependent on what that method is doing. However, methods are meant to do one unit of work, not 5,10,15.

The Cyclomatic Complexity of your code (which is what I based that point on) is a very good measure to check your methods against. No, I am not saying you must have x number of lines. However, if you do have more than 25 start looking at your method. If you have more than 50, start looking to refactor some parts of that method into other methods or look at what you are doing because that's way too many lines of code for a method in my opinion and I agree with this measure as a good formula to check your code against at all times.

see here:

www.codezest.com/.../...-standard-for-methods.aspx
Left by Dave Schinkel on Dec 09, 2008 7:02 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
The sole benefit of placing the opening curly to the right of any code block is it makes the entire file of code more readable in your IDE. The more lines of code you can see without scrolling, the more implicitly readable your code is. You can't read what you can't currently see, and the curly-to-the-right method allows you to see more at one time.
Left by Will Rogers on Dec 09, 2008 9:16 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
@Will Rogers
I guess to me this is not such a benefit. I don't mind scrolling a little more knowing that my eyes read easier when the right curly is on the next line. That's why I have page up, page, down, etc. Scrolling is always part of your classes and if you use regions wisely or even if you do not use regions, I don't mind scrolling. It's like trying to keep a resume 1 page long. Not going to happen so why not make the resume more effective. That's how I view this issue.

It just doesn't make sense to have a right curly on the same line when all others are on its own for readability in my opinion.
Left by Dave Schinkel on Dec 09, 2008 9:33 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
Placing the open brace at the end of the line is a precedent set by the K&R C programming book. I agree with Will Rogers. It makes code more readable...but I have given up doing it because no one else in .NET land wants to.
Left by J.P. Hamilton on Dec 09, 2008 9:33 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
@J.P. Hamilton

Yes, I see that also. Would be nice if developers did take the time to care about it as we pour through code so much every day.
Left by Dave Schinkel on Dec 09, 2008 9:56 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
Programmers read by indentation. Braces are for the compiler. It really doesn't matter where they are, as long as they're between the right pair of tokens and the rest of the file is indented well.
Left by Ricky Clarkson on Dec 09, 2008 11:37 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
wow OTBS rears its head again.

Here's a tip from a very grizzled vet of the c/c++/java/c# wars. Learn to read and appreciate each style. IMO as long as a source listing contains a consistent style, one should be able to read it with no difficulties.
Left by Keith Sader on Dec 09, 2008 11:56 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
@Keith Sader

Here's the thing. If you are going to put a } on the next line, do the same for the beginning {

@Ricky Clarkson
Programmers also read the code, not just indentation. Compiler reads it all. Indentation is critical which another rant of mine.
Left by Dave Schinkel on Dec 09, 2008 1:16 PM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
I have learnt over the many years to read any style. Positioning of braces if a personal preference. There are arguments for and against any particular position, and none are any more compelling than than how you must drink your coffee.

If you can't cope, set your IDE to reform the code the way you like it.
Left by Experienced on Dec 09, 2008 11:52 PM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
the scrolling part and the kernigan and richie reference do have a part to play in the evolution of ON screen display. in those days, you typically had a screen format of 80x25 and therefore those tricks were employed in order to maximise code lines on screen. nowadays, you can fit whole class definitions into 1280x1024 screen resolutions, so both the requirement and construct are moot points.

readability and consistancy are king here. of course, using the K&R method does have a kind of 'i come from a c background' sort of 'kudos' and some may hide their inadequacies beneath this veneer.

i say plus 10 to the:



1: if(something)

2: {

3: // do something

4: }

construct...
Left by jimi on Jan 09, 2009 11:56 AM

# re: Keep Methods Short & Stop with the Curly

Requesting Gravatar...
also,

meant to add that one of the things that i like least about one of my favourite books (C# Design Patterns by Judith Bishop) is that it employs the K&R brace formatting. you do get used to it but returning to your own code with 'proper' :) brace formatting is SOO much nicer...

anyway, 2 men in a room 3 opinions!! :)
Left by jimi on Jan 09, 2009 12:09 PM

Your comment:

 (will show your gravatar)
 
Please add 4 and 6 and type the answer here: