473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

am I too wired? On coding style

Hello. I have a question that spining around my head for a long time. I
prefer to make this kind of if statement:

if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);

But almost all the 'good' php code I saw write it this way:

if (!$GLOBALS['phpgw_info']['user']['admin'])
{
Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
}

the argument is the latter is more readable and easy to understand. But,
to me, a if statement immediately followed by another statement or
function call is more readable and easy to understand. It saved me from
thinking what's after this statement or function call. Truly, I think
keeping a stack of 'if' or 'for' in the brain is difficult for me, I
tends to remove the {} and add the statement after if-clause when if
statement contain only one statement.

I am just thinking if my way of keeping code logic in brain is too
wired. If most people truly consider the {} more easier to
read/understand, I'll force myself write understandable code; if there
are only very small difference in understanding (considering reading
10000 line of source, even small difficulty counts), I would like to
keep my style.

Jul 17 '05 #1
5 1383
Zhang Weiwu wrote:
Hello. I have a question that spining around my head for a long time. I
prefer to make this kind of if statement:

if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);

But almost all the 'good' php code I saw write it this way:

if (!$GLOBALS['phpgw_info']['user']['admin'])
{
Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
}

the argument is the latter is more readable and easy to understand. But,
to me, a if statement immediately followed by another statement or
function call is more readable and easy to understand. It saved me from
thinking what's after this statement or function call. Truly, I think
keeping a stack of 'if' or 'for' in the brain is difficult for me, I
tends to remove the {} and add the statement after if-clause when if
statement contain only one statement.

I am just thinking if my way of keeping code logic in brain is too
wired. If most people truly consider the {} more easier to
read/understand, I'll force myself write understandable code; if there
are only very small difference in understanding (considering reading
10000 line of source, even small difficulty counts), I would like to
keep my style.

Its simply a matter of style. If its your own code use whatever format
you are comfortable with. If you are producing commercial code then go
with the style that the organisation uses.
Jul 17 '05 #2
Zhang Weiwu wrote:
Hello. I have a question that spining around my head for a long time.
I prefer to make this kind of if statement:

if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);

But almost all the 'good' php code I saw write it this way:

if (!$GLOBALS['phpgw_info']['user']['admin'])
{
Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
}

the argument is the latter is more readable and easy to understand.
Nah, the best argument is: if you use {} you can easily add debug
statements.

I would write it like:

if ( !$GLOBALS[ 'phpgw_info' ][ 'user' ][ 'admin' ] ) {

Header(

'Location: ' .
$GLOBALS[ 'phpgw' ]->link(

'/',
'menuaction=forum.uiforum.index'
)
);
}
But,
to me, a if statement immediately followed by another statement or
function call is more readable and easy to understand.
Then you'll have a hard time reading code. I guess that PHP can do the
following, which I even consider more clear:

$GLOBALS[ 'phpgw_info' ][ 'user' ][ 'admin' ] or
Header(

'Location: ' .
$GLOBALS[ 'phpgw' ]->link(

'/',
'menuaction=forum.uiforum.index'
)
);

so $GLOBALS bla bla OR else do Header ...

Which I sometimes indeed write as:

$GLOBALS[ 'phpgw_info' ][ 'user' ][ 'admin' ] or Header(

'Location: ' .
$GLOBALS[ 'phpgw' ]->link(

'/',
'menuaction=forum.uiforum.index'
)
);

if I am 100% sure it doesn't need debugging statements. I read the part
before the or as an assertion. It must be valid to continue, or else do
the or part to "fix" the problem. In my Perl code I do things like:

defined $variable or $variable = 'default value';

Which I read: the variable must be defined, or else, give it a defined
value.

( sometimes you see this written as: $variable ||= 'default value',
which can be wrong in special cases).
It saved me from
thinking what's after this statement or function call. Truly, I think
keeping a stack of 'if' or 'for' in the brain is difficult for me, I
tends to remove the {} and add the statement after if-clause when if
statement contain only one statement.


Yup, and then you have to put the {} back when you want to debug a piece
of code.

--
John MexIT: http://johnbokma.com/mexit/
personal page: http://johnbokma.com/
Experienced programmer available: http://castleamber.com/
Happy Customers: http://castleamber.com/testimonials.html
Jul 17 '05 #3
"Zhang Weiwu" <zh********@realss.com> wrote in message
news:q5************@arneg.hackerdom.org...
Hello. I have a question that spining around my head for a long time. I
prefer to make this kind of if statement:

if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);

But almost all the 'good' php code I saw write it this way:

if (!$GLOBALS['phpgw_info']['user']['admin'])
{
Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
}

the argument is the latter is more readable and easy to understand. But,
to me, a if statement immediately followed by another statement or
function call is more readable and easy to understand. It saved me from
thinking what's after this statement or function call. Truly, I think
keeping a stack of 'if' or 'for' in the brain is difficult for me, I
tends to remove the {} and add the statement after if-clause when if
statement contain only one statement.

I am just thinking if my way of keeping code logic in brain is too
wired. If most people truly consider the {} more easier to
read/understand, I'll force myself write understandable code; if there
are only very small difference in understanding (considering reading
10000 line of source, even small difficulty counts), I would like to
keep my style.


I like this better:

$GLOBALS['phpgw_info']['user']['admin'] or HeAdEr(
'Location: '.$GLOBALS['phpgw']->lInK('/',
'menuaction=forum.uiforum.index'));

Actually, I don't :-)
Jul 17 '05 #4
Chung Leong wrote:
"Zhang Weiwu" <zh********@realss.com> wrote in message
news:q5************@arneg.hackerdom.org...
Hello. I have a question that spining around my head for a long time. I
prefer to make this kind of if statement:

if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);

But almost all the 'good' php code I saw write it this way:

if (!$GLOBALS['phpgw_info']['user']['admin'])
{
Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
}

the argument is the latter is more readable and easy to understand. But,
to me, a if statement immediately followed by another statement or
function call is more readable and easy to understand. It saved me from
thinking what's after this statement or function call. Truly, I think
keeping a stack of 'if' or 'for' in the brain is difficult for me, I
tends to remove the {} and add the statement after if-clause when if
statement contain only one statement.

I am just thinking if my way of keeping code logic in brain is too
wired. If most people truly consider the {} more easier to
read/understand, I'll force myself write understandable code; if there
are only very small difference in understanding (considering reading
10000 line of source, even small difficulty counts), I would like to
keep my style.

I like this better:

$GLOBALS['phpgw_info']['user']['admin'] or HeAdEr(
'Location: '.$GLOBALS['phpgw']->lInK('/',
'menuaction=forum.uiforum.index'));

Actually, I don't :-)

Readability is more important than brevity, Revisiting code that was
written months ago is always a problem whether in ones own code space or
a commercial environment. I suppose the phrase "pleas leave this code in
the state you would like to find it" just about sums the argument up <g>
Jul 17 '05 #5
In: <q5************@arneg.hackerdom.org>, Zhang Weiwu <zh********@realss.com> wrote:
Hello. I have a question that spining around my head for a long time. I
prefer to make this kind of if statement:

if (!$GLOBALS['phpgw_info']['user']['admin']) Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
Personally, I hate when people do that. I like the { }, I just
find it easier to see what statement goes with what. (Particularly
if it's followed by a statement that spans multiple lines)

But almost all the 'good' php code I saw write it this way: if (!$GLOBALS['phpgw_info']['user']['admin'])
{
Header(
'Location: '.$GLOBALS['phpgw']->
link('/', 'menuaction=forum.uiforum.index')
);
}
I don't like that either. (I must be a grump!) Here is what I like:

if($condition) { //1-liner }

if($condition) {
// multi
// line
// code.
}

Some folks really hate my style too. Sometimes I'll break $condition
across multiple lines, but only if it can't easily fit on 1 line.

Heh.. I think light ought to be spelled 'lite' too. :-)
10000 line of source, even small difficulty counts), I would like to
keep my style.


$works_for_you ? keepYourStyle():change();

Jamie
--
http://www.geniegate.com Custom web programming
gu******@lnubb.pbz (rot13) User Management Solutions
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jocab | last post by:
Just had a look at the Wired.com site and it seems it's completely in XHTML and CSS. I don't understand this snippet of code on their homepage, though: <link rel="stylesheet" type="text/css"...
18
by: craig | last post by:
I am curious about how many of you prefer style 1 vs. style 2, and why. Are there names for these style? style 1: method { }
144
by: Natt Serrasalmus | last post by:
After years of operating without any coding standards whatsoever, the company that I recently started working for has decided that it might be a good idea to have some. I'm involved in this...
4
by: Dotnetjunky | last post by:
Hi, So far, I've found tons of documents describing recommended coding standards for C#, but not a single piece on VB.NET yet. Anybody here knows such a coding standards guideline on VB.NET...
4
by: Mike Labosh | last post by:
I realize that you people have not seen much of me other than some framework responses I have posted. I am primarily a VB guy (yes, you can laugh) But I have lurked here for several years,...
13
by: benben | last post by:
Is there an effort to unify the c++ coding standard? Especially identifier naming. Not a big issue but it would be annoying to have to incorporate different coding styles simultaneously when...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
1
by: Jim Johnson | last post by:
is this C style coding? I don't seem to see much C++ code in this way. is this a bad programming practice? code seem ugly coding this way. =================
7
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.