The Boy Scout Rule

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: The Boy Scout Rule

Post by onekk »

Post amended due to the explanation of @adrianinsaval below.

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/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: The Boy Scout Rule

Post by adrianinsaval »

onekk wrote: Sun Jun 26, 2022 4:45 pm because status could be different from 0 and token could be different from the value in UNIT.
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
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: The Boy Scout Rule

Post by onekk »

adrianinsaval wrote: Sun Jun 26, 2022 5:20 pm
onekk wrote: Sun Jun 26, 2022 4:45 pm because status could be different from 0 and token could be different from the value in UNIT.
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 return the result.
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)

Code: Select all

return True if (status == 0 && token == UNIT) else False
Ok I've guessed the point, thanks for correcting me!

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/
FreddyFreddy
Posts: 176
Joined: Wed Mar 09, 2022 3:15 am
Location: Oz

Re: The Boy Scout Rule

Post by FreddyFreddy »

onekk wrote: Sun Jun 26, 2022 5:26 pm And the point of the exercise is to simplify it:

Code: Select all

return status == 0 && token == UNIT;
Much easier to read and comprehend.
User avatar
mfro
Posts: 663
Joined: Sat Sep 23, 2017 8:15 am

Re: The Boy Scout Rule

Post by mfro »

openBrain wrote: Sun Jun 26, 2022 11:53 am Yes, when I see such a weird thing, I systematically replace with

Code: Select all

return !functionThatReturnsBool() != false:false:true;
I don't like when code can be understood easily by kiddies. :mrgreen: :mrgreen: :mrgreen:
Representing it's own style, your variant would at least throw a compile time error :lol:
Cheers,
Markus
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: The Boy Scout Rule

Post by onekk »

FreddyFreddy wrote: Sun Jun 26, 2022 5:30 pm
onekk wrote: Sun Jun 26, 2022 5:26 pm And the point of the exercise is to simplify it:

Code: Select all

return status == 0 && token == UNIT;
Much easier to read and comprehend.
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++)

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 :-D

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"? :lol:

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/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: The Boy Scout Rule

Post by adrianinsaval »

onekk wrote: Sun Jun 26, 2022 5:42 pm do you know why it is called ternary?
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
FreddyFreddy
Posts: 176
Joined: Wed Mar 09, 2022 3:15 am
Location: Oz

Re: The Boy Scout Rule

Post by FreddyFreddy »

onekk wrote: Sun Jun 26, 2022 5:42 pm 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?
Three parts.

For the same example would be

Code: Select all

return status == 0 && token == UNIT ? true : false; 
but that would also be unnecesarily complex.
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: The Boy Scout Rule

Post by onekk »

Thanks to all.

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/
drmclem
Posts: 11
Joined: Sun May 21, 2017 5:31 pm

Re: The Boy Scout Rule

Post by drmclem »

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
Post Reply