3

For example, to convert between g and kg, I have a constant 1000:

public static final float G_TO_KG=1000; . . . this.result = someResult*1000; 

I found G_TO_KG always bind to operator '*'. So my question is, is it better to define a static method:

public static float gToKg(result){ return result*1000; } 

So that I would not use wrong operator, also don't need to repeat the operator each time?

4
  • 1
    You want to write an entire method to save one character? Commented Aug 3, 2018 at 2:32
  • So 1000 grams are one million kilograms in your world? Commented Aug 3, 2018 at 6:54
  • 1
    @RobertHarvey it's not about saving some typing, it's about eliminating a possible source of errors: Do I multiply or divide here? In fact, OP's example contains exactly one such error – with a function, it can be fixed in one place. Commented Aug 3, 2018 at 7:16
  • 1
    @amon: My question was rhetorical. See the OP's edit, though I'm still not convinced of his revised reasoning. Commented Aug 3, 2018 at 14:50

1 Answer 1

4

If you mean to define a transformation, you should definitely make a function rather than a number.

I would even say your original name is wrong, it should rather be G_IN_KG

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.