Skip to main content
added 85 characters in body
Source Link
Greg Burghardt
  • 46.1k
  • 8
  • 87
  • 150

In contrast to null's answer, defining a type for a "unit" can be beneficial if an integer isn't enough to describe the measurement. For instance, weight is often measured in multiple units within the same measurement system. Think "pounds" and "ounces" or "kilograms" and "grams".

If you need a more granular level of measurement defining a type for the unit is beneficial:

public struct Weight {
    private int pounds;
    private int ounces;

    public Weight(int pounds, int ounces) {
        // ...Value range checks go here
        // Throw exception if ounces is greater than 16?
    }

    // Getters go here
}

For something like "age" I recommend calculating that at run time based on the person's birth date:

public class Adult {
    private Date birthDate;

    public Interval getCurrentAge() {
        return calculateAge(Date.now());
    }

    public Interval calculateAge(Date date) {
        // Return interval between birthDate and date
    }
}

In contrast to null's answer, defining a type for a "unit" can be beneficial if an integer isn't enough to describe the measurement. For instance, weight is often measured in multiple units within the same measurement system. Think "pounds" and "ounces" or "kilograms" and "grams".

If you need a more granular level of measurement defining a type for the unit is beneficial:

public struct Weight {
    private int pounds;
    private int ounces;

    public Weight(int pounds, int ounces) {
        // ...
    }

    // Getters go here
}

For something like "age" I recommend calculating that at run time based on the person's birth date:

public class Adult {
    private Date birthDate;

    public Interval getCurrentAge() {
        return calculateAge(Date.now());
    }

    public Interval calculateAge(Date date) {
        // Return interval between birthDate and date
    }
}

In contrast to null's answer, defining a type for a "unit" can be beneficial if an integer isn't enough to describe the measurement. For instance, weight is often measured in multiple units within the same measurement system. Think "pounds" and "ounces" or "kilograms" and "grams".

If you need a more granular level of measurement defining a type for the unit is beneficial:

public struct Weight {
    private int pounds;
    private int ounces;

    public Weight(int pounds, int ounces) {
        // Value range checks go here
        // Throw exception if ounces is greater than 16?
    }

    // Getters go here
}

For something like "age" I recommend calculating that at run time based on the person's birth date:

public class Adult {
    private Date birthDate;

    public Interval getCurrentAge() {
        return calculateAge(Date.now());
    }

    public Interval calculateAge(Date date) {
        // Return interval between birthDate and date
    }
}
Source Link
Greg Burghardt
  • 46.1k
  • 8
  • 87
  • 150

In contrast to null's answer, defining a type for a "unit" can be beneficial if an integer isn't enough to describe the measurement. For instance, weight is often measured in multiple units within the same measurement system. Think "pounds" and "ounces" or "kilograms" and "grams".

If you need a more granular level of measurement defining a type for the unit is beneficial:

public struct Weight {
    private int pounds;
    private int ounces;

    public Weight(int pounds, int ounces) {
        // ...
    }

    // Getters go here
}

For something like "age" I recommend calculating that at run time based on the person's birth date:

public class Adult {
    private Date birthDate;

    public Interval getCurrentAge() {
        return calculateAge(Date.now());
    }

    public Interval calculateAge(Date date) {
        // Return interval between birthDate and date
    }
}