|
i usually try to single line as much as i can.
In any case, would be something like this:
if (condition) { statement; }
|
|
|
|
|
|
When I see the brackets with a single line of code, I think the developer has forgotten some instructions.
If there are no brackets, I understand immediately that the single instruction is desired.
|
|
|
|
|
I often have to fix bugs in legacy code that has the statement next to the if condition:
if (condition) statement;
It's a complete nightmare, especially when the code wraps around or runs off the page it doesn't matter which! 
|
|
|
|
|
I would only omit the braces if the entire statement is on one line.
if (condition) statement;
My least favorite option is
if (condition)
statement;
(I never, ever write that)
And when there's an else clause, I always use braces anyway.
modified 14-Feb-18 9:27am.
|
|
|
|
|
same here but sometimes i use on 'single line if' also if there are many line of codes on the function.
|
|
|
|
|
Seconded. Including braces for something like i++; is a bit too verbose for me, but it either has to be same line or braces.
Regards,
Rob Philpott.
|
|
|
|
|
None of my code has ever used an if statement. Just run a different program if you want it to do something else. My code is clear and always reads start to finish with no guessing!
Besides, who needs and if statement to print "Hello World"
Hogan
|
|
|
|
|
I once had to deal with a very strange bug in some legacy code written by someone who ...
a) put in braces for no apparent reason
b) used inconsistent indenting
c) wrote very long statements instead of breaking the code up into sub-routines (for readability)
The upshot was that I completely missed the scenario (simplified here) of
if (condition)
dosomething();
{
...
} I spent far too long setting up a test environment so that I could debug the code only to spot the problem as I was putting a breakpoint in place
Fortunately for the perpetrator they no longer worked at the same company
|
|
|
|
|
CHill60 wrote: b) used inconsistent indenting
The first thing I'd do in that case is to autoformat the indenting into something approaching sanity, even if I had no intention of making a 500 whitespace change commit just to make everything else easier.
Did you ever see history portrayed as an old man with a wise brow and pulseless heart, weighing all things in the balance of reason?
Is not rather the genius of history like an eternal, imploring maiden, full of fire, with a burning heart and flaming soul, humanly warm and humanly beautiful?
--Zachris Topelius
Training a telescope on one’s own belly button will only reveal lint. You like that? You go right on staring at it. I prefer looking at galaxies.
-- Sarah Hoyt
|
|
|
|
|
CHill60 wrote: a) put in braces for no apparent reason
Hmmm, be careful with removing braces in the middle of the function. Unless you know why the other engineer put them there. They can be used to enforce variable scope. You can actually use them to reduce the memory usage in a thread.
Best Wishes,
-David Delaune
modified 15-Feb-18 1:18am.
|
|
|
|
|
Trust me ... this guy wasn't that clever
As for variable scope ... most of his stuff was global
The whole thing was re-engineered - was clearer to read, faster to operate and used far less memory (and "leaked" none) by the time we finished.
|
|
|
|
|
|
I prefer:
if (condition)
statement; //short and simple only, no multi-line lambdas, nested calls, etc.
if (condition)
{
statement; //everything else
}
For truly simple, short statements it just looks cleaner in my opinion.
EDIT: Also I don't mix the styles like
if (condition)
statement;
else
{
statement;
statement;
}
If a brace is needed for one I make the rest braces as well.
modified 13-Feb-18 1:47am.
|
|
|
|
|
Most of the time, if in Visual Studio, I use the if code snippet that adds the braces anyway. I generally prefer them but I don't usually alter existing code that doesn't have them.
Otherwise, for one-line statements I might write
if (condition) statement;
Kevin
|
|
|
|
|
I find that having:
if (condition)
{
statement;
}
makes it easier for debugging, because the code path is much more obvious, and I can search it quicker.
|
|
|
|
|
So I always use braces
I didn't care until I found some weird behaving code.
if (something)
statement;
statement;
statement; Yeah...
|
|
|
|
|
Exactly!
Everyone is born right handed. Only the strongest overcome it.
Fight for left-handed rights and hand equality.
|
|
|
|
|
As the solo dev here, I get to do whatever I want. For single statement blocks, I almost always leave out the curly braces.
Over the years, I've really come to despise braces and semi-colons altogether and nowadays opt for VB on new projects.
"Go forth into the source" - Neal Morse
|
|
|
|
|
kmoorevs wrote: As the solo dev here And there's the rub.
/ravi
|
|
|
|
|
A solo developer should always write code with the next solo developer in mind. He might be very large and highly psychotic - a few diligent keystrokes today could save several indiscriminate axe-strokes tomorrow.
98.4% of statistics are made up on the spot.
|
|
|
|
|
PeejayAdams wrote: highly psychotic
Psychotic people are statistically less violent than the general population. Just sayin.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Richard Andrew x64 wrote: Psychotic people are statistically less violent than the general population.
Too many violent people who have yet to be captured and categorized as psychotic then. Unless those the majority of psychotics are just eating mushrooms and licking frogs.
|
|
|
|
|
Bassam Abdul-Baki wrote: Too many violent people who have yet to be captured and categorized as psychotic then.
You must be confusing psychopathic people with psychotic people. It's the psychopaths that can be violent, not the psychotics.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
just my personal opinion but the braces (not brackets) should be used and properly formatted.
if (true == value) {
foo = value;
}
I am also a proponent of constant values on the left side of the condition. This comes from my early days of C/C++ development and wanting the compiler to catch code mistakes like a missing '=' sign.
|
|
|
|