HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Class for handling unit conversions

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
handlingconversionsforclassunit

Problem

I am creating a class to be the end all defacto class for handling unit conversions. This is particularly difficult in my company's industry where imperial architectural strings are used as units. I wanted this class to be perfect in just about every way as it is the class that we will be giving our summer interns as the best example of how to write a class. By posting it here, I wanted to make sure that it is the best example!

The references to the DimensionConverter class is to a private class built just for this purpose. (I would like it reviewed as well... but I don't know how to post two classes appropriately here on Code Review.)

What else could I possibly improve about the way this class is written in the context of readability, ease of maintenance, and logical design?

```
namespace TypeLibrary
{
///
/// Class used for storing dimensions that may need to be accessed in a different measurement system
/// Will accept anything as input
///
/// decimal inches into AutoCAD notation
///
/// double inches = 14.1875
/// Dimension dm = new Dimension( inches, DimensionTypes.Inch);
///
/// Print(dm.Architectural)
///
/// ========Output==========
/// 1'2 3/16"
///
///
///
public class Dimension
{
#region internal variables

//internal Dimension type is set to milimeter to cause the least amount of rounding error when performing calculations.
const DimensionTypes internalUnitType = DimensionTypes.Millimeter;

//dimension value
private double internalDecimalUnit = 0.0;

#endregion

#region Constructors

///
/// Accepts any string value for input.
///
public Dimension(string passedArchitecturalString)
{
storeArchitecturalStringAsInternalUnit(passedArchitecturalString);
}

///
/// Accepts any type of value for input. This allows for less complexity in buildi

Solution

One obvious starting point: replace your code like this:

public static bool operator ==(Dimension d1, Dimension d2)
    {
        //compare the two dimensions for equality
        //return a bool based on their relative values
        if (d1.internalDecimalUnit == d2.internalDecimalUnit) 
        {
            return true;
        }
        else
        {
            return false;
        }


...with the much simpler, more readable:

return (d1.internalDecimalUnit == d2.internalDecimalUnit);

Code Snippets

public static bool operator ==(Dimension d1, Dimension d2)
    {
        //compare the two dimensions for equality
        //return a bool based on their relative values
        if (d1.internalDecimalUnit == d2.internalDecimalUnit) 
        {
            return true;
        }
        else
        {
            return false;
        }
return (d1.internalDecimalUnit == d2.internalDecimalUnit);

Context

StackExchange Code Review Q#46390, answer score: 7

Revisions (0)

No revisions yet.