How cn I improve this code

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How cn I improve this code

Roelof
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


Tournament.st (14K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: How cn I improve this code

Sean P. DeNigris
Administrator
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

Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: How cn I improve this code

Richard O'Keefe
In reply to this post by Roelof
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 <[hidden email]> 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