|
In reading all of the responses, I'm quite surprised that the only alternative mentioned was the tertiary operator (?: ). While it's simple to respond 'context dependent' as I saw in some of the replies, I think maybe it would help to spell out the concerns as well as offer up more choices.
Let's start with the elephant in the room... readability and maintainability. They both go hand in hand to answer the question "what are you trying to do or say in the code?"
In addition, some of the choices can lead to both bugs and code creep.
Let's start with if/else as a potential solution to express a/the logical path to traverse. A couple of simple rules would help (these are my rules, some may be obvious, but some are what I call 'Where's the curly brace' religious preferences. Take everything I write here are my own style rather than engage in religiosity).
if (condition 1)
{
}
else if (condition 2)
{
}
else
{
}
I always include the curly braces to ensure that scope is honored i.e. what code is executed based on which condition is met.
Too often I've see this:
if (condition 1)
else if (condition 2)
else
The problem here is if another author comes in to add/maintain the code, and adds a second line of execution to one of the conditions. If that author forgets to now add the curly braces to enclose scope, bad things happen.
if (condition 1)
else if (condition 2)
else
In addition, the whole Nickleback induced hatred the tertiary operator hails back to a late 70's bug introduced in a version (or couple of versions) of the Whitesmiths C compiler, and has since become lore. If we are striving for readability, then:
if (condition) ? "do one thing" : "do the other thing";
If you are vehemently opposed to this, chances are you also avoid the null coalescing as well as other similar constructs:
char *s = null;
char *t = null;
...
t = s ? "some string" : "some default";
But really, is this the only way to proceed?
How about:
switch (operand)
{
case 1:
break;
case 2:
break;
case 3:
break;
default:
break;
}
Once your application starts to grow in complexity, then you start to contend with nesting:
if (condition 1)
{
if (condition 2)
{
}
else if (condition 3)
{
if (condition 4)
{
}
}
}
This quickly becomes a nightmare to test and maintain. Testing requires the exercising of all logical branches, which this format complicates.
It quickly becomes evident that a better abstraction and/or implementation would be better suited. In the case of states, where a change of state triggers an execution of code and a transition to a new state, then a state machine would be better suited here to replace the if/else scaffolding (beyond the scope of this response).
And so it goes...
BTW, there are valid cases where the second format form of if/else would be better suited. For the case of nullable types i.e. bool? var, sometimes explicitly spelling out the true and false cases makes for better reading and comprehension:
bool? state;
...
if (state == true)
{
}
else if (state == false)
{
}
else
{
}
There's more than one way to skin and if/else clause!
|
|
|
|
|
Stacy Dudovitz wrote: If that author forgets to now add the curly braces to enclose scope, bad things happen.
So not only coding now to guess at the competence of future programmers but also presuming that comprehensive unit testing will not happen?
|
|
|
|
|
To the first question about competence, that may be a bit harsh. We are, after all, human beings, and as such, even the best of us make mistakes. To be clear, while some mistakes can be attributed to competency levels and/or experience, I've also seen errors due to time pressure from the decisions makers, long hours of debugging a nasty problem, or other factors which can contribute to bugs.
Hence, my suggestion/recommendation falls more in line with defensive coding rather than assuming that everyone that is not me is an idiot. Your mileage may vary on that point.
With regards to unit testing, even in shops where unit testing is mandated as part of the development process (again, until a time deadline or other external factors discourage the practice in light of external pressures), my experience has been that often unit testing is more to tick off a checkbox than to actually produce maintainable and reliable code. Writing proper unit test code is an artform unto itself, and is predicated on other practices such as one method should encompass exactly one function. (Lets not split hairs on the triumvirate of the types of testing i.e. unit, system and integration tests. In this case I am referring to all three under the single banner of 'unit testing'.)
Bottom line, whatever practice you or your shop choose to implement, it should be purposeful and consistent. My post here was to offer a more thorough response to the original question.
|
|
|
|
|
Stacy Dudovitz wrote: often unit testing is more to tick off a checkbox
That can be solved with required code reviews and required code coverage metrics.
|
|
|
|
|
Agree on the curly braces. There was someone who did a lot of research about which if/else brace pattern worked best for automated merging, but I can never find it.
|
|
|
|
|
Do not attempt the extra check if you are dealing with floating points.
You might really need the extra ELSE clause.
if (x > 1.0) {
}
else if (x <= 1.0) {
}
else {
// yes you can reach this
throw wtf;
}
What are possible values of x?
|
|
|
|
|
Disagree! (contingent on compiler switches set, of course )
else {
throw wtf;
}
if (x == NaN) or some similar hairy answer, the compiler (again, with the correct compilation switches set) will automagically throw an exception.
Sidebar: One would NOT want to set said same switches on an embedded piece of code, since typically exception handling is turned off. Therefore, one would NOT, in such an instance wish to throw wtf .
|
|
|
|
|
The OP never mentioned language or compilers.
Some compilers for whatever language may not support those options.
It was a general stylistic question. Most compilers (and forums) wonβt even warn when you use assignment operator instead of comparison operator.
π
|
|
|
|
|
Hell no! If you must put the second conditional for documentation purposes, make it a comment
if (cond)
...
else // if (!cond)
...
or
if (cond)
...
else // cond
...
I've seen that style used for conditional compilation, not so much for braces.
#if cond
...
#else
...
#endif //cond
|
|
|
|
|
Wordle 951 3/6
β¬β¬π©β¬β¬
π©π©π©π¨β¬
π©π©π©π©π©
|
|
|
|
|
Wordle 951 3/6*
π©β¬π©β¬β¬
π©π©π©β¬β¬
π©π©π©π©π©
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Wordle 951 6/6
β¬β¬β¬β¬β¬
β¬β¬β¬β¬π¨
β¬π©π©β¬β¬
β¬π©π©β¬β¬
π¨π©π©π©β¬
π©π©π©π©π©
Phew
|
|
|
|
|
β¬β¬π¨β¬β¬
β¬β¬π©β¬β¬
π¨π©π©π¨β¬
π©π©π©π©π©
In a closed society where everybody's guilty, the only crime is getting caught. In a world of thieves, the only final sin is stupidity. - Hunter S Thompson - RIP
|
|
|
|
|
Wordle 951 5/6
β¬π©π¨β¬β¬
π©π©β¬β¬β¬
π©π©β¬β¬β¬
π©π©β¬π©β¬
π©π©π©π©π©
|
|
|
|
|
Wordle 951 4/6*
β¬β¬π¨π¨β¬
π¨π¨β¬β¬β¬
π©π©β¬π©β¬
π©π©π©π©π©
Happiness will never come to those who fail to appreciate what they already have. -Anon
And those who were seen dancing were thought to be insane by those who could not hear the music. -Frederick Nietzsche
|
|
|
|
|
Wordle 951 6/6*
π©β¬π©β¬β¬
π©β¬π©β¬β¬
π©β¬π©β¬β¬
π©β¬π©β¬β¬
π©β¬π©β¬π¨
π©π©π©π©π©
I didn't find that easy!
|
|
|
|
|
Wordle 951 3/6
β¬π¨β¬β¬β¬
π©π¨β¬β¬β¬
π©π©π©π©π©
Ok, I have had my coffee, so you can all come out now!
|
|
|
|
|
Venting on! This is a vent!!! <===
Why does Microsoft make everything so D*** difficult? I don't want to use Windows Hello... Stop prompting me!!
Why does the system configuration require editing the registry?
Why are they attempting to duplicate the Mac experience?
Why does updating a simple action mandate a blessing from the Pope and/or a drunk Newfie?
Why are they making the simplest operation a major exercise in futility and frustration???
Why is it that .... H***, I'm could go on for quite awhile but I won't.
Suffice to say I am not a fan of Windows 11.
Venting Off!!
If it wasn't for the fact that I am deeply mired (?) in the Windows environment. I would move to Linux.
I might just consider learning how to develop the a KDE type interface for Linux and write one that emulates Windows 7!!
Cegarman
document code? If it's not intuitive, you're in the wrong field
Welcome to my Chaos!
|
|
|
|
|
|
Hi,
Ok!! Thanks for the info! I will definitely have a look at both of those!!
Cegarman
document code? If it's not intuitive, you're in the wrong field
Welcome to my Chaos and Confusion!
|
|
|
|
|
I feel for you, I am not really doing Windoze software anymore, part of the reason you have described. Windows Peaked with 7, still have it on a couple machines at home. 10 is OK(ish), 11 as you said, I still have a Windows 2000 machine takes an age to start up (only 16 bit!) but you can beat it with a rock it will keep truckin', make a loud noise 11 will fall over (or drain your battery if you are unplugged!)
|
|
|
|
|
After my first attempt at installing Win2000 on a blank machine succeeded in a fraction under 11 minutes, I fell in love with that version. Sadly I've had to move on, but still remember it fondly... It did everything you could want it to, never complained, never crashed, and everything was pretty much where you would expect it to be. Downhill all the way since then as far as I'm concerned...
|
|
|
|
|
The MOD still (or did use it until recently) use it. It's NT at heart it will work on a stone tablet if it meets the basic boot requirements
|
|
|
|
|
Hi,
I traded someone my Windows ME disks (floppy's) for their copy of Windows 2K.
I believe I got the better part of that deal. Sadly, that disk is long gone. I do have a CD with key for Windows XP laying around somewhere. And a copy of Redhat 7 hiding somewhere as well.
Cegarman
document code? If it's not intuitive, you're in the wrong field
Welcome to my Chaos and Confusion!
|
|
|
|
|
I still have an ME installer that I never used all legal OEM copy some where.
|
|
|
|