Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1339 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

do NOT use {} for single if-else statements..

Thread title: do NOT use {} for single if-else statements..
Closed Thread  
Page 2 of 3 < 1 2 3 >
    Thread tools Search this thread Display Modes  
02-18-2007, 11:58 PM
#11
Ali is offline Ali
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 45
iTrader: 0 / 0%
 

Ali is on a distinguished road

  Old

derek lapp - you are right ternary operator is better to use for single statments, and here is how to use it..

$myVar = condition ? if_condition_true : if_condition_false

thats much better approach, but more times I see people using brackets, and code looks childish..

cheers

02-19-2007, 12:40 AM
#12
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

This topic is based entirely on Ali's own personal opinion. It's not "bad" code use use the curly braces around single statements, it is just that Ali doesn't like that style.

The advantages and disadvantages of omitting the braces are, in my opinion, negligible and it's really just a matter of sticking with how you want to do it.

PHP Code:
// This?
if (true === $condition)
    
$is_true true;
else
    
$is_true false;

//or this?  OMG ONE MORE LINE!
if (true === $condition) {
    
$is_true true;
} else {
    
$is_true false;
}

// or this, with ternary operator
$is_true = (true === $condition) ? true false;

// or lazy boolean, with ternary
$is_true $condition true false
IMO, if it aids readability of the code then why on earth NOT use the curly braces?

02-19-2007, 12:48 AM
#13
Ali is offline Ali
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 45
iTrader: 0 / 0%
 

Ali is on a distinguished road

  Old

//or this? OMG ONE MORE LINE!

LOL - thats what I hate most. Remember the days when we used Pascal ? any Pascal users here BTW ?

02-19-2007, 12:58 AM
#14
DJAC is offline DJAC
DJAC's Avatar
Status: Member
Join date: Mar 2006
Location: Canada
Expertise:
Software:
 
Posts: 286
iTrader: 0 / 0%
 

DJAC is on a distinguished road

Send a message via MSN to DJAC

  Old

I always add braces to my if statements for the following two reasons:
  1. Readability (as Salathe mentioned)
  2. Decreases the probability of errors when adding code in the future.
In some languages, finding a missing { or } can be a HUGE headache. If you don't have the braces in the first place and you add a new line to your if statement, then there is a chance that you will forget to add the braces (which you would now need).

So just use { } and you'll never have to worry about forgetting them

02-19-2007, 01:04 AM
#15
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

Originally Posted by CreativeLogic View Post
Derek, your code won't even work, it will return a p**** error.
that's probably because of the <?=.

PHP Code:
echo $condition == 'true' "it's good baby" "it's bad"
works perfectly fine.

02-19-2007, 05:43 AM
#16
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

I find it better style to use { and } with single line ifs, you can indent it and make any possible debugging better. and should the if become 2 or more lines you are making it easier for yourself and less likely to have a future bug. This comes in more importance when you are making a really large project, simple code is easier to read, its just 2 lines.

02-21-2007, 01:36 PM
#17
Adrian.jc is offline Adrian.jc
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
 
Posts: 1
iTrader: 0 / 0%
 

Adrian.jc is on a distinguished road

  Old

I was just browsing and had to comment.

It doesn't really matter.
It may speed the script up by a few mili seconds, but that's it.

It's better to use { }, it's clearer, and better for nested statements.

Using the alternative Control Structure, ? : would be better than no { }

Adrian

02-21-2007, 11:26 PM
#18
Cole is offline Cole
Status: I love this place
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 721
iTrader: 0 / 0%
 

Cole is on a distinguished road

  Old

Originally Posted by CreativeLogic View Post
Derek, your code won't even work, it will return a p**** error.

Using the braces make no difference and actually make the code more readable in my eyes. I think that getting rid of them is actually a bad idea and will result in more issues. So really, it's not beginners that are doing this.
So what, he forgot to escape the quotation marks.

PHP Code:
<?= $==10 'it\'s 10' 'it\'s not 10'?>
And I generally use the ternary operator when the If statement is not complex.

02-21-2007, 11:32 PM
#19
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

Originally Posted by Cole View Post
So what, he forgot to escape the quotation marks.

PHP Code:
<?= $==10 'it\'s 10' 'it\'s not 10'?>
And I generally use the ternary operator when the If statement is not complex.
That still won't work. "$==10" < What's that?

02-21-2007, 11:49 PM
#20
Cole is offline Cole
Status: I love this place
Join date: Apr 2005
Location:
Expertise:
Software:
 
Posts: 721
iTrader: 0 / 0%
 

Cole is on a distinguished road

  Old

Originally Posted by Andrew R View Post
That still won't work. "$==10" < What's that?
That's just like filler I'm guessing, I'm guessing he's just saying this is a random variable, fill it in with whatever.

Closed Thread  
Page 2 of 3 < 1 2 3 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed