Re: Array questions

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

Re: Array questions

Andreas.Raab
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:
1) The method has a comment.
2) 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
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Array questions

Jerome Peace
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 <[hidden email]> wrote:

> From: Andreas Raab <[hidden email]>
> Subject: [Newbies] Re: Array questions
> To: "A friendly place to get answers to even the most basic questions about Squeak." <[hidden email]>
> 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:
> 1) The method has a comment.
> 2) 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




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners