sometimes we need to create simple method with just one parameter. For example:
private boolean isMockedPlayer(Player player) {
return player == Player.MOCKED;
}
So, my question is: wouldn't it be better to name method: isMocked(Player player) and not repeate word "Player" so many times? Any official guidance? I realize that such repeat is essential for setters, but not sure for other cases like above.
Edited: Well, an example probably wasn't the best. But the question is not about this code snippet, but about general principle - should we repeat parameter type in method name or not?
Why not have it on the
Playerinstance itself?player.isMocked()seems more natural thansomeOtherService.isMockedPlayer(player).It does kind of defeat the purpose though if
Playeris anenum, because thenMOCKEDis public.On the other hand, if
Playeris an actual class and ifMOCKEDis an internal static instance that you maintain, then you can implementisMocked()as above and you won't even have to exposeMOCKEDto the outside world.Well, it depends. Choose a naming convention that is at the very least:
You want to clearly convey the context and meaning of the operation that you are performing.
I think your example itself is kind of redundant because you're checking the state of an instance. So in this scenario, it makes more sense for the method to be part of the instance itself. But a better example is perhaps a method on an class that accepts a parameter that is the same type as the class itself. For example:
In this case there is no need to call the method
mergeBinarySearchTree;bst.mergeBinarySearchTree(other)doesn't convey any more information thanbst.merge(other). But even then, you can't use this as a hard and fast rule. Perhaps your object has many different kinds ofmergeoperations that all accept different things. In that case it might make sense to have the method include the name of the thing being merged in... but that could also depend on how you have designed your object model.tl; dr It depends; but in general, choose a name that conveys accurate information about the semantics and context of the operation without being needlessly redundant or verbose.