Its quite common to require opposite pairs of functionality. Things like show/hide, or hide/unhide, or protect/unprotect, register/unregister, load/unload… you get the idea.
There are 2 basic approaches to this:
- have 1 function that takes a true/false parameter to tell it to do or undo the action
- have 2 clearly named functions whose names are good opposites, as above.
Which do you prefer?
I personally prefer the 2nd (I think) and I would say thats usually the way we interact with COM object models. I notice a lot of C code and Win API stuff uses the first though, so I thought about it a bit more.
I think the second (2 separate methods) is probably easier to understand, but on reflection the first is maybe more powerful. Or at least allows more concise code.
if msgb = vbyes then
to the more concise
menu.show(msgb = vbyes)
I guess the hard bit is getting a method name that makes sense with a true false parameter. Maybe a property gives the best of both worlds?
menu.visible = (msgb = vbyes)
What do you prefer? in your own code? in working with others (eg libraries)?