4 ms·
Is ariels's critique valid? Is that best practice for functions returning a boolean? What about applying the same logic to a function returning a number betwee
by Kiro 12y ago
Is ariels's critique valid? Is that best practice for functions returning a boolean?
What about applying the same logic to a function returning a number between X and Y?
if (random_number_between(1, 10) > 5) {
do_something();
}
Is this correct? I would personally name such functions get_random_number_between instead but that violates this principle.
- jschwartzi 12y agoIt matters less what the convention is than that there is a convention in your codebase and that it makes clear what the affirmative case is.
- rab_oof 12y agoYup. It's important to have functioning code that mere mortals can understand most easily. Because the code represents system behavior and people need unambiguous conventions to reduce misunderstanding and prevent introduction of errors along the way.
- NickPollard 12y ago'best practice' is a difficult concept when it comes to naming things, which is one of the two hard problems of computer science. However, I agree with ariel. In a functional-style program, or indeed in most programs, the important thing about a function is what it returns, not what it does. Calling a function 'getRandomNumber' describes the action of the function, not the return value, which is just 'randomNumber'. As your example shows, the logic in English flows much more naturally with the latter. It's much better to talk about the box.size, rather than the box.getSize, for example. To me, the presence of 'get' in a function name is almost always a bad idea. Similarly, I normally use the word 'as' rather than the word 'to' when naming conversion functions, e.g. list.asArray, rather than list.toArray
- laumars 12y agoYou make some valid points, but what about instances where you have a getter and a setter?
- mnx 12y agoSome languages provide you with a mechanism to deal with that (c#s properties), and many others allow you to get a similar result with function overloading. So you don't have getX() and setX(), you have X(), and X(int x)
- laumars 12y agoWhile function overloading is a neat trick, I think it's a step backwards in terms of code readability. In those instances, it's only clear from the usage rather than the name how that function behaves, so I think I'd rather see getX and setX over X() and X(int x) and be absolutely clear about the role of that function.
- NickPollard 12y agoWith setters, you're implicitly working in a mutable world, at which point it makes sense to name it for the action, so I think you have to use 'set'. So I would have x() and setX(), instead of getX() and setX(). In an immutable world where you're making a copy (perhaps even through a lens), then I often use 'with', e.g. myNewObject = myObject.withX(12.0).
- laumars 12y agoInteresting. I guess on many occasions, the get/set could also be avoided by thinking about the usage a little deeper. Take HTTP frameworks for example: getHeader() setHeader(key, val string) could also be defined as: requestHeader() responseHeader(key, val string) I must admit I'm someone who, despite best efforts to give meaningful and consistent function names, often ends up with something less standardised by the time projects start to grow, deadlines loom and fatigue sets in (I'd imagine this is probably true for most people - if we're completely honest). But it's always good to get (if you pardon my pun) reminders and advice about sane naming conventions.
- jimmaswell 12y agogetX and setX are the standard Java conventions. If you're working in Java it's probably best to follow it.
- nl 12y agonaming things, which is one of the two hard problems of computer science. To expand on this, the two hard things in computer science are: 1) Naming Things 2) Cache Invalidation 3) Off-By-One Errors
- claudius 12y agoI always assumed the two hardest, uh, things in CS are off-by- concurrencyone errors, naming conventions and.
- kbart 12y agoPersonally, I don't agree with that because it makes things harder to read. I always use is_something (or isSomething, depending on the style used) form for functions that do some check and return a boolean value. It's really easy to notice such functions when reading code and understand their intend. Proposed computer_on_fire() is by far the worst example, the intention of such function is not clear by it's name and I would have to look it's definition/description to understand what it does. TL;DR using prefixes like is, set, get makes code much more readable. EDIT: for your random number function I'd simply use get_random_number(x, y) too.