The way you have it now, You have two write NxN if
statements for each category that has N units.
if from_spinner is Degree
if to_spinner is Degree
return input
if to_spinner is Radian
return input * 0.0174532925199
if to_spinner is Gradian
return input * 1.11111111111
if from_spinner is Radian
if to_spinner is Degree
return input * 57.2957795131
if to_spinner is Radian
return input
if to_spinner is Gradian
return input * 63.6619772368
if from_spinner is Gradian
if to_spinner is Degree
return input * 0.9
if to_spinner is Radian
return input * 0.0157079632679
if to_spinner is Gradian
return input
Instead, choose a unit that will act as an intermediary between the input and the output. Then you need N if
statements to convert from input to intermediary, and N if
statements to convert from intermediary to output, for a total of 2N.
//we will use degrees as the intermediary unit
intermediary = null
//caluclate intermediary
if from_spinner is Degree
intermediary = input
if from_spinner is Radian
intermediary = input * 57.2957795131
if from_spinner is Gradian
intermediary = input * 0.9
//calculate final
if to_spinner is Degree
return intermediary
if to_spinner is Radian
return intermediary / 57.2957795131
if to_spinner is Gradian
return intermediary / 0.9
It doesn't seem like it's much more efficient when you have only three units, but for larger values of N, it saves you a lot of effort. For instance, compare this 105 line doubly nested method to its 29 line equivalent that uses intermediary values:
if from_spinner is Millimeter
if to_spinner is Millimeter
return input
if to_spinner is Centimeter
return input * 0.1
if to_spinner is Meter
return input * 0.001
if to_spinner is Kilometer
return input * 1e-06
if to_spinner is Inch
return input * 0.0393700787402
if to_spinner is Foot
return input * 0.00328083989501
if to_spinner is Mile
return input * 6.2137273665e-07
if from_spinner is Centimeter
if to_spinner is Millimeter
return input * 10.0
if to_spinner is Centimeter
return input
if to_spinner is Meter
return input * 0.01
if to_spinner is Kilometer
return input * 1e-05
if to_spinner is Inch
return input * 0.393700787402
if to_spinner is Foot
return input * 0.0328083989501
if to_spinner is Mile
return input * 6.2137273665e-06
if from_spinner is Meter
if to_spinner is Millimeter
return input * 1000.0
if to_spinner is Centimeter
return input * 100.0
if to_spinner is Meter
return input
if to_spinner is Kilometer
return input * 0.001
if to_spinner is Inch
return input * 39.3700787402
if to_spinner is Foot
return input * 3.28083989501
if to_spinner is Mile
return input * 0.00062137273665
if from_spinner is Kilometer
if to_spinner is Millimeter
return input * 1000000.0
if to_spinner is Centimeter
return input * 100000.0
if to_spinner is Meter
return input * 1000.0
if to_spinner is Kilometer
return input
if to_spinner is Inch
return input * 39370.0787402
if to_spinner is Foot
return input * 3280.83989501
if to_spinner is Mile
return input * 0.62137273665
if from_spinner is Inch
if to_spinner is Millimeter
return input * 25.4
if to_spinner is Centimeter
return input * 2.54
if to_spinner is Meter
return input * 0.0254
if to_spinner is Kilometer
return input * 2.54e-05
if to_spinner is Inch
return input
if to_spinner is Foot
return input * 0.0833333333333
if to_spinner is Mile
return input * 1.57828675109e-05
if from_spinner is Foot
if to_spinner is Millimeter
return input * 304.8
if to_spinner is Centimeter
return input * 30.48
if to_spinner is Meter
return input * 0.3048
if to_spinner is Kilometer
return input * 0.0003048
if to_spinner is Inch
return input * 12.0
if to_spinner is Foot
return input
if to_spinner is Mile
return input * 0.000189394410131
if from_spinner is Mile
if to_spinner is Millimeter
return input * 1609340.0
if to_spinner is Centimeter
return input * 160934.0
if to_spinner is Meter
return input * 1609.34
if to_spinner is Kilometer
return input * 1.60934
if to_spinner is Inch
return input * 63359.8425197
if to_spinner is Foot
return input * 5279.98687664
if to_spinner is Mile
return input
.
intermediary = null
if from_spinner is Millimeter
intermediary = input * 0.001
if from_spinner is Centimeter
intermediary = input * 0.01
if from_spinner is Meter
intermediary = input * 1.0
if from_spinner is Kilometer
intermediary = input * 1000.0
if from_spinner is Inch
intermediary = input * 0.0254
if from_spinner is Foot
intermediary = input * 0.3048
if from_spinner is Mile
intermediary = input * 1609.34
if to_spinner is Millimeter
return intermediary / 0.001
if to_spinner is Centimeter
return intermediary / 0.01
if to_spinner is Meter
return intermediary / 1.0
if to_spinner is Kilometer
return intermediary / 1000.0
if to_spinner is Inch
return intermediary / 0.0254
if to_spinner is Foot
return intermediary / 0.3048
if to_spinner is Mile
return intermediary / 1609.34