Live data from Hacker News

Choosing good names

marcelo-cure.blogspot.com

11–20 of 41 posts

Re: Choosing good names

#11
I would have called the function "calculateTransferTax". It's shorter while providing the same meaning.

Also, the "tax" argument is the tax _rate_. "tax" in this instance I would expect to mean the final amount. So I'd call that parameter "taxRate".

(And I'd use ints for everything, of course)

Re: Choosing good names

#12
In this case, I'd argue that the names are still bad for a couple of reasons.

Firstly, it's too verbose - a nice middle ground would have been better: calculateTransferTax(Double transferValue, Double tax). It's fairly clear what the tax is being calculated over.

Secondly, I'd say that "tax" is a bad name for the tax percentage since it doesn't describe that what actually is (the percentage / rate of tax), and since it's a Double, I'd expect it to be "0.2" for a 20% tax rather than 20.0 which appears to be what the method would actually expect.

Re: Choosing good names

#13

I would have called the function "calculateTransferTax". It's shorter while providing the same meaning. Also, the "tax" argument is the tax _rate_. "tax" in this instance I would expect to mean the final amount. So I'd call that parameter "taxRate". (And I'd use ints for everything, of course)

How would you use ints when there's division in the code? I've heard it's usually better to prefer ints to floating points, but in my limited experience I've found that it's easier said than done.

Re: Choosing good names

#14

I would have called the function "calculateTransferTax". It's shorter while providing the same meaning. Also, the "tax" argument is the tax _rate_. "tax" in this instance I would expect to mean the final amount. So I'd call that parameter "taxRate". (And I'd use ints for everything, of course)

How would you use ints when there's division in the code? I've heard it's usually better to prefer ints to floating points, but in my limited experience I've found that it's easier said than done.

Using floating point arithmetics for currency is just asking for trouble: http://stackoverflow.com/questions/3730019/why-not-use-doubl...

Re: Choosing good names

#15

You could provide even more clarity by using meaningful types like Money or TaxPercentage instead of Double. This would allow you to ensure only valid values are passed and would help avoid problematic floating point arithmetic :)

The general idea was to show a simple exemple. But I really appreciate your point. Thanks

I think money stuff is complicated and finicky enough to immediately make the example distractingly non-simple. In English, at least, taxes are usually charged and calculated on, not over things. Do you really mean transfer? Those often have fees rather than taxes. Or is it a Purchase? A Transaction? Isn't 'amount' clearer than 'transferValue?'. Is 'tax' supposed to be a percentage? A fraction? An absolute amount?

This sort of thing is probably why such examples are always blandly about cars and their colors and shapes and their areas.

Re: Choosing good names

#16

You could provide even more clarity by using meaningful types like Money or TaxPercentage instead of Double. This would allow you to ensure only valid values are passed and would help avoid problematic floating point arithmetic :)

Actually, I think using non standard types increases complexity, despite the more informative naming.

I would now have to look up the implementation of Money and TaxPercentage to be sure of what it does, whereas if you move the type names into the variable names like in the following examples: 'double total_money' and 'double tax_percentage' I would immediately understand what's going on.

I do see how a TaxPercentage type allows you to create a type that restricts itself to percentages. I feel that it is serves readability better to leave this checking to the function using the value itself rather than having the type do it.

Re: Choosing good names

#17

You could provide even more clarity by using meaningful types like Money or TaxPercentage instead of Double. This would allow you to ensure only valid values are passed and would help avoid problematic floating point arithmetic :)

I guess you mean typedefs and not separate classes for Money, TaxPercentage etc. I recently came across a codebase that implement type classes for almost all types of things it handles and found that to be overkill. Almost always, name and age can be represented by a string and an integer respectively; separate Name and Age classes just reduce the readability in your code.

In a language that supports it reasonably, the advantage of genuinely separate types is that the language will prevent you from doing stupid things like accidentally multiplying to monetary amounts or adding a percentage to a monetary amount.

Reasonable support here mostly means that you can implement operator overloads, so that you can define e.g. a Money type that can be added but not multiplied.

Money handling may not actually be such a good example for this, because you should really have proper unit testing there anyway. I did work on a codebase that had types for SI units, and it was quite a nice experience.

Re: Choosing good names

#18
The me the best convention is Linux kernel coding style. I prefer slightly more verbose variable and function names on interpreted languages, to compensate the lack of static type checking, but calculateTaxOverTransfer() is probably too much.

Local variable names should be short, and to the point. If you have random integer loop counter, it should probably be called "i". Calling it "loop_counter" is non-productive, if there is no chance of it being mis-understood. Similarly, "tmp" can be just about any type of variable that is used to hold a temporary value.

If you are afraid to mix up your local variable names, you have another problem, which is called the function-growth-hormone-imbalance syndrome. See chapter 6 (Functions).

Source: Linux kernel coding style - https://www.kernel.org/doc/Documentation/CodingStyle

Re: Choosing good names

#19
When it comes to code readability, and ultimate reusability, I believe renaming variables is where most time refactoring is best spent. Not only does it encourage better naming, but if the naming or paradigm model is off, it encourages better design.

Re: Choosing good names

#20

I would have called the function "calculateTransferTax". It's shorter while providing the same meaning. Also, the "tax" argument is the tax _rate_. "tax" in this instance I would expect to mean the final amount. So I'd call that parameter "taxRate". (And I'd use ints for everything, of course)

How would you use ints when there's division in the code? I've heard it's usually better to prefer ints to floating points, but in my limited experience I've found that it's easier said than done.

Either return cents (for decimal currencies), or more generally create a "currency" type with ints and conversion ratios for each "field" (for example a "US Dollar" type would have "dollars" and "cents", with ratios of "1" and "100". This means you can handle non-decimal currencies[1] like the Malagasy Ariary[2] and the Mauritanian Ouguiya[3] (1 ariary = 5 iraimbilanja, 1 ouguiya = 5 khoums)

As you said, easier said than done; but you really don't want FP errors popping up in financial calculations :-)

[1]: https://en.wikipedia.org/wiki/Non-decimal_currency

[2]: https://en.wikipedia.org/wiki/Malagasy_ariary

[3]: https://en.wikipedia.org/wiki/Mauritanian_ouguiya

Post reply on HN