I just have a general question on how to create efficient programs. For example, I made a unit converter program for Blackberry (The Ultimate Unit Converter if you want to check it out 🙂 )that converts between 5-7 different units. To code this, I did the brute-force way by simply creating methods for all possible units. So, for example, I created methods for all of the following: centimeters to inches, centimeters to meters, inches to centimeters, inches to meters, meters to centimeters, meters to inches, ETC.
My question is: Is there a more efficient way of coding this? Because it becomes a pain when I have to add in a unit, because then I have to create another set of methods.
Thank you! (Sorry if I sound stupid.)
Simple: just choose one “standard” unit (lets say SI units) and convert to it and from it.
For example, make a “inches to meters”, “meters to inches”, “centimeters to meters”, “meters to centimeters”, etc. To convert from inches to centimeters, simply call “inches to meters” and then “meters to centimeters”.
To add a new unit, you only need to add 2 methods: “NewUnit to meters” and “meters to NewUnit”.
Of course, its up to you how to organize your methods. You could have one method that takes units as arguments, for instance.