# [Newbies] crossSum

Ron Teitelbaum Ron at USMedRec.com
Tue May 9 14:46:00 UTC 2006

```> From: Enrico Schwass
> Sent: Tuesday, May 09, 2006 6:30 AM
>
> e.g. 251 crossSum (sometimes called checksum) should return 8
>
> 1234 -> 10
>
> The first problem is: Where to put the new method? We decide to put it
> directly into Integer (arithmetic). There are no side effects, we guess.
> Are we right?

The first question is a very good question.  The answer is, "It depends".
It depends on a lot of factors like how you are using it and if the method
has a more general purpose.  What I usually do is try to figure out where
the method is going to be used.  As a rule of thumb if there is no self in
your method it should either be a class method in really intuitive spot, or
it should be moved to where the self is.  So to answer your question, if
your method has a specific purpose on a class, having a class method like:
VerifiedMessage class>> crossSum: anInteger  makes sense.  If you believe
that it is useful for consumers of integer then Integer >> crossSum makes
sense.

>
> It would be great if others post their solution and it could be a good
> example for describing the test first development cycle, too.

Which would give you:

VerifiedMessage class>> crossSum: anInteger
"return to the sender the sum of each decimal digit of anInteger"
^anInteger asString
inject: 0
into: [:a :b | a + b asString asInteger]

Since the method is in your own class the testing is much simpler, but if
the method was in integer, then it's important to make sure you are not
overriding another implementation of crossSum: and that your method works on
all subclasses of Integer.

But it should work

Integer>> crossSum
"return to the sender the sum of each decimal digit of self"
^self asString detectSum: [:a | a asString asInteger]

Ron Teitelbaum
President / Principal Software Engineer
US Medical Record Specialists
Ron at USMedRec.com

```