Jump to content

quick question


bennyboywonder

Recommended Posts

I just thought I would check something.

 

Whenever I write an IF (or other control structure) if the resulting code is only one line, I tend not to bother with the curly braces, as it seems to make no difference as to how the code is executed. I have recently seen some examples written and noticed that these people have put the curly braces in regardless. Does this matter?

e.g. is

if(myval == 1) alert("myval is 1");

exactly the same as

if(myval == 1) { alert("myval is 1"); }

cos it certainly seems to run the same. Or is this just a conventions thing?

Link to comment
https://forums.phpfreaks.com/topic/41112-quick-question/
Share on other sites

I just thought I would check something.

 

Whenever I write an IF (or other control structure) if the resulting code is only one line, I tend not to bother with the curly braces, as it seems to make no difference as to how the code is executed. I have recently seen some examples written and noticed that these people have put the curly braces in regardless. Does this matter?

e.g. is

if(myval == 1) alert("myval is 1");

exactly the same as

if(myval == 1) { alert("myval is 1"); }

cos it certainly seems to run the same. Or is this just a conventions thing?

 

i think it is just a conventions thing, i can understand it if you had to execute an entire block of code, but for just calling a function i would actually leave out eh braces.

Link to comment
https://forums.phpfreaks.com/topic/41112-quick-question/#findComment-199139
Share on other sites

why?

Because it's lazy, and prone to errors... the worst is when people put it on another line, then add another statement, and don't realize that the 2nd statement will always run, becase it's not covered by the if().  Always use the braces -- JS, unlike Perl, does not have a true inline if, so you shouldn't code like it does.

Link to comment
https://forums.phpfreaks.com/topic/41112-quick-question/#findComment-199888
Share on other sites

why?

Because it's lazy, and prone to errors... the worst is when people put it on another line, then add another statement, and don't realize that the 2nd statement will always run, becase it's not covered by the if().  Always use the braces -- JS, unlike Perl, does not have a true inline if, so you shouldn't code like it does.

 

but assuming I don't make what sounds like an *extremely stupid* mistake, then, it doesn't make any difference?

Link to comment
https://forums.phpfreaks.com/topic/41112-quick-question/#findComment-200330
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.