|
|
|
|
Thread title: do NOT use {} for single if-else statements.. |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
02-18-2007, 11:42 PM
|
#1
|
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
Posts: 1,078
|
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.
|
|
02-19-2007, 01:04 AM
|
#2
|
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-21-2007, 11:26 PM
|
#3
|
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
|
#4
|
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
|
#5
|
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.
|
|
02-18-2007, 11:43 PM
|
#6
|
Status: Senior Member
Join date: Aug 2005
Location: New York
Expertise:
Software:
Posts: 825
|
Using brackets isn't a 'wrong' way of doing it. It's arbitrary...
|
|
02-18-2007, 11:58 PM
|
#7
|
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
|
#8
|
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
|
#9
|
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
|
#10
|
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
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|