[Newbies] How to shorten a method (using inject: into: ?)

cdrick cdrick65 at gmail.com
Mon Jul 21 13:13:54 UTC 2008


Hi Stan,

2008/7/21 stan shepherd <squeak414 at free.fr>:
>
> Hi, I have the following method, that code critics flags as long:
>
> testMaleMeiosis2
>        | testSet mCount pCount mTotal pTotal |
>        pTotal := 0.
>        mTotal := 0.
>        Forecaster testMale meiose
>                do: [:strand |
>                        testSet := strand testRun.
>                        mCount := testSet maternalCount.
>                        pCount := testSet paternalCount.
>                        mTotal := mTotal + mCount.
>                        pTotal := pTotal + pCount].
>        self
>                should: [mTotal = 100
>                                and: [pTotal = 100]]
>
> I'm trying to shorten it, including various attempts with inject: into: ,
> but with no success. Any tips please?

First I would do that:

testMaleMeiosis2
       | testSet  mTotal pTotal |
       pTotal := 0.
       mTotal := 0.
       Forecaster testMale meiose
               do: [:strand |
                       testSet := strand testRun.
                       mTotal := mTotal + testSet maternalCount.
                       pTotal := pTotal + testSet paternalCount].
       self
               should: [mTotal = 100
                               and: [pTotal = 100]].

then maybe:

testMaleMeiosis2
| testSet maternalTotal paternalTotal|
testSet := strand testRun.
maternalTotal := Forecaster testMale meiose
	inject: 0
	into: [:mTotal :val | mTotal + testSet maternalCount].
paternalTotal := Forecaster testMale meiose
	inject: 0
	into: [:mTotal :val | pTotal + testSet paternalCount].

self should: [mTotal = 100 and: [pTotal = 100]].

"you might call sub-methods for maternalCount and parentalCount"

or to do in one method but this is not clear... you con inject anArray
containing you results like:

 #(1 2 3 )
	inject: #(1 1)
	into: [:array :elem |
			array at: 1 put: (array at: 1) 	* elem.
			array at: 2 put: (array at: 2) 	+ elem.
			array]

--> returns #(6 7)

So somethink like that should work:

testMaleMeiosis2
| resultArray tesSet |
testSet := strand testRun.
resultArray := Forecaster testMale meiose
	inject: #(0 0)
        into: [:array :val |
             array at: 1 put: (array at: 1) + testSet maternalCount.
	     array at: 2 put: (array at: 2) + testSet paternalCount.
	     array]

self should: [ resultArray = #(100 100)].


I nevertheless prefer to have two seprate inject method as I find more
readable. When methods are too long, you can split in several methods
too. I may also put testSet as an instvar if used in other places...

testMaleMeiosis2
| maternalTotal paternalTotal|
maternalTotal := self computeMaternalTotal.
paternalTotal := selfComputePaternalTotal.
self should: [maternalTotal = 100 and: [paternalTotal =100]].

HTH

Cédrick


The problem is you have two operations in the block but I tried that
and it seems to work (passing an array with your mTotal and pTotal)



>
> ...Stan
> --
> View this message in context: http://www.nabble.com/How-to-shorten-a-method-%28using-inject%3A-into%3A--%29-tp18567292p18567292.html
> Sent from the Squeak - Beginners mailing list archive at Nabble.com.
>
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list