Slate-style
Brian Rice
water at tunes.org
Sun Jun 5 09:37:15 PDT 2005
On Jun 4, 2005, at 7:55 PM, Pupeno wrote:
> Can you please check it and do any comments that you feel
> apropiate, I'm
> interested in mistakes I've made (almost everything seems to work)
> as well as
> how can I make it in less lines/more efficient.
Your newCodeMaker method should be a "new" method on the CodeMaker
prototype. Notice that the newCodeMaker method does NOT use the
argument m for MasterMind. This is called a "bad code smell" in the
refactoring community - that means it's something you should be on
the look-out for when writing or looking at code. Remember that if
your method-name contains a noun, you are probably writing the code
wrongly. Slate code should read like noun-verb phrases, with nouns
and verbs in separate parts of the sentence.
For randomItemsFrom:, creating a new RandomStream for each call is
very expensive - RandomStream creation involves making and
initializing a 624-element array. Just using an existing RandomStream
is very cheap, so find some way to do that, even if you have to add
the RandomStream as a persistent attribute of your game or make it a
global at some level.
You say "creates a stack" and then proceed to not use a Stack, but an
ExtensibleArray. Just use a Stack and push:. :) Although, honestly,
you should use:
[| :result | .... result nextPut: (element you want in the
result) ...] writingAs: someCollectionType
where someCollectionType can be an Array or Set or whatever you like.
This abstracts over the protocol with a stream, and returns the
stream's contents for you.
Unify the optional parameter "ar" with "allowRepeated". You can
assign to optionals - they are just local variables with possible
inputs.
Use inform: instead of "Console ;". It will take care of line-endings
for you as well as not forcing you to care where the message goes.
The "good doWithIndex:" call at the end has a complex body. You would
do well to /avoid/ looking in the manual for everything and instead
use grep on, say, sequence.slate to see what methods have verbs you
are trying to express. Cleaning that up will take time, though,
especially since that area looks like it needs to grow.
> One real problem I couldn't solve is the generation of (pseudo-)
> random code, I
> used the RandomStream, with newSeed: but it always leads to the
> same set of
> code. How can I make it really random ?
You have to give it a different seed every time you make a new
RandomStream (the period of this randomizer is huge, so that's not a
big deal - it's just the starting point that needs to vary). The
traditional way to do this is to have access to a time primitive that
tells you seconds-from-the-epoch or something, since that will be
different every time you use it - Slate doesn't have this yet - any
volunteers to add one?
--
-Brian
More information about the Slate
mailing list