Do you use blank lines in your code?
programmers.stackexchange.com
programmers.stackexchange.com
edit: but seriously, yeah I use tonnes of whitespace and bucketloads of comments. No point making things harder than they need to be. Also commenting helps you solidify your understanding of the code.
I tend to write like this:
while(foo) {
code;
}
rather than while(foo)
{
code;
}
I use newlines to separate tasks in the code. Example: // do the foo check
if(foo){
removeFoo();
}
// now perform the bar operation
if(shouldDoBar) {
performBar();
}
// now return
return fooBar;I believe you will find it easier to read.
See: http://area51.stackexchange.com/proposals/3352 And: http://programmers.stackexchange.com/about
In my opinion, there are too many sites under this umbrella and when I have a questions I am not sure where it shall be posted anymore.
I definitely think it improves readability to break up these chunks of functionality. The problem, though, is I can't think of a good example that I don't think will have someone tell me to put them in two different methods...
I will note, however, that function call overhead still adds up. Qt4 on GNOME is significantly slower delivering events than it needs to be, as every single call stack is over 50 methods deep.
Here's a quick & dirty Java class file parser I have open in the other window, a representative sample of how I work. I can't imagine how such a thing could be written legibly liberal use of blank lines.
sub parse_class_file {
my($fn) = @_;
my ($name, $super);
# my $interfaces = []; # Not used yet
my $constant_pool = [];
my $fields = {}; # Referenced by name
my $methods = {}; # Referenced by name_description
open(C, "$fn") or die("Could not open '$fn': $!\n");
# Cafebabe
my $magic = read_field(\*C, "H*", 4);
# Class file format version
my $minor = read_field(\*C, "n");
my $major = read_field(\*C, "n");
# Load constant pool
load_constant_pool(\*C, $constant_pool);
# Class name, inheritance
my $access_flags = read_field(\*C, "n");
my $this_class_index = read_field(\*C, "n");
my $super_class_index = read_field(\*C, "n");
# Load interfaces -- just throw them away for right now
my $interface_count = read_field(\*C, "n");
for(my $i = 0; $i < $interface_count; $i++) { read_field(\*C, "n"); }
load_fields(\*C, $fields, $constant_pool);
load_methods(\*C, $methods, $constant_pool);
close(C);
return( { name => $constant_pool->[$this_class_index]->{name},
super => $constant_pool->[$super_class_index]->{name},
fields => $fields,
methods => $methods } );
} for (int i; i < 10; ++i) {
if (i % 3 == 0) {
continue;
}
array[i] += 2;
}
I use blank lines to separate declaration and other codes, and logical block within a function, and also between functions.It is no use for me to put blank lines between statements. It would better to view structure in one screen. Scrolling between pages make me tired too.
And ya, scrolling is the enemy.
On a related note though, there's a lot of code in my current work project, and my previous one, where people separate every function definition with a comment line like
//==========================================================
Some files even have two lines like that between every function. It drives me nuts, in a minor way. I don't feel like I've ever had a problem seeing where functions begin and end, so I don't understand the point of those comments.
We have a coding standard document, and there's nothing in there about doing this, yet there are several slightly different styles of it than people seem to have picked up independently. Is this a game industry quirk? Is it common elsewhere? When did it start, and WHY?
Emacs can be set up (with Cedet) to draw lines between functions without any comments needed (so no wasted space either). I've had that on before too just to see if it changed my mind, but no, I don't like that either. But at least if everyone's IDE would do that, it could be a personal preference.
I hadn't seen those specifically at other developers, but I had seen a row of slashes between functions at a couple of other PC developers. I don't recall seeing them with console folks, though.
And yeah, the instance where there are two lines of this are actually lines of slashes, the line of = is just one of several versions.
I do remember some old IDE (Metroworks on Mac? I barely used it) would recognize a particular comment style like this and create a kind of table of contents dropdown list, using these to break the list into sections. But putting them between every function would have removed the value in that too.
I never saw it in console work but that was all pre-Xbox. The Xbox was much, much nicer to program on than the GameCube or PS2 because it was all integrated into Developer Studio and fast instead of in some crazy flaky debugger. So a lot of places went to Xbox as their primary development console. As a result, I presume many recent console programmers are much less divergent from the PC-centric world anyway.
So when the code has an apparent structure, like the nested divs of HTML, I figure the lines are superfluous, as the fine structure is somewhat obvious, and I help myself with comments where it gets hairy. When the code is a big pile of CSS or javascript, without the obvious structure of HTML, a good spacing system really helps me keep tabs on where I am and quickly scroll to the section I'm looking for.
I did edit some HTML with edit.com in DOS but I switched to notepad after a couple of months.
Except in the case of CSS, in which I MUCH prefer 1-selector-per-line over 1-rule-per-line. My CSS lines often go above 160 chars.
- between groups of member variable declarations if any
- between function definitions if defining more than one
- after local variable definitions if any
- before a big ugly comment and probably within the big ugly comment, but the big ugly comment needs to be removed eventually, so no.
- and finally, before the ANSI "return 0;" in main
int i;
for (i; i < 10; ++i) {
if (i % 3)
array[i] += 2;
}
http://hoop-la.ca/software/cat/cat.cI once had to do maintenance on a Visual Basic project with zero blank lines and minimal comments "to save screen space," even though the developers had decent-sized LCDs.
The example
for (int i; i < 10; ++i)
{
if (i % 3 == 0) continue;
array[i] += 2;
}
Could be rewritten as: for (int i; i < 10; ++i)
{
if (i % 3 != 0){
array[i] += 2;
}
}