Proper naming query
i have naming problems with two of my functions
i've got a function
is_void
that returns true if the argument is "empty" (in some sense). How would you call an opposite function?isnt_void
?is_set
?is_not_void
?i have a pair of functions, the first one installs a handler to catch errors in the subsequent code and the second removes this handler.
install_error_handler/remove_error_handler
looks 开发者_C百科too long and ugly, i'd prefer a pair of short verbs (like watch/unwatch).
Any ideas are greatly appreciated.
Thanks for the answers so far
UPDATED: I need a function for "isn't void" because it's going to be used like someArray.map(is_not_void)
the second one cannot be simply "register" or "install", because it's used without arguments.
I'm not entirely sure why you need a different function for #1; if it's not void, then is_void would return false.
For #2, if you're passing an error handler into the method; Install/Uninstall should be sufficient.
In general, try to (1) minimize the number of interface functions and (2) provide precise and consistent names.
In the case of naming the not is_void()
case, the first principle tells us to not have the function exist at all since the user can always negate the boolean return. In other words, is_void()
is a constraint checking function, it is up to the caller to decide to explicitly negate the constraint as necessary.
The install_...
and remove_...
case is where the second principle applies. The problem with short verbs is that they are very ambiguous but it is not always possible to remove ambiguity through naming. What is most important is that they are as descriptive as possible and that you apply some consistent method. If you use watch_...
and unwatch_...
, use them consistently. Don't have watch_errors
alongside observe_warnings
. The inconsistency will cause users to look for reasons for the naming difference.
Personally, I prefer install_..._handler
and uninstall_..._handler
or add_observer
with a filter condition over watch_condition
and unwatch_condition
simply because unwatch does not look like a real word to my eyes.
The test functions are a good idea. However, I'd warn against using negative naming because that invites double (or more) negative logic if (not is_not_void(a))
. A simple not operator should suffice: if (not is_void(a))
.
From your update I assume you mean you need a pointer to the method. I always try to use names to be as clear as possible, so I would ask myself first what the meaning of is_void
is. From your text I would propose is_empty
, is_not_set
or is_nothing
instead of is_void
the opposites could be is_not_empty
, is_set
or is_something
, the last one sound a bit funny but would probably come close to the meaning in your code.
I don't think you have to guard against overly verbose naming, you have to watch out for overly terse naming as that often is a source for ambiguity.
As for the install_error_handler
and uninstall_error_handler
, if these names describe the purpose correctly I would keep them. If not try to describe what their meaning is in terms of your code (guard, protect, lock...)
To answer the first part of your question: as a rule, I avoid negatives in function names. Something like:
IsNotVoid( String s )
may seem harmless enough, but invariably, you're going to do something like
if ( ! IsNotVoid( s ) && ! IsNotVoid( t ) )
{
}
... which is going to make the next programmer think way too hard for not being able to immediately discern your intent.
精彩评论