--- Begin Message ---
Hello Kasper
Thanks for the feedback.
I also like readable and easy to understand code but I get the feeling that it;s good practice to use as minimal as possible if then's
So I wonder if and how I can achieve that here.
Roelof
Op 27-12-2019 om 20:47 schreef Kasper Østerbye:
Hi
First, I think your code is readable and easy to understand.
I think the problem does not lend itself too much towards having “an object oriented solution”, as such.
If you are looking for a one-liner using smalltalk libraries, you could do:({10 -> 0. 5 -> 1. 1 -> 5. 0 -> 10} detect: [ :dist | dist key < distance ]) value
but I really think your code is easier to read, which is what I appreciate the most.Best,
Kasper
On 27 December 2019 at 20.18.38, Roelof Wobben via Pharo-users (pharo-users@lists.pharo.org) wrote:
Hello,
Im trying to solve a challenge from exercism where I have to calculate the points somehow gets on a very simple darts board.
I solved it like this :
scoreX: anInteger y: anInteger2
| distance |
distance := (anInteger squared + anInteger2 squared) sqrt.
distance > 10
ifTrue: [ ^ 0 ].
distance > 5
ifTrue: [ ^ 1 ].
distance > 1
ifTrue: [ ^ 5 ].
^ 10
but now I use three if then and I think it's ugly code.
Is there a way I can make it more the smalltalk way ?
Regards,
Roelof
--- End Message ---
Re: [Pharo-users] can I write this without the three if then;s
Roelof Wobben via Pharo-users Fri, 27 Dec 2019 11:53:45 -0800
- [Pharo-users] can I write this without the t... Roelof Wobben via Pharo-users
- Re: [Pharo-users] can I write this with... tbrunz
- Re: [Pharo-users] can I write this ... Roelof Wobben via Pharo-users
- Re: [Pharo-users] can I write t... tbrunz
- Re: [Pharo-users] can I wri... tbrunz
- Re: [Pharo-users] can ... tbrunz
- Re: [Pharo-users] can I write this with... Kasper Østerbye
- Re: [Pharo-users] can I write this ... Roelof Wobben via Pharo-users
- Re: [Pharo-users] can I write this with... Richard O'Keefe
- Re: [Pharo-users] can I write this ... Roelof Wobben via Pharo-users
- Re: [Pharo-users] can I write this with... Dennis Schetinin
- Re: [Pharo-users] can I write this ... Esteban Lorenzano
- Re: [Pharo-users] can I write this ... Richard O'Keefe
- Re: [Pharo-users] can I write this with... Richard Sargent
- Re: [Pharo-users] can I write this with... ponyatov