Forum Moderators: coopster

Message Too Old, No Replies

php if without {braces}?

is it a good practice..?

         

achshar

4:11 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



hello everybody..
i was about to give my site a code update to make it lighter and faster..

i typically use alot of ifs and many of them are single liners like

if(blah) {$foo=1}
else {$foo=0}

which could be written as

if(blah) $foo=1
else $foo=0

and works correctly.. but i want to know is it a good practice to gave single liners written like that.
also which one would be faster? if else or switch case?

Frank_Rizzo

4:30 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I doubt if there is anything in it speedwise - unless you are performing billions of calculations a time.

Smaller in size would be this:

$foo=0;
if(blah)$foo=1;

achshar

4:36 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



hmm just as i thought.. i just needed some opinion.. :)

Smaller in size would be this:
$foo=0;
if(blah)$foo=1;


hey thanks for pointing that out :) you saved some more bytes for me..

rocknbil

5:07 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$foo = ($blah)?1:0;

Now that's a one liner. :-)

It's generally not good practice - IMO - for unbracketed code with the exception of the ternary short circuit evaluation above, but it's a matter of preference. A switch is only for long lists of variable conditions, I'd say, more than 3 or 4. Speed, doubt it makes a whole lot of difference.

Matthew1980

5:27 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi all,

$foo = ($blah)?1:0;

Now that's a one liner. :-)


I seem to remember Rocknbil calling the ternary operator "your new best friend" somewhere along the line ;)

I completely agree with 'opting out' of not using curly braces (by which i mean I use them), it makes the code easier to segment and read & structure if you are using a form of indentation in your IDE/text editor.

Where I can I use switch statements, unless I need something that I can be more precise on conditions & their evaluation. I also parenthesise where I can too, it break's up the code into easier to read bundles, so when you come back to it weeks later, it's not too confusing :)

All of these 'styles' as Rocknbil points out are purely down to preference & what the project demands, and how efficient you are striving to be.

Cheers,
MRb

achshar

5:33 pm on Jun 25, 2010 (gmt 0)

10+ Year Member



hmmm i dont have enormous code files..
for a file with size 12kb, a minus of a single kb would mater.. :)

it break's up the code into easier to read bundles, so when you come back to it weeks later, it's not too confusing :)

agreed.. those braces do help you 'grasping' the code when you come back again after some time.. plus it i a great way to find code errors in an editor such as Dreamweaver..
it cannot, on the other hand, recognize errors with multiple lines of codes in absence of braces

Matthew1980

6:03 pm on Jun 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there achshar,

<off topic>
Ack! You used the D word ;) Again, the IDE/text coding environment is different for everyone, I personally can't stand over-blown IDE's

The only real difference as you would see is if you had chunks of code using high-overheads & complex, reused code. This is where functions come into play, and classes to a degree, especially if you are after a efficient script. I tend to get it working first, then refine after - though I admit that's not the best way, but it's how I have always been - "old dogs, new tricks" etc ;)

Happy streamlining anyway :)

Cheers,
MRb

coopster

10:07 am on Jun 26, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I never use loose syntax and the primary reason being code compression. Code compressors, in my experience, handle the job best when they find full syntax. I've set my editor to autocomplete my control structures.

SteveWh

12:48 pm on Jun 27, 2010 (gmt 0)

10+ Year Member



The braces are more necessary in PHP than in C++. If you remove braces, be careful if you go back to edit code later. For example, if you remove braces to get this:

if(something)
do_something();

Then decide to add a comment:

if(something)
// comment about do_something
do_something();

...that's fine in C++, but I did it once in PHP and do_something() never got done. Fixed it with:

if(something)
{
// comment about do_something
do_something();
}

So for my own purposes I wouldn't bother removing already-existing braces because there's half a chance I'll want to add additional code or a comment later, and have to waste more time putting them back in.

Readie

3:41 pm on Jun 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's good practice to include them anyway in my opinion, if only so you're in the habit of using them - so you don't have an error when you do need them.