Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

C++ Refactoring: Default booleans and pointers

This is another recount of an experience I had refactoring some C++ code recently.  In some ways this is also a follow up of my previous post about why you shouldn't use boolean parameters (especially default value ones).

I recently refactored a large set of API's in our code base to remove a common parameter.  It was a member variable that was constantly being passed as an argument.  For many reasons we decide that this was unacceptable and it was better to use the member variable for instance methods and if needed define static methods which took the parameter as a value. 

After making the switch I recompiled and starting running regression suites on the code and found that a whole set were failing.  It turns out that many of our methods had the following pattern.

class Student;

int CalculateValue(Student *pStudent, Manager *pManager, bool cache=false);

The parameter I was removing was "pManager".  So now we have a function that used to take 2 or 3 arguments and now takes 1 or 2 arguments.  Unfortunately, now all of the places that used to call this function with 2 arguments will now recompile without warning since pointers are implicitly convertible to booleans. 

No matter.  Redefined the method with the same arguments and did not specify an implementation.  This caused compiler errors where necessary and allowed me to fixup the calling code. 

Boolean parameters are troublesome.  Defaulted value boolean parameters are much more so.

Yes you could attribute this to the fact that I didn't fully think through a subset of my changes and that's certainly fair.  However if we practiced good coding standards up front, this could have been avoided.  If we had originally done any of the following the code would be more resilient to changes.

  1. Chosen an enum instead of a boolean parameter
  2. Forced the caller to specify the boolean parameter by not providing a default

Published Monday, October 29, 2007 12:36 PM by Jared Parsons

Filed under: ,

Comments

# MSDN Blog Postings » C++ Refactoring: Default booleans and pointers @ Monday, October 29, 2007 2:49 PM

PingBack from http://msdnrss.thecoderblogs.com/2007/10/29/c-refactoring-default-booleans-and-pointers/

MSDN Blog Postings » C++ Refactoring: Default booleans and pointers

New Comments to this post are disabled
Page view tracker