Coding style: pass 1 or true to a function parameter defined as bool?

This is a matter of style, rather than correctness. I found the following code in

result = m.inverse (mattyp, info, rcond, 1, 1);

where the function prototype is defined in dMatrix.h as

  inverse (MatrixType& mattype, octave_idx_type& info, double& rcon,
           bool force = false, bool calc_cond = true) const;

In general, should we be passing a numeric value, 1, when the argument is of type bool or should we pass true?

My own preference is to pass true as this indicates the programmer took a look at the prototype and understood how the function was declared.

1 Like

yes, 1 in C or C++ is similar to true, when you’re passing it, means true.

By the function name and the parameters that receive, i supose that the function makes some inverse to some data structure and rcond is the condition that you are passing to it, calc_cond i think is a boolean to indicate if the conditions have to be considered or not.

In C++ it does not make a difference except in readability, but if editing an Octave script it is an order of magnitude slower to use false/true instead of 0/1.

>> foo = false; tic; for i = 1:1000000, foo = true; end; toc
Elapsed time is 1.7288 seconds.
Elapsed time is 1.73261 seconds.
Elapsed time is 1.76691 seconds.
Elapsed time is 1.72874 seconds.
Elapsed time is 1.72056 seconds.

>> foo = 0; tic; for i = 1:1000000, foo = 1; end; toc
Elapsed time is 0.171194 seconds.
Elapsed time is 0.168232 seconds.
Elapsed time is 0.168748 seconds.
Elapsed time is 0.168896 seconds.
Elapsed time is 0.168294 seconds.

To re-iterate from my original post, there is no functional difference or performance difference in C++. This is only a question of style in the code (hence, issue is tagged with “cpp-style”).

Which formulation of the code is going to be easier to understand for programmers who have to read, understand, and potentially debug this code several years from now?

I would use true and false now. I’m fairly certain that the original code used int for these parameters because it was written before bool was added to C++ and I never bothered to try to use an enum or some other method of defining true and false constants.