QualityCode.com Essay: Question Every Parameter
One of my key coding philosophies is “Less is more”. One example is with function parameters. I recently inherited a subsystem that had a function that was defined something like this:
CMenu* BeginMenu(int x, int y, BOOL bIsSysMenu, DWORD dwFlags, HMENU hMenu, HWND hWndOwner, BOOL bMouse, char key, LPARAM lParam, BOOL bIsPopup);
Whew! Not only is it a lot of parameters, but several of them are only used under certain circumstances. For example, ‘key’ is only used if bMouse is FALSE, and ‘dwFlags’ is only used if bIsPopup is TRUE. Very confusing.
My solution in this case was to break the function into three functions, each of which has a parameter list that is specifically targeted at its needs. The BeginMouseMenu function, for example, does not have a ‘key’ parameter. One of the three functions has the ability to determine the correct value for bSysMenu without it being passed.
Even if these three functions end up calling an internal function like the original, at least clients of my subsystem don’t have to try to digest the “function from hell” just to use the functionality they need.
Passing fewer parameters keeps things simple. And especially in software, simple is good.