Style bits vs. Separate bool's
Posted
by
peterchen
on Programmers
See other posts from Programmers
or by peterchen
Published on 2012-09-25T06:31:11Z
Indexed on
2012/09/25
9:48 UTC
Read the original article
Hit count: 395
My main platform (WinAPI) still heavily uses bits for control styles etc. (example).
When introducing custom controls, I'm permanently wondering whether to follow that style or rather use individual bool's. Let's pit them against each other:
enum EMyCtrlStyles
{
mcsUseFileIcon = 1,
mcsTruncateFileName = 2,
mcsUseShellContextMenu = 4,
};
void SetStyle(DWORD mcsStyle);
void ModifyStyle(DWORD mcsRemove, DWORD mcsAdd);
DWORD GetStyle() const;
...
ctrl.SetStyle(mcsUseFileIcon | mcsUseShellContextMenu);
vs.
CMyCtrl & SetUseFileIcon(bool enable = true);
bool GetUseFileIcon() const;
CMyCtrl & SetTruncteFileName(bool enable = true);
bool GetTruncteFileName() const;
CMyCtrl & SetUseShellContextMenu(bool enable = true);
bool GetUseShellContextMenu() const;
ctrl.SetUseFileIcon().SetUseShellContextMenu();
As I see it,
Pro Style Bits
- Consistent with platform
- less library code (without gaining complexity), less places to modify for adding a new style
- less caller code (without losing notable readability)
- easier to use in some scenarios (e.g. remembering / transferring settings)
- Binary API remains stable if new style bits are introduced
Now, the first and the last are minor in most cases.
Pro Individual booleans
- Intellisense and refactoring tools reduce the "less typing" effort
- Single Purpose Entities
- more literate code (as in "flows more like a sentence")
- No change of paradim for non-bool properties
These sound more modern, but also "soft" advantages. I must admit the "platform consistency" is much more enticing than I could justify, the less code without losing much quality is a nice bonus.
1. What do you prefer? Subjectively, for writing the library, or for writing client code?
2. Any (semi-) objective statements, studies, etc.?
© Programmers or respective owner