|
I try not to, especially if it's just a return statement. But then, when that's the case, as long as the condition isn't already too long, I'll try to put it on the same line.
But then some editors will try to force it on the next line anyway.
|
|
|
|
|
what about conditional operators?
userCanVote = (userAge == 18) ? true : false;
I think this is the example that breaks the mold for everything. I have seen some people do crazy things with conditional operators just to show their "knowledge" of them off.
|
|
|
|
|
Craziest code example I have seen for a while.
I have never heard of a country, organization or anything else were only 18-year-olds, noone else whether younger or older, can vote.
And I have never seen the conditional statement used to select between alternatives 'true' and 'false'. I hope the compiler is smart enough to ignore the conditional and generate code as if you had written
userCanVote = userAge == 18;
|
|
|
|
|
Member 7989122 wrote: I have never heard of a country, organization or anything else were only 18-year-olds, noone else whether younger or older, can vote.
Kevin
|
|
|
|
|
I admin, recently the CodeLen suggest a code piece that I had to do a double look.
return foo ? (foo=new Foo()); or
if (obj is Foo foo) foo.bar();
It's shorter.
|
|
|
|
|
When omitted, it's usually by deletion.
They get popped in by habit - and even if only one statement follows there's no reason to remove them. It's to create a visual convention - you (at least my) eyes have everything grouped for them, indent levels almost always co-re-inforeced with something like:
if(the beginning) {
some_silly_possibility();
}
There are conventions (my own, obviously) for else's, too. Similar for all code-blocks. Even when source is well out of sight you know the beginning and the end (and which/where to insert/delete code and/or braces).
Even if not via some (so-called) standard, it is visually obvious to any onlooker, and that's the real point.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
Braces around single line commands. Needed or not?
Survey period: 12 Feb 2018 to 19 Feb 2018
|
|
|
|
|
I mean when I use If..While or For my finger automatically put open and close braces and then start coding between them...
It same like when I switch from C# to VB.NET code then I put semicolon at the end of the and VB compiler used to give to give me compilation error...
Find More .Net development tips at : .NET Tips
The only reason people get lost in thought is because it's unfamiliar territory.
|
|
|
|
|
koolprasadd wrote: It same like when I switch from C# to VB.NET code then I put semicolon at the end of the and VB compiler used to give to give me compilation error...
Yes, recently I've written a lot of code where I've been switching back and forth between C# and VB. Frazzles the brain!
Kevin
|
|
|
|
|
My rule of thumb, generally, is to put the braces in. On the rare occasions I don't I usually ask myself, "why does this not use the ternary operator then?" In the end, I virtually never wind up without braces.
|
|
|
|
|
ReSharper handles that for me if or else has more than one statement, it puts braces both, else, removes.
|
|
|
|
|
In my experience, ReSharper is a very good tool for enforcing coding standards.
In my opinion, you and your coders should also be aware of the reasons behind the rules. This makes them more likely to be followed, rather than treated as "one of those annoying Management things".
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Ditto
Graeme
"I fear not the man who has practiced ten thousand kicks one time, but I fear the man that has practiced one kick ten thousand times!" - Bruce Lee
|
|
|
|
|
The project I started my career as a developer used Resharper to ensure the compliance of most of the coding standards. Two years later the project is rolled out and I switch to another which does not have Resharper, I felt really helpless for the next few days. Good thing I could learn to code without its assistance.
|
|
|
|
|
When it's an error detect and method fail condition, then is short, sweet and single line:
So
if (inputList.Count == 0) return; Or
if (inputList == null) throw new ArgumentException("inputList cannot be empty");
I grew up having to indent code manually, and I learned the hard way that
if (a == b)
c;
d; does not do what it looks like it does, particularly when you edit code and add c or d!
If the brackets are there to start with, it's pretty obvious:
if (a == b)
{
c;
}
d; Modern IDE's make it a whole load easier, but ... good habits are hard to break!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: if (a == b)
{
c;
}
d;
But that's so butt ugly visually distressing!
if (a == b) {
c;
}
The reason I do it that way is to demark the beginning of the block(it's parent) with the end of the block. A further inner one would have all of that bumped in an indent's worth. The idea is for the eye-candy to be very easy to decode, visually. Note the vertical row in your version. Also, I save a line!!!!
But, I'm practical - everyone like their own indents or they'd be using something else.
Ravings en masse^ |
---|
"The difference between genius and stupidity is that genius has its limits." - Albert Einstein | "If you are searching for perfection in others, then you seek disappointment. If you are seek perfection in yourself, then you will find failure." - Balboos HaGadol Mar 2010 |
|
|
|
|
|
It's too often that single statements turn into multiple statements and you forget to add braces. So allways add the braces from the very beginning.
|
|
|
|
|
henningbenk wrote: single statements turn into multiple statements and you forget to add braces
I've made thousands of stupid errors in my career so far, but have yet to do that. How is this not obviously wrong, unless you code with Python?
if (something)
do1();
do2();
In fact most compilers will issue a warning for that nowadays.
|
|
|
|
|
Nemanja Trifunovic wrote: I've made thousands of stupid errors in my career so far, but have yet to do that.
Agreed.
|
|
|
|
|
I've seen that exact problem come up before, usually in a rush when people are feverishly trying to meet a deadline. Thankfully it's only happened a couple of times but it's a real PITA to debug when it happens.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music."
-- Marcus Brigstocke, British Comedian
|
|
|
|
|
Nemanja Trifunovic wrote: I've made thousands of stupid errors in my career so far, but have yet to do that.
Ditto.
Kevin
|
|
|
|
|
That is only a problem if you put the single statement on a new line. When I do that, I always add the braces from the beginning, but if the single statement is simple and short, such as a simple value assignment (if (fruitType == banana) color = yellow;), it all goes on one line. If you add another statement to this 'if' claues, you move the first assignment down and add the braces.
Never an indent without a brace on the line above!
|
|
|
|
|
but it depends on the situation.
|
|
|
|