R
Rhino
I was trying to solve a simple problem yesterday when I found myself
wondering about the best design approach for the problem.
I was trying to convert an arbitrary number of seconds into a number of
hours, minutes and seconds. For example, given 3661 seconds, I wanted to
determine that this was 1 hour, 1 minute, and 1 second. Initially, I wrote
this as a static method and returned an int array containing the number of
hours, the number of minutes, and the number of seconds, in that order.
I wasn't wild about passing back an array where the program using it would
just have to know that result was in that order so I thought it might be
better to make a class called HoursMinutesSeconds where the constructor was
HoursMinutesSeconds(int inputSeconds) which calculated the number of hours,
minutes, and seconds for a given value of inputSeconds, then stored those
values in class variables which were made accessible to the outside world
via getters. Then, the user could do this and not have to worry about the
sequence of the values:
HoursMinutesSeconds hms = new HoursMinutesSeconds(3661);
int hours = hsm.getHours();
int minutes = hsm.getMinutes();
int seconds = hsm.getSeconds();
Would you agree that this is a better approach or should I be using a method
instead of a class for this job?
Also, this raised a very interesting followup question. In the real world,
negative seconds don't make a whole lot of sense so it occurred to me that I
might want to prevent any instantation of my HoursMinutesSeconds class if
the input value was a negative number.
Is it reasonable to put conditions like this on a class like
HoursMinutesSeconds? If so, how should I accomplish this?
My first thought was to inspect the value of 'inputSeconds' in the
constructor for the class and if the value was negative, thrown an Exception
of some kind, maybe a 'homegrown' one like
'HoursMinutesSecondsInstantiationException'. However, I don't recall ever
seeing a constructor throw an exception and I vaguely recall reading a
newsgroup post several years back where someone stated categorically that
constructors should never throw exceptions. I may be misremembering that
post though.
If it is reasonable to prevent a constructor from instantiating itself under
certain conditions, what is the best way to accomplish that? I'm inclined to
think that the work should happen outside the class, not in its constructor.
For example, given a method that was supposed to return a number of hours,
minutes, and seconds given a number of seconds via the HoursMinutesSeconds
class, I suspect the best way to prevent instantion when inputSeconds is
negative is something like this:
private void convert() {
int inputSeconds = getSeconds();
if (inputSeconds < 0) {
System.err.println("Number of seconds is negative.");
System.exit(16);
} else {
HoursMinutesSeconds hms = new HoursMinutesSeconds(inputSeconds);
System.out.println("Hours = " + hms.getHours());
System.out.println("Minutes = " + hms.getMinutes());
System.out.println("Seconds = " + hms.getSeconds());
}
So, which is my best approach: prevent instantiation outside the object
itself if the resulting object wouldn't make sense or let the object itself
realize that it wouldn't make sense and abort its instantiation within the
constructor? Or is the whole question nonsense because I shouldn't be using
a class for this purpose in the first place?
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
wondering about the best design approach for the problem.
I was trying to convert an arbitrary number of seconds into a number of
hours, minutes and seconds. For example, given 3661 seconds, I wanted to
determine that this was 1 hour, 1 minute, and 1 second. Initially, I wrote
this as a static method and returned an int array containing the number of
hours, the number of minutes, and the number of seconds, in that order.
I wasn't wild about passing back an array where the program using it would
just have to know that result was in that order so I thought it might be
better to make a class called HoursMinutesSeconds where the constructor was
HoursMinutesSeconds(int inputSeconds) which calculated the number of hours,
minutes, and seconds for a given value of inputSeconds, then stored those
values in class variables which were made accessible to the outside world
via getters. Then, the user could do this and not have to worry about the
sequence of the values:
HoursMinutesSeconds hms = new HoursMinutesSeconds(3661);
int hours = hsm.getHours();
int minutes = hsm.getMinutes();
int seconds = hsm.getSeconds();
Would you agree that this is a better approach or should I be using a method
instead of a class for this job?
Also, this raised a very interesting followup question. In the real world,
negative seconds don't make a whole lot of sense so it occurred to me that I
might want to prevent any instantation of my HoursMinutesSeconds class if
the input value was a negative number.
Is it reasonable to put conditions like this on a class like
HoursMinutesSeconds? If so, how should I accomplish this?
My first thought was to inspect the value of 'inputSeconds' in the
constructor for the class and if the value was negative, thrown an Exception
of some kind, maybe a 'homegrown' one like
'HoursMinutesSecondsInstantiationException'. However, I don't recall ever
seeing a constructor throw an exception and I vaguely recall reading a
newsgroup post several years back where someone stated categorically that
constructors should never throw exceptions. I may be misremembering that
post though.
If it is reasonable to prevent a constructor from instantiating itself under
certain conditions, what is the best way to accomplish that? I'm inclined to
think that the work should happen outside the class, not in its constructor.
For example, given a method that was supposed to return a number of hours,
minutes, and seconds given a number of seconds via the HoursMinutesSeconds
class, I suspect the best way to prevent instantion when inputSeconds is
negative is something like this:
private void convert() {
int inputSeconds = getSeconds();
if (inputSeconds < 0) {
System.err.println("Number of seconds is negative.");
System.exit(16);
} else {
HoursMinutesSeconds hms = new HoursMinutesSeconds(inputSeconds);
System.out.println("Hours = " + hms.getHours());
System.out.println("Minutes = " + hms.getMinutes());
System.out.println("Seconds = " + hms.getSeconds());
}
So, which is my best approach: prevent instantiation outside the object
itself if the resulting object wouldn't make sense or let the object itself
realize that it wouldn't make sense and abort its instantiation within the
constructor? Or is the whole question nonsense because I shouldn't be using
a class for this purpose in the first place?
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare