Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

empty lines in functions/methods

Tags:

coding-style

At our company's Xmas party we had almost a physical fight over one issue: whether or not to allow empty lines in a function/method in our code (c/c++/c#). This is all about just single blank line, for example imagine code like:

 private void LoadData()
        {
            if (!loaded)
                return;
            DataStore coll;
            SqlData s = new SqlData();
            if (!s.CallStoredProcedureToStore(out coll, "xy.usp_zzz",...))
                return;

            dataViewXy.BeginUpdate();
            dataViewXy.Items.Clear();
            for(int i = 0; i < coll.RowCount; i++)
            {
                dataViewXy.Items.Add(coll[i]["ID"]);
            }
            dataViewXy.EndUpdate();
        }

this is a nonsensical mix, just to illustrate situation - first block of function loads data, second one fills some kind of data control and the blank line separates them.

One should also have written a comment before each 'block', but the important thing is that the for guys would place a blank line before that comment and the against guys would not.

For: Allows programmer to logically separate pieces of function so it improves readability as some kind visual cues for the eyes to follow

Against: Decreases readability and programmer should use comment for separating pieces of the function instead.

My personal opinion is to allow blank lines in functions, because without them long pieces of code look to me like a never ending stream of lines without any cues about where to find the stuff I'm looking for)

like image 890
Axarydax Avatar asked Jan 04 '10 10:01

Axarydax


People also ask

How do you use blank lines in your code?

Blank lines improve readability by setting off sections of code that are logically related. Two blank lines should always be used in the following circumstances: Between sections of a source file. Between class and interface definitions.

How do you use blank lines in Python?

Use blank lines in functions, sparingly, to indicate logical sections. Python accepts the control-L (i.e. ^L) form feed character as whitespace; many tools treat these characters as page separators, so you may use them to separate pages of related sections of your file.

Do blank lines matter in Python?

The empty lines are there for better visibility when reading or writing code. This does not add to or take from the program as empty lines are ignored.

What are blank lines?

Definition of blank line : a line on a document that marks where one should write something Sign your name on the blank line.


2 Answers

I think that an empty line in method body is a code smell. Read this blog post about this very subject: An Empty Line is a Code Smell. In short, empty lines in method body is a bad practice because a method should not contain "parts". A method should always do one thing and its functional decomposition should be done by language constructs (for example, new methods), and never by empty lines.

like image 131
yegor256 Avatar answered Nov 24 '22 21:11

yegor256


That will be awesome duel to watch. Just like the final scene in The Good, The Bad, and the Ugly but over method whitespace instead of gold.

Honestly, this shouldn't even matter unless you need to constantly mull over someone else's code in your team on a pretty frequent basis. Personally, I am all for whitespace as it gives a logical grouping. But it might not be the same for someone else. Heck, even I might change the way I group things over time. Grouped items that previously used to make sense might not anymore.

Regardless of how we group items, I think grouping is very important. We can argue over why empty lines are unnecessary, but the fact is that the brain can only handle a limited amount of information at a time. So if I can understand the function as composed of three subgroups for instance, instead of ten statements, that makes a lot of difference. And grouping by comments is subjective to your IDE. Most IDE's color them lightly than the rest of the code which gives that feel of separation, but that becomes IDE specific.

So to argue for the point, it's all about grouping. If a class becomes too big, we have to break it for the sake of understandability and maintenance. Similarly, if the sequential lines of code inside a method becomes too much too grasp, it must be broken down more methods or logically separated with a blank newline.

Also, if the argument is going nowhere, throw in some random stuff like how these empty lines relate to the Buddhist concept of nothingness bla bla..

like image 22
Anurag Avatar answered Nov 24 '22 23:11

Anurag