|
Not to me their not
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
|
|
|
|
|
VPN's (like Tor) come in handy for this situation. I have a mail scrubbing service that blocks my IP here. Use it for that (no, I don't use Tor).
>64
There is never enough time to do it right, but there is enough time to do it over.
|
|
|
|
|
pkfox wrote: to Chris and the boys. And the girls... there are some involved in making CP, what CP is too
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Greetings & Kind Regards
I seek advice and knowledge of others' practice re/ a minor matter of style. In particular to be specific I vacillate betwixt and between two forms of final statement of if ... else ... series of statements assuming every possible condition is accounted for as shown in simple specimen / example / sample below (not snippet as it is not a snip of a larger code base).
1st style :
if(expression == true) {...}
else {...}
2nd style :
if(expression == true) {...}
else if(expression == false) {...}
Note it is the final statement I am inquiring regards. Do your kind selves accept the default final result as shown in 1st style or perform explicit unnecessary test as shown in 2nd style for no other reason than to make the code self documenting and easier to understand in particular for a complex lengthy series of conditions.
btw in 2nd style on occasion I add as final statement else throw who_the_heck_knows_what_happened;
Thank You Kindly
[edit] I have concluded kind Jacquers is quite correct. As it happened upon some coding just now of a simple series of such it was evident an explicit final test results in confusion as it is clearly not necessary. However in a lengthy complex series an explicit test makes code self documenting and so simpler to understand. KISS
modified 26-Jan-24 0:31am.
|
|
|
|
|
It really depends on the logic you're trying to achieve. It's going to depend on the context it's in. And yes, the first style is fine if that's what you're intending to do.
|
|
|
|
|
The second requires more "comprehension" (reading) time. "Else" means "everything else". And beside "true" and "false", there may be "no value"; e.g. tri-value checkboxes / radio buttons.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Long ago, before WWW was invented, we did have Internet for email, accessing open source code libraries, exchanging photos - and open discussion forums. One of the discussion platform was COM, running on DEC mainframes. COM frequently asked the user yes/no-questions (e.g. "Do you want to delete this entry?"). The routine accepted answers "Yes", "No" and "Maybe". For a "maybe" answer, COM used a random generator to choose between "yes" and "no" actions. (I was using COM for at least a year before I was made aware of the "maybe" option, but after that, I used it frequently )
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
if (expression)
{
}
else
{
} Everything else is just unnecessary typing/reading.
|
|
|
|
|
Indeed.
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
I agree
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
|
|
|
|
|
Agree. 8 lines is required, even for cases that could have used a simple ?:, but beyond 8 lines is a waste.
Many times I have met programmers who really oppose ?: and insist that e.g.
ticketClass = (age >= 16)? adult : child; must be written over 8 lines as
if (age >= 16)
{
ticketClass = adult;
}
else
{
ticketClass = child;
} If their productivity is measured in number of source code lines produced, I can see the justification for it, but that's all I can think of
Another funny thing is that those who oppose ?: frequently are proud of their creations in regular expressions.
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
Or even worse ...
if (age >= 16)
{
ticketClass = adult;
}
else if (age < 16)
{
ticketClass = child;
}
|
|
|
|
|
You still do not handle the null case.
True story:
A friend of mine, living here in Norway, is a US citizen. She was pregnant, and planned a recreation trip out of Norway, 3 months after the expected time of birth, with her baby. Even a baby needs a passport. If you live in Norway and apply for a US passport, it can (or at least could in those days, this is 35+ years ago) take half a year to get through the paper mill.
The parents didn't want to know the sex of the baby before the delivery, so when they applied for a passport for the yet unborn baby, they could not state its name. They could not state its birthday. They could not provide a photo of the passport holder. Yet, the US passport authorities did issue a passport to a person of unknown sex, unknown name, unknown birthdate, with no photo or fingerprint.
I am honestly surprised that none of the systems handling that passport application went into a fatal exception Or maybe they did, and it had to be debugged before the passport could be issued. Maybe, 35 years ago, US passports were essentially handled through manual procedures where exceptional conditions were handled by human brains. I am not sure that all of the fully automated passport handling systems we are using today would be able to handle that passport without stumbling and falling over.
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
trønderen wrote: this is 35+ years ago ... surprised that none of the systems handling that passport application went into a fatal exception
That long ago might not have been a system. Might have been a person.
Could also just be a special case that they handle.
|
|
|
|
|
Yuck
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
|
|
|
|
|
It could be worse, it could be raining:
else if(marks > 80 && marks < 95)
{
}
else if(marks > 70 && marks < 80)
{
}
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
Yes, I have seen that a few times in QA.
|
|
|
|
|
I think the ternary operator gets a bad rap (although somewhat deserved) because it so often gets abused, particularly, I think, because it's so easy to abuse.
Just today I caught myself chaining 3 of them together, and then luckily confused myself and rewrote it using if statements.
Check out my IoT graphics library here:
https://honeythecodewitch.com/gfx
And my IoT UI/User Experience library here:
https://honeythecodewitch.com/uix
|
|
|
|
|
you forgot one...
ticketClass = age switch
{
>= 16 => adult,
_ => child
};
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
|
|
|
|
|
I like things more compact
if ( expression ) {
} else {
} I think that's the way it's done in K&R, in which case I'm in good company.
But putting the opening brace on the first line of a function definition annoys me intensely e.g. Don't:
int main(int argc, char *argv[]) {
"A little song, a little dance, a little seltzer down your pants"
Chuckles the clown
|
|
|
|
|
yeah, the brace on the same line makes me want to puke.
Charlie Gilley
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Has never been more appropriate.
|
|
|
|
|
charlieg wrote: yeah, the brace on the same line makes me want to puke. I follow the same rule when I write plain prose. If I want to write something that is not absolute but associated with some condition, I enclose the entire conditional part in braces and put on separate lines. Such as
If (the rain stops), I told her,
{
let's take a walk in the park.
}
but if it keeps raining,
{
I want to lit the fireplace and fetch a bottle of wine from the basement
} When I write the prose this way, it is so much easier for the reader to foollow the structure of both the conversation and the actions.
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|
|
agreed. On the other hand, I am not a religious zealot of braces. It really comes down to being able to debug - meaning putting a breakpoint on something I want to check or might need to check. Simple logic, I like (love) the ? option.
Don't get me started on prefix or postfix ++ or -- operators in if statements. There is a special place in coder hell for that crap.
Charlie Gilley
“They who can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety.” BF, 1759
Has never been more appropriate.
|
|
|
|
|
Wholeheartedly agree!
Having the braces on separate lines makes it so much easier to determine where the then/else clauses begin and end. Lining them up vertically at each indentation level also means you can easily see what goes with what, such as:
if (expression)
{
// then result
if (other condition)
{
// Other condition then result
}
else
{
// other condition else result
}
}
else
{
// else result
}
I really hate code that puts all the opening braces on the if (expression) line, or the "else" line. Programmers who don't believe in clarity of their code should be banned from the practice.
Don't forget -- comments can also help a lot in improving clarity.
|
|
|
|
|
Norm Powroz wrote: Having the braces on separate lines makes it so much easier to determine where the then/else clauses begin and end. You have indentation for that, don't you?
If you indent neither of the braces, they do not enforce the indentation, but blurs both the indentation and the keyword causing the indentation. If you indent both, the keyword is 'revealed'. And the block is extended by two two lines, making it more prominent.
I think that making 'the indenting keyword' more visible (by avoiding the blurring opening brace immediately below it) is a good thing. Indenting before the opening brace is logically inconsistent - so the brace should be un-indented, at the line of the if / while / ... . Also, when you read e.g. an if, and the line ends abruptly, with no explanation, you should rather include the brace to indicate: There is an explanation - this brace tells that a block is following!
The closing brace on a separate indented line ensures that the indented block is at least two lines long. That should be enough for everybody, to make sure that the block is easily identified.
I think un-indented braces blur the indented block, rather than enforcing it as it should.
Religious freedom is the freedom to say that two plus two make five.
|
|
|
|