Hi Andreas,
+1.
Shows the difference between thinking about things off the cuff and thinking about things after years of experience,
Though if I were programming things I would do it by using methods on hand rather than invent the new ones for this. We got to get him to read the books. Then the code.
Cheers --Jer
--- On Fri, 4/23/10, Andreas Raab andreas.raab@gmx.de wrote:
From: Andreas Raab andreas.raab@gmx.de Subject: [Newbies] Re: Array questions To: "A friendly place to get answers to even the most basic questions about Squeak." beginners@lists.squeakfoundation.org Date: Friday, April 23, 2010, 8:03 PM On 4/23/2010 3:48 PM, Jerome Peace wrote:
Second, how would i make a method that will return
the
number of integers in the array different from the integer parameter
anArray howmany: 5 should return
how many
elements in the array are different from the number
howMany: anItem
^(self reject: [ :each | each = anItem ]) size .
Better:
howManyAreNot: anItem "Answer the number of elements different from anItem"
^self count:[:each| each ~= anItem]
Why is that better? 3 reasons:
- The method has a comment.
- The method name tells you what the method does - the
spec said to answer the number of elements that are NOT the argument; the method name should reflect that. 3) It uses #count: instead of #reject: making it more compact.
Do note that 1) and 2) really go together. Having a method called #howMany: that returns how many items are NOT in the collection and no comment clarifying whether the method is wrongly named or simply buggy is really problematic.
Cheers, - Andreas