if else Style
-
if (expression)
{}
else
{}
Everything else is just unnecessary typing/reading.
Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript
-
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
Second style was invented at the Department of Redundency Department.
Wrong is evil and must be defeated. - Jeff Ello
-
Second style was invented at the Department of Redundency Department.
Wrong is evil and must be defeated. - Jeff Ello
You can say that again.
-
You can say that again.
-
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
BernardIE5317 wrote:
final statement else throw who_the_heck_knows_what_happened
In such cases, would a
switch case
statement be more appropriate? I mean when there are more than 2 choices. -
BernardIE5317 wrote:
final statement else throw who_the_heck_knows_what_happened
In such cases, would a
switch case
statement be more appropriate? I mean when there are more than 2 choices.Thank you for the thought provoking suggestion. Please consider per below. if(a==true && b==true) {...} else if(a==true && b == false) {...} else if(a==false && b == true) {...} else if(a==false && b == false) {...} else throw who_the_heck_knows_what_happened; // just for the heck of it
-
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
I do hope that the "==true" and "==false" are not meant literally! A programmer comparing a logical expression against "true" or "false" have not understood what is meant by a logical expression. I wonder how many of those programming that way also speak that way! "If you have a moment to spare is true, I want a talk with you", or "If the door is unlocked is false, you'll find the key under the door mat" - noone that I know speaks that way. I have met a few programmers who program that way, but I never heard any of them speak with "is true" or "is false".
Religious freedom is the freedom to say that two plus two make five.
-
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.
Best, Sander Azure DevOps Succinctly (free eBook) Azure Serverless Succinctly (free eBook) Migrating Apps to the Cloud with Azure arrgh.js - Bringing LINQ to JavaScript
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.
-
I do hope that the "==true" and "==false" are not meant literally! A programmer comparing a logical expression against "true" or "false" have not understood what is meant by a logical expression. I wonder how many of those programming that way also speak that way! "If you have a moment to spare is true, I want a talk with you", or "If the door is unlocked is false, you'll find the key under the door mat" - noone that I know speaks that way. I have met a few programmers who program that way, but I never heard any of them speak with "is true" or "is false".
Religious freedom is the freedom to say that two plus two make five.
May I please inquire how else does one evaluate a logical expression in C++. One can write
if(a)
orif(!a)
. But these perform the same function asif(a == true)
andif(a == false)
. if(I do not understand your meaning == true) I kindly request clarification.; -
I do hope that the "==true" and "==false" are not meant literally! A programmer comparing a logical expression against "true" or "false" have not understood what is meant by a logical expression. I wonder how many of those programming that way also speak that way! "If you have a moment to spare is true, I want a talk with you", or "If the door is unlocked is false, you'll find the key under the door mat" - noone that I know speaks that way. I have met a few programmers who program that way, but I never heard any of them speak with "is true" or "is false".
Religious freedom is the freedom to say that two plus two make five.
May I please inquire how else does one evaluate a logical expression in C++? One can write
if(a)
orif(!a)
. But these perform the same function asif(a == true)
andif(a == false)
. if(I do not understand your meaning == true) I kindly request clarification.; -
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.
-
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
condition ? f() : g();
:sigh:
CI/CD = Continuous Impediment/Continuous Despair
-
May I please inquire how else does one evaluate a logical expression in C++. One can write
if(a)
orif(!a)
. But these perform the same function asif(a == true)
andif(a == false)
. if(I do not understand your meaning == true) I kindly request clarification.;Similarly, "if the door is locked" perform the same function as "if the door is locked is true". In speech, noone that I know of includes the "is true". So why do you program as
if (doorIsLocked == true) ...
rather than
if (doorIsLocked) ...
I see no reason for or advantage of creating a more complex logical expression, adding a second, redundant element. Both "doorIsLocked" and "doorIsLocked == true" are logical expressions, the second one just more complex than it needs to be. (Hopefully, the compiler is able to optimize the redundant element away!) If I program a test like
if (x < 10 && x < 20) ...
all programmers I know would point out that it is redundant to test for "x < 20" if you already have tested that x < 10. Adding an extra element a logical expression, to see whether a true "a" is equal to "true" (or that a false "a" is different from "true") is similarly redundant. The logical "a" expression is true or false, all by itself!
Religious freedom is the freedom to say that two plus two make five.
-
condition ? f() : g();
:sigh:
CI/CD = Continuous Impediment/Continuous Despair
Your kind suggestion ? I agree : I still agree;
-
Your kind suggestion ? I agree : I still agree;
-
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.
-
condition ? f() : g();
:sigh:
CI/CD = Continuous Impediment/Continuous Despair
-
Or even worse ...
if (age >= 16)
{
ticketClass = adult;
}
else if (age < 16)
{
ticketClass = child;
} -
Or even worse ...
if (age >= 16)
{
ticketClass = adult;
}
else if (age < 16)
{
ticketClass = child;
}