for (...) {
/* This is a multiline for-loop */
if (...) {
/* This is a multiline if-statement */
}
}
for (...)
{
/* This is a multiline for-loop */
if (...)
{
/* This is a multiline if-statement */
}
}
Not in my opinion.jronald said:But the latter is more clear, isn't it? It's symmetric.
I use the former almost exclusively, unless the client requires something else. I never thought of it as being Unixy.jronald said:But the latter is more clear, isn't it? It's symmetric.
I agree that code style is a personal choice. Although there can be different styles in one project, they are localized (in files), localizing is great. It's ok to read code in different styles, and think that it will help to improve the code style, and different styles for different codes sometimes. The problem is that almost all code are in one style nowadays.phoenix said:Code style is a personal decision. Use whatever style you want for your own code. If your projects become big and public, then you can impose whatever style guidelines you want.
So long as you are willing to adopt the coding style of whatever other projects you want to contribute code to.There's nothing worse than someone coming out of the woodwork with a single patch saying, "This is the one try style, bow down before it, and re-write all of the exising code to match this one patch."
![]()
In this view, you are right.gilinko said:I also use the former variant, mainly because the preferred coding standard by both Sun Java Code Conventions and for the Drupal CMS system Drupal Coding Standard.
However, my main reason for the user of the former is that it cuts down on the scrolling up and down the source code due to the "extra" lines. In the simple example there is two extra lines, and when the if/else/try/whachamacallit grows in numbers so does the length of the file in the editor.
void foo() {
...
if (bar) {
...
}
...
}
// But
void baz(int lots, int of, int parameters, int so, int much,
int it, int does, int not_fit, int on, int just,
int one, int line)
{
...
if (really && really && long
&& ugly && condition)
{
...
}
...
}
def foo():
...
if bar:
...
...
jronald said:In this view, you are right.
In another view, after proper abstracting, a function should not be that large in most cases.
If a funtion is large, it can use the former style, or even a more compacted one.
So every function can have a style that suits.
while (true)
{ if (whatever = something)
{ do_something;
}
else
{ if (stuff = boring)
{ do_something_else
lots_of_somethings
}
}
}
phoenix said:Personally, I prefer a hybrid:
Code:while (true) { if (whatever = something) { do_something; } else { if (stuff = boring) { do_something_else lots_of_somethings } } }
Keeps the braces lined up so you can visually see the groupings, but doesn't waste as much space as putting them on their own lines. And, depending on the text editor, I may indent the braces.
However, this is just my preferred indentation/brace-style, and not the basis for my religion.![]()
package qxp;
use strict;
sub H($){pack'H*',$_[0]}
sub by{map{[splice@_,1,$_[0]]}!($#_%$_[0])..$#_/$_[0]}
sub is($){$_ eq$_[0]}
sub cat{join'',@_}
sub list{@_}
sub wa{wantarray?@_:cat@_}
sub yuki(&@){shift->();@_}
sub fopen(;$){open my$z,$_[0]||$_;$z}{my %file;
sub file($){readline($file{$_[0]}||=fopen$_[0])}}
sub slurp(;$){wa map{yuki{close$_}<$_>}fopen$_[0]}
#sub spew($;$){sub{map{print $_ $_[1];close$_}fopen$_[0]}->($_[0],$#_?$_[1]:$_)}
sub spew($;$){open my$z,">$_[0]";print $z $#_?$_[1]:$_;close $z}
sub table{map{chomp;[split/\t/]}@_}
sub trim{map{s/^\s+|\s+$//g}@_?@_:$_}
sub count{my%z;$z{$_}++for@_;\%z}
sub uniq{keys%{count@_}}
sub copy{wa local@_=@_}
sub find(&@){no strict 'refs';local(*{(caller).'::a'})=\(my$a=$_[1]);&{$_[0]}||($a=$_)for(@_[2..$#_]);$a}
sub first(&@){$_[0]->()&&return$_ for@_[1..$#_]}
sub min{find{$a<$_}@_}
sub max{find{$a>$_}@_}
sub section{map{$_->[$_[0]]}@_[1..$#_]}
*#=sub{map$#$_,@_};
sub zip{map section($_,@_),0..max &#(@_)}
sub char{substr$#_?shift:$_,$_[0],1}
sub via(&$){local $_=$_[1];$_[0]->()}
*\=sub{map[section($_,@_)],0..max &#(@_)};
{my%ops;*?=sub{ref$_[-1]?map&?($_[0],@$_),&\(@_[1..$#_]):wa do{my@z=$_[1];@z=$ops{$_[0]}->($_,@z)for@_[2..$#_];@z}};
{no strict 'refs';(*$_,$ops{$_})=eval"sub{&?('$_',\@_)},sub{wa(\@_[1..\$#_]) $_ \$_[0]}" for qw'** =~ !~ * / % x + - . << >> < > <= >= lt gt
le ge == != <=> eq ne cmp & | ^ && || .. ... = , => and or xor'}}
my%refs=(''=>'_',map{$_=>lc substr $_,0,1}qw{SCALAR ARRAY HASH CODE REF GLOB LVALUE});
sub refs{wa map{$refs{ref $_}||'o'}@_}
sub _{cat map{
is '' ? $_:
is 'a' ? @{$_[0]}:
()
}refs@_}
sub wday{(int(365.25*($_[2]-($_[1]<3)))+int(30.6*(1+$_[1]+12*($_[1]<3)))+$_[0]-621050)%7+1}
sub leap{$_[0]%4?0:$_[0]%100?1:$_[0]%400?0:1}
sub days{(31,(28+leap $_[1]),(31,30,31,30,31)x2)[$_[0]-1]}
sub cal{by 7,((undef)x(wday(1,@_)-1),1..days @_)}
sub all{@_==grep$_,@_}
sub one{1==grep$_,@_}
sub none{!grep$_,@_}
sub any{!&none}
sub import{no strict 'refs';*{caller(0)."::$_"}=${"$_[0]::"}{$_} for grep !/^(BEGIN|__ANON__|a|import)$/, keys%{"$_[0]::"}}
package www;
use strict;
use qxp;
use config;
use L2;
#use encoding 'utf8';
my $utf8=0;
sub import{no strict 'refs';do{*{caller(0)."::$_"}=${"$_[0]::"}{$_}} for qw'template html depercent';} # out! out!
my %template;
sub template;
#require tags;
*template=templator{copy $template{$_[0]}||=slurp "$path::templates/$_[0].htm"}
# '<#','#>' => sub{package tags;eval$_[0]},
'${','}' => sub{$_[1]{$_[0]}},
'<@','@>' => sub{template $_[0]},
'<^','^>' => sub{$^{$_[0]}}
;
sub html;
*html=subst
'&'=>'&',
'<'=>'<'
;
sub depercent{map{s/%([\dA-Fa-f]{2})/pack 'C',hex $1/ge;utf8::decode $_ if $utf8;$_}@_}
sub ok{print template 'index'}
sub rf{
$_[0]{url}||=$ENV{HTTP_REFERER};
$_[0]{time}||=0;
print template 'refresh',$_[0];
}
$ENV{PERL}=$];
$ENV{URL}=s,^/,,?[grep!/\W/,split'/']:[] for copy $ENV{REDIRECT_URL};
$ENV{QUERY}={depercent split/&|=/,$ENV{QUERY_STRING}};
$ENV{POST}={depercent split/&|=/,do{read STDIN, my $x, $ENV{CONTENT_LENGTH};$x}} if $ENV{REQUEST_METHOD} eq 'POST' and $ENV{CONTENT_TYPE} =~
'^application/';
$ENV{COOKIE}={split/; |=/,$ENV{HTTP_COOKIE}};
666;
expl said:Where you place brackets does not matter much, what makes pain in my brain most of time are people using several spaces instead of a single tab when I need to patch or copy code between tabbed and spaced code I end up spending a lot of time converting spaces to tabs or vice versa.