This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 233027 - "if-else Statements Must Use Braces" Error
Summary: "if-else Statements Must Use Braces" Error
Status: REOPENED
Alias: None
Product: php
Classification: Unclassified
Component: Editor (show other bugs)
Version: 7.4
Hardware: All All
: P3 normal (vote)
Assignee: Ondrej Brejla
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-19 03:07 UTC by kaijia
Modified: 2016-12-24 02:19 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
A example case when the warning shows (7.76 KB, image/png)
2013-07-19 03:07 UTC, kaijia
Details

Note You need to log in before you can comment on or make changes to this bug.
Description kaijia 2013-07-19 03:07:19 UTC
Created attachment 137409 [details]
A example case when the warning shows

In a PHP functions, NB will notice that an if statement with only one line to do need braces, like:

if(empty($password))
    die();

This will be a yellow line under the second line, and saying "if-else Statements Must Use Braces".

But if you change it to:

if(empty($password))
{
    die();
}

There's no warning message.

This error occurs on NB 7.4 Beta (not tested on 7.3), but it works fine on NB 7.2.1.
Comment 1 Ondrej Brejla 2013-07-19 07:16:20 UTC
And it's ok, because you change it to a proper format...if statement with braces, so hint disappeared :) It's a new hint and you can switch it off in options -> editor -> hints -> php.
Comment 2 kaijia 2013-07-19 07:26:11 UTC
(In reply to comment #1)
> And it's ok, because you change it to a proper format...if statement with
> braces, so hint disappeared :) It's a new hint and you can switch it off in
> options -> editor -> hints -> php.

Ops, now I see. But I think the former one is also a proper format too. Check out the PHP Manual (http://www.php.net/manual/en/control-structures.if.php) and it use:

<?php
if ($a > $b)
  echo "a is bigger than b";
?>

as an example.
Comment 3 Ondrej Brejla 2013-07-19 07:28:16 UTC
Yes, you can use it...but we suggest to don't use this type of conditions. It's more readable with brackets. It's the same behavior as is in java editor...
Comment 4 xDaizu 2015-02-13 11:30:10 UTC
(In reply to Ondrej Brejla from comment #3)
> Yes, you can use it...but we suggest to don't use this type of conditions.
> It's more readable with brackets. It's the same behavior as is in java
> editor...

Even though I agree it is best practice to always use braces (for many reasons I'm don't want to focus on arguing) I REALLY think it should be rephrased to 

"if-else Statements Should Use Braces"

My students where really confused about the phrasing of this warning, when they have seen code (not by me) without braces and they dont understand how it can work if if-else MUST (mandatorily, obligatorily, by definition) use braces. Must is must, should is should. Lets user proper words :)
Comment 5 xDaizu 2015-02-13 12:52:08 UTC
I come back to comment that actually I'm back to add that a work mate pointed out that some platforms, like Prestashop, have in their coding standard the OBLIGATION of NOT using curly braces in one-statement blocks (you can read one of many discussion about the topic here: http://forge.prestashop.com/browse/PSCFV-2044 

I don't agree with this so-called "standard", of course, but you all will agree that Prestashop is not a small thing to just ignore, so I think this is another reason to take into account towards de "Should" instead of "Must", because "must" is an obligation, if you do what you must do, they won't accept your code. And they must accept my code! Don't put me in that situation! (xDD)

Now seriously, it's best practice. A really good one, but not an obligation, so even if it's marked as warning and not error, I really think you should rephrase that :)
Comment 6 Crazy-P 2016-12-24 02:19:19 UTC
This bug still exists in 8.2
"Enforcing" this warning to push people to use braces ain't a good practice due many reasons. And I believe there should be an options for "accept one-liners or not" as per project basis.