Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / C++
Tip/Trick

Never Abuse Boolean to be Your 2 Value Type

Rate me:
Please Sign up or sign in to vote.
4.33/5 (2 votes)
24 Dec 2018CPOL1 min read 5.8K   2
Use an enum instead

Introduction

In my OpenGL Video Encoder, the constructor for H264Writer used to be like this before refactoring, the boolean parameter is to indicate to use HEVC codec. What is not apparent is when it is set to false, what codec is used? H264 or WMV?

C++
H264Writer(const wchar_t* mp3_file, const wchar_t* src_file, 
           const wchar_t* dest_file, bool hevc, 
		   UINT32 bitrate = 4000000);

To remedy this for clarity, I introduced an enum called VideoCodec to replace the boolean. It also make it easy for expansion when I add a third or fourth codec in future.

C++
enum class VideoCodec
{
	H264,
	HEVC
};
H264Writer(const wchar_t* mp3_file, const wchar_t* src_file, 
           const wchar_t* dest_file, VideoCodec codec, 
		   UINT32 bitrate = 4000000);

In EnumVideoEncoder function, I did the same refactoring. When calling the function, which is more clear in its intent to the code reader?

C++
if (H264Writer::EnumVideoEncoder(encoders, Processing::Software, VideoCodec::H264))

Or this?

C++
if (H264Writer::EnumVideoEncoder(encoders, false, false)

Clearly, the winner is the first one. Never abuse boolean to be your 2 value type, use an enum instead.

In my Outline Text Library, in the TextGradOutlineStrategy class's Init method, the last parameter is another example of abusing boolean. It is not clear to anyone except the original owner what gradient type is used for when the SinusoidGradient is false.

C++
void Init(
    Gdiplus::Color clrText, 
    Gdiplus::Color clrOutline1, 
    Gdiplus::Color clrOutline2, 
    int nThickness,
    bool SinusoidGradient);

To fix it, a GradientType enum is added to replace the boolean.

C++
enum class GradientType
{
	Linear,
	Sinusoid
};

void Init(
    Gdiplus::Color clrText, 
    Gdiplus::Color clrOutline1, 
    Gdiplus::Color clrOutline2, 
    int nThickness,
    GradientType gradType);

Likewise, you should not use a boolean type to represent a gender type. You get my point by now. Even a well-established software behemoth like Microsoft committed the same mistake with MFC CFileDialog. I always had a hard time in reading a CFileDialog whether it is OpenFileDialog or SaveFileDialog without first looking it up in MSDN.

C++
// CFileDialog constructor.
CFileDialog(BOOL bOpenFileDialog,
    LPCTSTR lpszDefExt = NULL,
    LPCTSTR lpszFileName = NULL,
    DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    LPCTSTR lpszFilter = NULL,
    CWnd* pParentWnd = NULL );
...
// To use CFileDialog
CFileDialog dlg(FALSE); // save file dialog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Shao Voon is from Singapore. His interest lies primarily in computer graphics, software optimization, concurrency, security, and Agile methodologies.

In recent years, he shifted focus to software safety research. His hobby is writing a free C++ DirectX photo slideshow application which can be viewed here.

Comments and Discussions

 
GeneralWell, ENUMS are good, but .. Pin
asiwel26-Dec-18 7:40
professionalasiwel26-Dec-18 7:40 
GeneralRe: Well, ENUMS are good, but .. Pin
Shao Voon Wong15-Apr-19 20:00
mvaShao Voon Wong15-Apr-19 20:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.