I'm not sure where I should add new blank line(s) in my source code. I would like to write beautiful code that is both easy to read and understand.
Is it object-based only, or concept-based开发者_开发百科? Answers with examples would be most helpful to me.
Personally I don't use much vertical whitespace - other people will recommend more than I describe below. As long as there's some kind of visual separation between separate things, I want to look at code, not blank lines. I tend to put:
- A single blank line between non-member functions (and, in C++, between member functions defined outside the class)
- A single blank line between classes.
- A single blank line before and after comments that relate to multiple functions/classes that follow, i.e. that define "sections" in the code: "helper functions for XML processing", that sort of comment.
- No blank line between member functions in a class
- Usually no blank lines inside a function. If a function goes in several stages, I might put blank lines between them, or just a comment between them explaining the stages. Very long functions are more likely to be broken up with whitespace in this way, not that I consider very long functions a particularly good thing. If a member function does have internal blank lines, I'd normally separate it from other member functions in the class with blank lines too.
Two consecutive blank lines look a bit suspect to me, and I hardly ever do that myself. Three is just wasting perfectly good space on my monitor - anyone who wants things that separate should IMO put them in different files.
Comment blocks for auto-documentation (Javadoc, Doxygen, Pydoc and the like) also provide visual separation - more so than a few lines of whitespace, and more productively than any amount of whitespace.
When you're working in a team, copy the style of any file you modify. Don't reformat it to match your own preferences, don't add new code with your preferred formatting. Either agree a house style or just live with it.
I think it's a matter of personal preference or maybe of company standard, but here in the office, even if we have some guidelines, everyone has his way of writing code, and whitespace is one of the differences.
I prefer to use a lot of blank lines. Every time the context of my code changes I separate this part from the previous and next, and comment the whole block before.
It depends on your own preference or your company's guidelines where you must create carriage returns. In fact, such a guidline will also determine where you place spaces, tabs and how you breathe. Well, maybe not the last.
If you are on your own though and you want to be concise in your manner of coding, you could always consider implementing Microsoft StyleCop. It keeps your coding habits under control and you can configure it thus to be as stern as you like.
Like everyone has indicated each person has his own approach. In my case there are a few things I like to do consistently.
Blank lines before and after comments that are more than one line.
Each class gets at least two blank lines before and after.
Functions are separated by one line.
Other blank lines are more optional. The thing here is that I like comments and classes to stand out. Usually proper indentation will take care of most other needs. The trick here is to never waste vertical space unless you have to.
If you have to work with the code of others I think you would be delighted if they followed the same white space schedule. Getting lost in function or class definitions can be a big hassle.
This is probably a matter of preference or house style.
I include blank lines between functions, and between logical blocks in larger function calls.
code standards for me is:
- After each if / else statement end (The last
}
) - After each method
- After each property the consists of multiple lines
- After a collection of field
example:
string a = "Note: ", b= "This", c= "A", d= "String", e= "Test";
int f=1, h=1;
string A { Get; Set; }
string B { Get; Set; }
string C { Get; Set; }
string D
{
Get
{
Return this.d;
}
Set
{
if (value == "Foo")
{
// DoSomething
}
this.d=value;
}
}
//empty line
Check out StyleCop for source code layout: http://stylecop.codeplex.com/.
I'd also recommend FxCop or ReSharper.
Also, Control K the D layouts the document but doesn't sort out blank lines?
If a function is so complex that we are thinking to put blank lines to make it more comprehensible, it probably requires some refactoring, more than blank lines.
i.e, consider this simplified php snippet:
function doStuff(){
$a = new A();
$result = $a->foo();
$b = new B();
$result += $b->bar();
return $result;
}
Would be better in my opinion as:
function doStuff(){
$result = $this->processA();
$result += $this->processB();
return $result;
}
In this way you still improve readability without wasting space in your files.
I work often with my laptop, and having to scroll a lot inside the files is quite annoying. If the code can be clean and avoid unnecessary blank lines, it's quicker to develop and understand.
精彩评论