Hello,
I have this challenge from exercism : https://github.com/exercism/pharo-smalltalk/tree/master/exercises/clock and this function is given : hour: anInteger minute: anInteger2 self shouldBeImplemented Schould I make it on this class method work that the minutes will not exceed the 59 minutes So when for example when 70 min are given so hour:0 minute:70 it will be converted to hour: 1 minutes: 10 or can I better do this on the instance side. Regards, Roelof |
Administrator
|
On Sun, Aug 30, 2020 at 10:04 PM Roelof Wobben via Pharo-users <[hidden email]> wrote: Hello, I would argue against that approach. Make it a requirement that a given API must be used with correct values. e.g. #hours:minutes: requires hours between 00 and 23 and minutes between 00 and 59. If you want to convert equivalent time values, use a specific API. For example, many time implementations publicly define a seconds-based API, such as Time class>>#fromSeconds:. You can do the same with your implementation with a class-side #fromMinutes: method. (The corresponding instance methods would be #asSeconds and #asMinutes or something similar.) or can I better do this on the instance side. |
In reply to this post by Pharo Smalltalk Users mailing list
I have always found it annoying that the exercism specifications do not spell things out, so you HAVE to read the tests to find out what you are supposed to do, which pretty much spoils the point of them BEING tests. In this case, we note that there is a test case where the number of hours is 100, and it is to be accepted as meaning '04'. There is also a test case where the number of minutes is 160 and it is to be taken as 40 minutes plus 2 hours. So the only way you can pass the tests is something like class methods for: 'instance creation' hours: h minutes: m ^self new setMinutesSinceMidnight: (h * 60 + m bitOr: 0) \\ 1440 methods for: 'private' setMinutesSinceMidnight: m minutesSinceMidnight := m. methods for: 'accessing' hour ^minutesSinceMidnight // 60 minute ^minutesSinceMidnight \\ 60 The only *checking* that is involved is that the hour and minute counts must be integers, which I used " bitOr: 0" to do. What's the best way to do thisE? As for where to do checking in general, put it as close to the encapsulation boundary as you reasonably can. If there is a PUBLIC method for changing the time on a Clock, you need to put the checking in that method. If (as here) a clock value is supposed to be immutable, put it in the instance creation method. On Mon, 31 Aug 2020 at 18:18, Roelof Wobben via Pharo-users <[hidden email]> wrote: Hello, |
In reply to this post by Richard Sargent
Roelof explicitly said that this is for an Exercism exercise. That exercise defines the API (implicitly) in the test cases. Roelof has no choice about what the API is, his job is just to make the tests pass. Your ideas about how to design an API are good ones, it's just that Exercism says what to do. On Tue, 1 Sep 2020 at 05:01, Richard Sargent <[hidden email]> wrote:
|
Free forum by Nabble | Edit this page |