Old code
-
Reviewing some old projects I met a function like this:
bool func()
{
bool var = false;if(...) return var = false; else return var = true;
}
Actually what the function returns? I think
true
, because assigning can't befalse
.VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War
-
Reviewing some old projects I met a function like this:
bool func()
{
bool var = false;if(...) return var = false; else return var = true;
}
Actually what the function returns? I think
true
, because assigning can't befalse
.VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War
RomTibi wrote:
Actually what the function returns? I think
true
, because assigning can't befalse
.There is an easy way to know ! - make a program that will execute that code.
Patrice “Everything should be made as simple as possible, but no simpler.” Albert Einstein
-
Reviewing some old projects I met a function like this:
bool func()
{
bool var = false;if(...) return var = false; else return var = true;
}
Actually what the function returns? I think
true
, because assigning can't befalse
.VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War
Any assignment is also an expression which has the value of the assigned value.
if(...) return var = false; // returns false else return var = true; // returns true
Some programmers make use of this fact like this:
int x;
while ((x = SomeFunction()) > 0)
{
// use x for something..
}Whether or not this is good style is up to personal preference..
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
-
Reviewing some old projects I met a function like this:
bool func()
{
bool var = false;if(...) return var = false; else return var = true;
}
Actually what the function returns? I think
true
, because assigning can't befalse
.VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War
RomTibi wrote:
I think
true
, because assigning can't befalse
.True (regardless).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
-
Reviewing some old projects I met a function like this:
bool func()
{
bool var = false;if(...) return var = false; else return var = true;
}
Actually what the function returns? I think
true
, because assigning can't befalse
.VII. 36. When you surround an army, leave an outlet free. Do not press a desperate foe too hard. SUN-TZU - Art of War
-
RomTibi wrote:
I think
true
, because assigning can't befalse
.True (regardless).
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles