3 ms·
12. Use meaningful names for methods. The name must specify the exact action of the method and for most cases must start with a verb. (e.g. createPasswordHash)
by koningrobot 15y ago
12. Use meaningful names for methods. The name must specify the exact action of the method and for most cases must start with a verb. (e.g. createPasswordHash)
This depends. My classes often have side-effect-free methods that just return a value based on the state of the object. They are just properties the storage of which would be redundant. I never put "get" in front of the names of these methods. For instance, in a math Vector class, I don't have getNorm(), getUnit(), etc., I just have norm() and unit().
I struggle with what to name methods that compute, for instance, a dot product of two vectors. I don't like a.getDotProduct(b), nor a.dot(b) for that matter. The way the implicit parameter ("this") is special-cased in the notation makes everything so ugly. Nowadays I usually just have a class V with static methods that act on double[]s (so I end up with V.dot(a, b), which still sucks but at least isn't so confusing).
- TravisPe 15y agoI've recently been jumping around in my naming conventions. I'm a student and haven't quite settled in one one particular convention. For the current project I'm working on (C++) I've been using function overloading. So for example, I have a class called BankAccount. I have a private and a public method that will set the balance or return the balance (set is private, get is public) of the account, I've overloaded this so that if no parameter is passed it returns the balance, and if a parameter is passed it will set the balance accordingly. I use this in my deposit and withdrawal methods. I do the same thing for other setters/getters. I picked up this convention from seeing it used in other languages like Ruby and Javascript. Is this a bad practice? It seems logical to me, but I'm wonder what other peoples thoughts are and if it may be confusing to someone else.