Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to write a conversion function

Tags:

coding-style

Let's say that I'm writing a function to convert between temperature scales. I want to support at least Celsius, Fahrenheit, and Kelvin. Is it better to pass the source scale and target scale as separate parameters of the function, or some 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 the function is probably where it counts. With two parameters, the logic to determine what formula we're going to use is slightly more complicated, but a single parameter doesn't feel right somehow.

Thoughts?

like image 226
Scott Avatar asked Sep 12 '08 14:09

Scott


People also ask

What is a conversion function?

You can define a member function of a class, called a conversion function, that converts from the type of its class to another specified type.

What is a conversion function How is it created explain its syntax?

Conversion function syntaxConversion functions have no arguments, and the return type is implicitly the conversion type. Conversion functions can be inherited. You can have virtual conversion functions but not static ones. Parent topic: User-defined conversions (C++ only)

What are the conversion functions in SQL?

SQL Conversion functions are single row functions which are capable of typecasting column value, literal or an expression . TO_CHAR, TO_NUMBER and TO_DATE are the three functions which perform cross modification of data types.


4 Answers

Go with the first option, but rather than allow literal strings (which are error prone), take constant values or an enumeration if your language supports it, like this:

convertTemperature (TempScale.CELSIUS, TempScale.FAHRENHEIT, 22)
like image 121
jodonnell Avatar answered Oct 10 '22 12:10

jodonnell


Depends on the language.

Generally, I'd use separate arguments with enums.

If it's an object oriented language, then I'd recommend a temperature class, with the temperature stored internally however you like and then functions to output it in whatever units are needed:

temp.celsius(); // returns the temperature of object temp in celsius

like image 37
Adam Davis Avatar answered Oct 10 '22 11:10

Adam Davis


When writing such designs, I like to think to myself, "If I needed to add an extra unit, what would design would make it the easiest to do so?" Doing this, I come to the conclusion that enums would be easiest for the following reasons:

1) Adding new values is easy. 2) I avoid doing string comparison

However, how do you write the conversion method? 3p2 is 6. So that means there are 6 different combinations of celsius, Fahrenheit, and kelvin. What if I wanted to add a new temperate format "foo"? That would mean 4p2 which is 12! Two more? 5p2 = 20 combination. Three more? 6p2 = 30 combinations!

You can quickly see how each additional modification requires more and more changes to the code. For this reason I don't do direct conversions! Instead, I do an intermediate conversion. I'd pick one temperature, say Kelvin. And initially, I'd convert to kelvin. I'd then convert kelvin to the desired temperature. Yes, It does result in an extra calculation. However, it makes scalling the code a ton easier. adding adding a new temperature unit will always result in only two new modifications to the code. Easy.

like image 43
Rob Rolnick Avatar answered Oct 10 '22 12:10

Rob Rolnick


A few things:

  • I'd use an enumerated type that a syntax checker or compiler can check rather than a string that can be mistyped. In Pseudo-PHP:

    define ('kCelsius', 0); define ('kFarenheit', 1); define ('kKelvin', 2); $a = ConvertTemperature(22, kCelsius, kFarenheit);

Also, it seems more natural to me to place the thing you operate on, in this case the temperature to be converted, first. It gives a logical ordering to your parameters (convert -- what? from? to?) and thus helps with mnemonics.

like image 35
millenomi Avatar answered Oct 10 '22 12:10

millenomi