• C# if statements
    15 replies, posted
Can if statements be on one line and is it common practice/acceptable, e.g: [code]if (!foo) {bar++;}[/code] Are the curly brackets even needed then? Sorry if this seems like a stupid question, just my new tutor for uni is really pissy about standards.
There isn't really a standard for that AFAIK.. It's more coding style and preference. So you can't really go wrong.
I'm not sure if there is a recommended standard similar to how Java's coding standard works, however valid code is: [code] if (!foo) bar++; or if (!foo) { bar++; } [/code] (Regardless of indention style) I prefer the second because the first can sometimes lead to subtle mistakes like: [code] if (!foo) bar++; foobar++; [/code] In answer to your question, yes statements can be on one line as you pointed out (with or without the curly braces) generally it depends on the specific project your working on. It's less important to follow a single standard and more important to make sure that there is some sort of [b]consistent[/b] standard. Ask your teacher or chose what you prefer.
Personally, if it's something short like that, I do, but not usually.
[QUOTE=Ortzinator;18826137]Personally, if it's something short like that, I do, but not usually.[/QUOTE] Depends on whether or not it helps the code look more readable. If It's important and I want to draw attention to it I will use braces, but if it's some short thing I won't bother. I will usually keep the if statement and the result on different lines though, and tab appropriately.
You could even do [code]if (!foo) bar++;[/code]
or you can do [code] foo ? Console.Write("It is true") : Console.Write("It is false") [/code]
It really depends on the case. One line looks better when you are just going to do one thing. But if you are doing multiple lines or elseifs and such it's easier to tell what's going on if you split it between lines.
[QUOTE=Nisd;18915140]or you can do [code] foo ? Console.Write("It is true") : Console.Write("It is false") [/code][/QUOTE] That's not really the best usage case for that. Usually I use it for assigning a variable, like [code] uint unsigned = (uint)(signed < 0 ? -signed : signed); [/code] Or something like that.
I quite often use it when I need to print something out for debugging. Like [code] Console.Writeline(p.IsRunning ? ("Process is running") : (Process is not running")) [/code]
I end up doing this often when doing getters and setters. Such as: [code] public double measurement { get { return getMeasurement(); } set { setMeasurement(value); } } [/code]
Depends on the context. I avoid one line code if I need to have more than one statement. [code] if (a = 123) { Console.Writeline("123"); } else { Console.Writeline("345"); } [/code]
[QUOTE=bord2tears;18979804]I end up doing this often when doing getters and setters. Such as: [code] public double measurement { get { return getMeasurement(); } set { setMeasurement(value); } } [/code][/QUOTE] Er, what's wrong with: [code] public double measurement { get; set; } [/code]
There's some things you can't do with auto-properties. Maybe he's doing some validation in setMeasurement that would cause an infinite loop with the get. Then again, that could be me not knowing how to use the properties correctly in certain situations.
You still don't need the extra methods though.
Nah, just an extra private variable.
Sorry, you need to Log In to post a reply to this thread.