|
Yours is my preferred method. Though sometimes there are cases that either the false or true statement just makes it easier to understand what is happening. In some cases where I have if..then..else but no case statements I'll handle the only definitive answer first and handle the opposite and "maybe" answers elsewhere.
|
|
|
|
|
Graham Shanks wrote:
For instance
if(statement is false)
return ERROR_CODE;
else
{
}
in preference to
if(statement is true)
{
}
else
{
return ERROR_CODE;
}
This should be rewritten as follows for clarity and reduced code size.
if(statement is false)
{
return ERROR_CODE;
}
There is NO ELSE.
Gary
|
|
|
|
|
And THAT makes it even easier to read! Good catch!
Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
|
|
|
|
|
if(areUsingTDD)
thisQuestionDoesntReallyMakeSense
|
|
|
|
|
How does TDD fit in here?
You don't use coditions in TDD?
|
|
|
|
|
because where boolean types are supported only by integers then FALSE is always zero but TRUE can be 1, -1, 42 or anything that is not zero thus relying on TRUE to be one, and I have seen lots of old code that does, can lead to a world of pain and despair.
|
|
|
|
|
But in those cases it is better to test for non-zero instead of TRUE.
BOOL bAnswer = ReturnOfSomeFunction();
if( bAnswer != 0 ) {
do something;
}
|
|
|
|
|
That was exactly the point I tried to make. Tests on TRUE can be unreliable, Tests on FALSE that might well be stated as !FALSE or NOT FALSE can usually be relied upon. Hence my response to the survey test FALSE first.
|
|
|
|
|
But of course that only pertains to the Microsoft BOOL variable and not bool in C++. C did not actually have a bool value defined until C99. BOOL is actually an INT (sizeof(BOOL) = 4 or 8 on x64) whereas bool is a byte (sizeof(bool) = 1). Most languages use a built in type of bool that is one byte and only capable of being true or false (1 or 0). The proper use of BOOL is to test if it is non-zero for TRUE with most functions that return BOOL. Some functions that return BOOL will actually return more than just 2 possible answers (ie: 0 for success or a value representing a return or error code).
|
|
|
|
|
Then it depends on the language?
|
|
|
|
|
|
if(statement is true)
{
return "Yes";
}
else if(statement is false)
{
return "No";
}
else
{
return "The meaning of life";
}
In what school this philosophy lies?
Ittay Ophir
ittay.ophir@gmail.com
|
|
|
|
|
Don't know, but I can tell you that if you ever find your function returning "the meaning of life" then something has gone horribly horribly wrong. :-/
|
|
|
|
|
A decent compiler would issue a diagnostic for that final else clause.
Software Zen: delete this;
|
|
|
|
|
return 42;
This would be it I think.
Cheers
You have the thought that modern physics just relay on assumptions, that somehow depends on a smile of a cat, which isn’t there.( Albert Einstein)
|
|
|
|
|
|
There are two else s and at least one if to nuke.
|
|
|
|
|
That's fuzzy logic and would mean that "statement" could not be a boolean variable having only two states. An example would be if you were testing to see if a string was equal to something. An exact match would be true and a partial match would be not false or true and a complete miss would be false.
Statement = "My dog has fleas"
if( Statement = "dog" )
else if( Statement contains "dog" case insensitively)
else
|
|
|
|
|
Behind every seemingly trivial task there are many schools of philosophy
Marcello Turnbull
|
|
|
|
|
It means people have too much free time.
|
|
|
|
|
How much free-time do you have?
|
|
|
|
|
A little time for suggesting a survey but so much time for answering a question
Cheers
Marcello Turnbull
|
|
|
|
|
if somthingIsTrue()
do this
else
do that
-------------------- and not -----------------------
if NOT somethingIsTrue()
do something
ELSE
do something else
if it is true
forsooth!
|
|
|
|
|
Well
If NOT IsEOF
keep reading
else
clean up and close file, etc
|
|
|
|
|
If there is code under both true and false conditions then I put the true block first, otherwise it's dependent...so I need to select two choices...
|
|
|
|