Re: [Pharo-users] How cn I improve this code

2019-03-24 Thread Richard O'Keefe
Four blindingly obvious remarks.
(1) Where are the comments?  What problem is this actually solving?
Where is the specification?  Oh, it's at the *end*, not actually
in the Team class.

(2) name: anObject
  name := anObject
and way too many methods like it.
(a) Saying "anObject" is maximally unhelpful.  It's even worse than "s"
because that might be a clue that it is a string or symbol.  You
might want to read the (now free) book "Smalltalk with Style".
(b) I dislike "setter" methods that let anyone poke absolutely anything
into your object.  For example, supposing that name should be a
string
-- which is the kind of information that should go in a class
comment --
I tend to write methods like this as
name: aString
  name := aString asString.
which either puts a string into name or dies trying.
In fact, I would have an instance cra
(3) When I saw "matchplayed matchtied matchlost" I initially expected them
to be Boolean.  It would really help if you had a class comment saying
what they are (and what they mean) and/or an 'initialize' method that
sets them to 0.  Furthermore, these things are counts, so in English
would have to be matchesPlayed, matchesTied, matchesWon, matchesLost.
But even that is misleading; you might think they were containers
holding Match objects.  So I would suggest
  winCount lossCount tieCount
and
  totalCount
^winCount + lossCount + tieCount
making it *impossible* for totalCount to disagree with the others.
(4) Code like
aTeam matchwon: aTeam matchwon + 1.
aTeam points: aTeam points + 3.
really doesn't treat aTeam like an object.
We note that there are
   3 points for a win
   1 point  for a tie
   0 points for a loss
and realise that we can do
  totalPoints
^winCount * 3 + tieCount
  gotWin
winCount := winCount + 1.
and now we have
  aTeam gotWin.

The rule here is "if you want an object to change state, ASK THE OBJECT TO
DO IT."



On Sun, 24 Mar 2019 at 05:12, Roelof Wobben  wrote:

> Hello,
>
> As a challenge I had to write some code that makes ranks from a
> competition.
> Im still busy trying to find out how to model this problem.
>
> Right now I have made the code I included and I wonder how I can improve
> this code before I go on with this way.
>
> Roelof
>
>


Re: [Pharo-users] How cn I improve this code

2019-03-23 Thread Sean P. DeNigris
Roelof Wobben wrote
> how I can improve 
> this code before I go on with this way.

You might get better feedback hosting the code somewhere. Ideally on GH or
similar with a load script.



-
Cheers,
Sean
--
Sent from: http://forum.world.st/Pharo-Smalltalk-Users-f1310670.html



[Pharo-users] How cn I improve this code

2019-03-23 Thread Roelof Wobben

Hello,

As a challenge I had to write some code that makes ranks from a 
competition.

Im still busy trying to find out how to model this problem.

Right now I have made the code I included and I wonder how I can improve 
this code before I go on with this way.


Roelof

Object subclass: #Team
instanceVariableNames: 'name matchplayed matchtied matchlost points 
matchwon'
classVariableNames: ''
poolDictionaries: ''
category: 'Exercism-Tournament'!

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
matchtied: anObject
matchtied := anObject! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/23/2019 11:14'!
matchwon: anObject
matchwon := anObject! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
matchplayed: anObject
matchplayed := anObject! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
name
^ name! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
matchlost
^ matchlost! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
matchplayed
^ matchplayed! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
points: anObject
points := anObject! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
matchlost: anObject
matchlost := anObject! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
matchtied
^ matchtied! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
name: anObject
name := anObject! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/22/2019 15:58'!
points
^ points! !

!Team methodsFor: 'accessing' stamp: 'RoelofWobben 3/23/2019 11:14'!
matchwon
^ matchwon! !

"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!

Team class
instanceVariableNames: ''!

!Team class methodsFor: 'as yet unclassified' stamp: 'RoelofWobben 3/23/2019 
11:28'!
updateScoreWhenWonOnUnknownTeam: aTeam
| team |
team := Team new.
team name: aTeam.
team matchplayed: 1.
team matchwon: 1.
team matchlost: 0. 
team matchtied: 0. 
team points: 3.
^ team! !

!Team class methodsFor: 'as yet unclassified' stamp: 'RoelofWobben 3/23/2019 
11:16'!
updateScoreWhenWonOnKnownTeam: aTeam
aTeam matchplayed: aTeam matchplayed + 1.
aTeam matchwon:aTeam matchwon + 1.
aTeam points: aTeam points + 3 ! !


Object subclass: #Tournament
instanceVariableNames: ''
classVariableNames: 'Teams'
poolDictionaries: ''
category: 'Exercism-Tournament'!

!Tournament methodsFor: 'protocol' stamp: 'RoelofWobben 3/23/2019 11:30'!
tallyRows: aCollection
^ 0.! !


!Tournament methodsFor: 'class initialization' stamp: 'RoelofWobben 3/23/2019 
10:57'!
giveIndexOfATeam: aTeam
^ Teams findFirst: [ :element | element name = aTeam ]! !

!Tournament methodsFor: 'class initialization' stamp: 'RoelofWobben 3/23/2019 
10:57'!
initialize
Teams := OrderedCollection new! !

!Tournament methodsFor: 'class initialization' stamp: 'RoelofWobben 3/23/2019 
11:26'!
UpdateScoreOfATeam: aTeam
| index team |
index := Teams findFirst: [ :element | element name = aTeam ].
index ~= 0
ifTrue: [ team := Teams findFirst: [ :element | element name = 
'msk' ].
Team new updateScoreWhenWonOnKnownTeam: aTeam ]
ifFalse: [ team := Team updateScoreWhenWonOnUnknownTeam: aTeam.
Teams add: team ].
^ Teams.! !


ExercismTest subclass: #TournamentTest
instanceVariableNames: 'tournamentCalculator'
classVariableNames: ''
poolDictionaries: ''
category: 'Exercism-Tournament'!
!TournamentTest commentStamp: '' prior: 0!
# Tournament

Tally the results of a small football competition.

Based on an input file containing which team played against which and what the
outcome was, create a file with a table like this:

```text
Team   | MP |  W |  D |  L |  P
Devastating Donkeys|  3 |  2 |  1 |  0 |  7
Allegoric Alaskans |  3 |  2 |  0 |  1 |  6
Blithering Badgers |  3 |  1 |  0 |  2 |  3
Courageous Californians|  3 |  0 |  1 |  2 |  1
```

What do those abbreviations mean?

- MP: Matches Played
- W: Matches Won
- D: Matches Drawn (Tied)
- L: Matches Lost
- P: Points

A win earns a team 3 points. A draw earns 1. A loss earns 0.

The outcome should be ordered by points, descending. In case of a tie, teams 
are ordered alphabetically.

###

Input

Your tallying program will receive input that looks like:

```text
Allegoric Alaskans;Blithering Badgers;win
Devastating Donkeys;Courageous Californians;draw
Devastating Donkeys;Allegoric Alaskans;win
Courageous Californians;Blithering Badgers;loss
Blithering Badgers;Devastating Donkeys;loss