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?

ocomfd
  • 5,652
  • 8
  • 29
  • 37
  • 1
    You want to write an entire method to save one character? – Robert Harvey Aug 03 '18 at 02:32
  • So 1000 grams are one million kilograms in your world? – gnasher729 Aug 03 '18 at 06: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 03 '18 at 07:16
  • 1
    @amon: My question was rhetorical. See the OP's edit, though I'm still not convinced of his revised reasoning. – Robert Harvey Aug 03 '18 at 14:50

1 Answers1

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

max630
  • 2,543
  • 1
  • 11
  • 15