Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to format code?

Tags:

When I write code, I try to group lines of similar code together, then leave a blank line and write another block.

I believe this contributes to the neatness and readability of the code.

I'm not a big fan of bunching stuff together without any line spacing. It looks like hell, it's hard to read and it's difficult to follow.

One of the teachers I had, downgraded one of my assignments because I had spaced my code logically. He said, 'When you have to read code all day in the real world, you won't put this line spacing in and you'll be thanking me." Of course, I never did and never will thank him.

Now that I'm in the real world, most of the code files I see that have absolutely no line spacing are poorly written and poorly thought out.

This is probably more prevelant in VB type languages than in C type languages, but the same concept applies.

Two questions come to mind:

  • Where do you leave a blank line in your code?
  • How much line spacing is too much?
like image 710
user72491 Avatar asked Mar 12 '09 13:03

user72491


People also ask

What does it mean to format your code?

Source Code Format means a form of computer program, or any portion thereof, written in a programming language employed by computer programmers that must be compiled or otherwise translated before it can be executed by a computer.

How do I format C code?

The C/C++ extension for Visual Studio Code supports source code formatting using clang-format which is included with the extension. You can format an entire file with Format Document (Ctrl+Shift+I) or just the current selection with Format Selection (Ctrl+K Ctrl+F) in right-click context menu.

Why should you format your code?

Consistently using the same style throughout your code makes it easier to read. Code that is easy to read is easier to understand by you as well as by potential collaborators. Therefore, adhering to a coding style reduces the risk of mistakes and makes it easier to work together on software.


1 Answers

I follow Microsoft's Guidelines for C#.

Edit: The standard for C# is Don't fight the IDE. If you hit CTRL K+D, the IDE will automatically put blank lines in between code sections.

To follow that up, if you look at C# sample code on MSDN or anywhere else, there's normally a blank line in between each logically placed group. So There will be a blank line after all your member variables, a blank line after each method, etc.

In response to the comments expressing shock and horror that I use an IDE for C# programming:


REAL PROGRAMMERS

like image 90
George Stocker Avatar answered Oct 22 '22 23:10

George Stocker