Coding Conventions: Boolean Function Parameters.
Here is a coding convention that I have recently adopted and wish I would have adopted earlier. I would recommend it to all without exception. It is:
Use enumerations instead of boolean values as parameters to functions when there is more than one parameter.
For example these are okay:
Show(bool value);
Setup(ShowState showState, Orientation orientation, CollisionState collisionState);
These are not okay:
Setup(bool showOnStart, bool orientLeft, bool enableCollision);
Serialize(string filename, bool save);
Consider reading:
Setup(true, false, true);
Serialize(“myfile.bin”, true);
Vs. something like this:
Setup(ShowState::Show, Orientation::Right, CollisionState::Enabled)
Serialize(“myfile.bin”, SerializeOperation::Save);
Another advantage is that this opens things up later if you decide to add in more than one state for a variable.
For example, let’s say we wanted to add “Forward” and “Backward” to Orientation, or a “LoadAndWriteBack” to Serialize.
So there you go. A little gem to make your code better and your life easier.
Love the advice. Thank you.