The Boy Scout Rule
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Be nice to others! Respect the FreeCAD code of conduct!
Re: The Boy Scout Rule
Post amended due to the explanation of @adrianinsaval below.
Regards
Carlo D.
Regards
Carlo D.
Last edited by onekk on Sun Jun 26, 2022 5:27 pm, edited 1 time in total.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- adrianinsaval
- Veteran
- Posts: 5553
- Joined: Thu Apr 05, 2018 5:15 pm
Re: The Boy Scout Rule
Doesn't matter, it's the same because (status == 0 && token == UNIT) will always return either true or false, instead of an if statement to choose which to return you can just do return status == 0 && token == UNIT
Re: The Boy Scout Rule
Ah ok, it is similar to write in python (I have copied the condition as in C++, in Python probably is slightly different, I have to check the subtle boolean AND and OR writings of Python)adrianinsaval wrote: ↑Sun Jun 26, 2022 5:20 pmDoesn't matter, it's the same because (status == 0 && token == UNIT) will always return either true or false, instead of an if statement to choose which to return you can just return the result.
Code: Select all
return True if (status == 0 && token == UNIT) else False
Regards
Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
-
- Posts: 176
- Joined: Wed Mar 09, 2022 3:15 am
- Location: Oz
Re: The Boy Scout Rule
onekk wrote: ↑Sun Jun 26, 2022 5:26 pm And the point of the exercise is to simplify it:Much easier to read and comprehend.Code: Select all
return status == 0 && token == UNIT;
Re: The Boy Scout Rule
Representing it's own style, your variant would at least throw a compile time erroropenBrain wrote: ↑Sun Jun 26, 2022 11:53 am Yes, when I see such a weird thing, I systematically replace withI don't like when code can be understood easily by kiddies.Code: Select all
return !functionThatReturnsBool() != false:false:true;
Cheers,
Markus
Markus
Re: The Boy Scout Rule
Sometimes Python and C++ are similar, and my very limited knowledge of C++ is not helping (Limited knowledge due to some Arduino fiddling, and is not real C++)FreddyFreddy wrote: ↑Sun Jun 26, 2022 5:30 pmonekk wrote: ↑Sun Jun 26, 2022 5:26 pm And the point of the exercise is to simplify it:Much easier to read and comprehend.Code: Select all
return status == 0 && token == UNIT;
my code was Python, I don't know if "ternary operators" Someone has told me they are called in this way are similar in C++, do you know why it is called ternary?
Good for my part to follow the "boy Scout Rule".
My two sons are "Scouts" and usually calling them "Boy Scouts" is considerate a joke here in Europe
But these considerations are clearly OT.
Probably I have to follow another rule "Think twice before writing a post!" or maybe "three of more times" but now "how to call this rule!" "common sense rule"?
Sorry for bothering.
Regards
Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- adrianinsaval
- Veteran
- Posts: 5553
- Joined: Thu Apr 05, 2018 5:15 pm
Re: The Boy Scout Rule
because it has three operands 1st if 2nd else 3rd note that in python a ternary operator is redundant too, you can also just do
Code: Select all
return status == 0 and token == UNIT
-
- Posts: 176
- Joined: Wed Mar 09, 2022 3:15 am
- Location: Oz
Re: The Boy Scout Rule
Three parts.
For the same example would be
Code: Select all
return status == 0 && token == UNIT ? true : false;
Re: The Boy Scout Rule
Thanks to all.
Regards
Carlo D.
Regards
Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.
Blog: https://okkmkblog.wordpress.com/
Re: The Boy Scout Rule
Also...
You need to be careful of "short circuit" evaluation. If the first term is sufficient to establish the return value, the second term is not evaluated.
This avoids dereferencing undefined pointers for example.
Eg
If (ptr == Null || ptr->status() == false) return false;
Matthew
You need to be careful of "short circuit" evaluation. If the first term is sufficient to establish the return value, the second term is not evaluated.
This avoids dereferencing undefined pointers for example.
Eg
If (ptr == Null || ptr->status() == false) return false;
Matthew