473,544 Members | 2,231 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=for um.uiforum.inde x')
);

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=for um.uiforum.inde x')
);
}

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 1392
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=for um.uiforum.inde x')
);

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=for um.uiforum.inde x')
);
}

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=for um.uiforum.inde x')
);

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=for um.uiforum.inde x')
);
}

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=for um.uiforum.inde x'
)
);
}
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=for um.uiforum.inde x'
)
);

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=for um.uiforum.inde x'
)
);

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********@rea lss.com> wrote in message
news:q5******** ****@arneg.hack erdom.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=for um.uiforum.inde x')
);

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=for um.uiforum.inde x')
);
}

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=for um.uiforum.inde x'));

Actually, I don't :-)
Jul 17 '05 #4
Chung Leong wrote:
"Zhang Weiwu" <zh********@rea lss.com> wrote in message
news:q5******** ****@arneg.hack erdom.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=for um.uiforum.inde x')
);

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=for um.uiforum.inde x')
);
}

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=for um.uiforum.inde x'));

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.hackerdo m.org>, Zhang Weiwu <zh********@rea lss.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=for um.uiforum.inde x')
);
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=for um.uiforum.inde x')
);
}
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
3011
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" media="screen" href="http://a1112.g.akamai.net/7/1112/492/2002091447/www.wired.com/news/v/20020914/css/cs4/wnScreen.css" /> <link rel="stylesheet"...
18
2529
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
6743
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 initiative. Typically I find that coding standards are written by some guy in the company who has a way of coding that he likes and then tries to force...
4
5321
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 and minds sharing with us ? Thanks in advance.
4
1501
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, picking up little crumbs of C# wisdom. Perhaps it's just my style, but what's this annoying behavior of the IDE and how do I stop it: C# seems to...
13
2035
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 using more than one library. The standard library seems to have everything lower-cased while a lot of other libraries do their own way. Ben
7
4932
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 developed significant content for the C programming language that is available at: https://www.securecoding.cert.org/ by clicking on the "CERT C...
1
1555
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
18428
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
0
7438
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7783
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7720
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5309
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4930
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3430
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1850
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
996
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.