Source code formatting

B

Bill H

I have noticed in most perl code (and c++ code also) that when the
programmer use an if statement they usually format it like this:

if ($a == $b){
print "equal";
}

When I code I would write it this way:

if ($a == $b)
{
print "equal";
}

Is there any benefit in programming it the 1st way versus the second
way? I use the 2nd way cause it is easy to match up the { & } when
looking at the code (they are always in the same column).

Bill H www.ts1000.us
 
A

A. Sinan Unur

I have noticed in most perl code (and c++ code also) that when the
programmer use an if statement they usually format it like this:

if ($a == $b){
print "equal";
}

When I code I would write it this way:

if ($a == $b)
{
print "equal";
}

Is there any benefit in programming it the 1st way versus the second
way? I use the 2nd way cause it is easy to match up the { & } when
looking at the code (they are always in the same column).

The first is called K&R style. Some people like that, some people like
yours. I prefer to be able to fit as many meaningful lines as possible
in the limited vertical space I have. On my office machine with
1600x1200 resolution, I can just about see enough lines to have a good
feel of the code.

However, style is always a personal choice. You might enjoy reading

perldoc perlstyle
 
D

Dr.Ruud

Bill H schreef:
I have noticed in most perl code (and c++ code also) that when the
programmer use an if statement they usually format it like this:

if ($a == $b){
print "equal";
}

I would always put a space before the {. But rather I would code it like

print 'equal' if ($a == $b);

using single quotes.

When I code I would write it this way:

if ($a == $b)
{
print "equal";
}

That is 5 lines, where only 2 are needed.
(incl. 1 blank line)

Is there any benefit in programming it the 1st way versus the second
way? I use the 2nd way cause it is easy to match up the { & } when
looking at the code (they are always in the same column).

I read the 'if' as the lexical start of the block.


There are many styles:

if ($a == $b) {
print 'equal'
} else {
print 'not equal'
}


if ($a == $b) {
print 'equal'
}
else {
print 'not equal'
}


if ($a == $b)
{
print 'equal';
}
else
{
print 'not equal';
}


print ( $a == $b ? 'equal' : 'not equal' );


print ( ( $a != $b ? 'not ' : '') . 'equal' );
 
P

Pickleman

Writing code like this:
if ($a == $b)
{
print 'equal';
}

is called BSD style. People who use BSD style claim it helps them see
where the block starts or ends. But if you indent every block by 4
columns, as recommended by most people, then it's just as easy--you
scroll up to see where it ends, but looking for a block keyword, not a
{. You should also always write code like the second example above,
like:

if ($a == $b) {
print 'equal';
}
else {
print 'not equal';
}

because "cuddling" the else, like } else {, makes it harder to tell, at
the end of an else or elsif block, where it begins. You're looking for
a keyword in the same column, but instead you see a '}'. In Perl Best
Practices, Damian Conway recommends this for the exact same reasons.
 
X

Xicheng

Bill said:
I have noticed in most perl code (and c++ code also) that when the
programmer use an if statement they usually format it like this:

if ($a == $b){
print "equal";
}

When I code I would write it this way:

if ($a == $b)
{
print "equal";
}

Is there any benefit in programming it the 1st way versus the second
way? I use the 2nd way cause it is easy to match up the { & } when
looking at the code (they are always in the same column).

you may also try the following commands to see how some Perl modules
handle these styles:
perl -MO=Deparse,-sC my_code.pl
perl -MO=Deparse,-s my_code.pl
the option 'C' behind '-s' is used for cuddling "elsif", "else", and
"continue" blocks.
you may check out: "perldoc B::Deparse" for more information.

Xicheng
 
B

Bill H

Pickleman said:
Writing code like this:
if ($a == $b)
{
print 'equal';
}

is called BSD style. People who use BSD style claim it helps them see
where the block starts or ends. But if you indent every block by 4
columns, as recommended by most people, then it's just as easy--you
scroll up to see where it ends, but looking for a block keyword, not a
{. You should also always write code like the second example above,
like:

if ($a == $b) {
print 'equal';
}
else {
print 'not equal';
}

because "cuddling" the else, like } else {, makes it harder to tell, at
the end of an else or elsif block, where it begins. You're looking for
a keyword in the same column, but instead you see a '}'. In Perl Best
Practices, Damian Conway recommends this for the exact same reasons.

Other than personal style, is there any benefit from using one over the
other. Is there a speed increase in Perl if you put your open brace on
the same line as the "if"? Is Perl eating all the leading white space
one character at a time to get to the actual code?

Bill H www.ts1000.us
 
A

Anno Siegel

Bill H said:
Other than personal style, is there any benefit from using one over the
other. Is there a speed increase in Perl if you put your open brace on
the same line as the "if"?
No.

Is Perl eating all the leading white space
one character at a time to get to the actual code?

Of course it does that at some point (at compile time). You won't be
able to tell the difference.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top