|
|
|
|
Thread title: do NOT use {} for single if-else statements.. |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
02-18-2007, 11:58 PM
|
#11
|
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
Posts: 45
|
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
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
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
|
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
Posts: 45
|
//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
|
Status: Member
Join date: Mar 2006
Location: Canada
Expertise:
Software:
Posts: 286
|
I always add braces to my if statements for the following two reasons: - Readability (as Salathe mentioned)
- 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
|
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
Posts: 2,246
|
Originally Posted by CreativeLogic
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
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
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
|
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
Posts: 1
|
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
|
Status: I love this place
Join date: Apr 2005
Location:
Expertise:
Software:
Posts: 721
|
Originally Posted by CreativeLogic
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
|
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
Posts: 5,200
|
Originally Posted by Cole
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
|
Status: I love this place
Join date: Apr 2005
Location:
Expertise:
Software:
Posts: 721
|
Originally Posted by Andrew R
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.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|