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? Aug 3, 2018 at 2:32
  • So 1000 grams are one million kilograms in your world?
    – gnasher729
    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.
    – amon
    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. 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.