1 (edited by Paul 2006-05-28 17:22)

Topic: No { and } in code after if statement

Hello.

I'm discovering the code of punbb and I've just installed a mod.

It seems that the code would be much better if all "if" statements had a '{' after even if there is only one line after and then a "else" statement.

I know it is not a bug but this could:
1) enhance code readability
2) mod insertion (for example, if you need to add a line for a mod after a "if" statement and want to keep the original as a comment, then it is much better to have a '{' before and '}' after, isn't it ?)

I have been working as a programmer for more than 10 years and I can tell you that this is absolutely essential.

Also, please note that in most programming guides or code rules, you will find that the '{' is on the same line as the "if" or "else" statement. This is the de facto standard.

Hope this will help.

EDIT: Moved

Re: No { and } in code after if statement

This is NOT bug, so you posted in wrong forum (which is named "Bug reports")  smile

[no signature]

Re: No { and } in code after if statement

It doesn't really matter, and if a mod is released that should be near such a line, I bet you will be given the right replacement.

Re: No { and } in code after if statement

The has been up before, but since it's a difficult topic to search on, I can't find it smile

play75010 wrote:

1) enhance code readability

Not really, just more clutter.

play75010 wrote:

2) mod insertion (for example, if you need to add a line for a mod after a "if" statement and want to keep the original as a comment, then it is much better to have a '{' before and '}' after, isn't it ?)

This hasn't been a problem before with any mod. Everybody that are making mods are experienced PHP coders so they should know how it works.

play75010 wrote:

I have been working as a programmer for more than 10 years and I can tell you that this is absolutely essential.

Heh.. um.. Everybody says that. And I can't agree with you, it's not essential at all.

play75010 wrote:

Also, please note that in most programming guides or code rules, you will find that the '{' is on the same line as the "if" or "else" statement. This is the de facto standard.

AFAIK (it's what I've heard, can be wrong) having the { on the same line as the if statement is more common in the java world, and having it on the second line is often seen in C/C++. PHP is neither and many people that comes from java tends to use place it on the same line. Personally, I think having it on a second line increses readability a lot. It also kind of eliminates the need to have { } on one-liners, since it makes it so obvious. Lastly, having the { on the same line as the statement is nowhere near a de facto standard, everyone does it differently, so just adapt to the project you're working on.

Re: No { and } in code after if statement

OK. No worries. I was just trying to contribute.

In my last job, my code would have been automatically rejected if "if" statements were not surrounded by '{' and I've seen numerous situations where it made sense especially when several people worked on the same code.

6 (edited by StevenBullen 2006-05-29 11:22)

Re: No { and } in code after if statement

play75010 wrote:

In my last job, my code would have been automatically rejected if "if" statements were not surrounded by '{' and I've seen numerous situations where it made sense especially when several people worked on the same code.

Be thankful its your last job then... and not your current one. Then you dont have to worry.... sounds like you listen to your work to much... and not to your head.

Re: No { and } in code after if statement

I have on motto: "No matter how you do it, if it works, it's fine" big_smile
And I live by it every minute...

Re: No { and } in code after if statement

I wrote a blog post about this some time ago.

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: No { and } in code after if statement

Rickard wrote:

The latter is of course only true if you put your curly braces in the right place smile

QFT smile

But I agree with your blog Rickard, proper indentation and clean code makes a mistake like that very unlikely.

Re: No { and } in code after if statement

Yeah smile

11

Re: No { and } in code after if statement

i not use braces for single line but when used i wrote then open one in a line

 if (i==0)
{                  <-- here
  ....
  ....
}
If your people come crazy, you will not need to your mind any more.