coding style - Best way to write a conversion function -
let's i'm writing function convert between temperature scales. want support @ least celsius, fahrenheit, , kelvin. better pass source scale , target scale separate parameters of function, or sort of combined parameter?
example 1 - separate parameters: function converttemperature("celsius", "fahrenheit", 22)
example 2 - combined parameter: function converttemperature("c-f", 22)
the code inside function counts. 2 parameters, logic determine formula we're going use more complicated, single parameter doesn't feel right somehow.
thoughts?
go first option, rather allow literal strings (which error prone), take constant values or enumeration if language supports it, this:
converttemperature (tempscale.celsius, tempscale.fahrenheit, 22)
Comments
Post a Comment