* A coding experiment.*
Consider a Scrum development environment. Every programming team has an end user as a member.
The team's task is to code a credit card validity check.
A first goal is that the user representative shall read the code and agree that it is a correct rendering of their code checker:

    luhnTest: trialNumber
        | s1 odd s2 even charValue reverse |
-----------------------------------------------
/" Luhn test according to Rosetta"
"Reverse the order of the digits in the number."/
    reverse := trialNumber reversed.
/"Take the first, third, ... and every other odd digit in the reversed digits and sum them to form the partial sum s1"/
    s1 := 0.
    odd := true.
    reverse do:
        [:char |
            odd
                ifTrue: [
                    s1 := s1 + char digitValue.
                ].
                odd := odd not
        ].
/"Taking the second, fourth ... and every other even digit in the reversed digits: Multiply each digit by two and sum the digits if the answer is greater than nine to form partial sums for the even digits"
    "The subtracting 9 gives the same answer. "
"Sum the partial sums of the even digits to form s2"/
    s2 := 0.
    even := false.
    reverse do:
        [:char |
            even
                ifTrue: [
                    charValue := char digitValue * 2.
                    charValue > 9 ifTrue: [charValue := charValue - 9].
                    s2 := s2 + charValue
                ].
                even := even not
        ].
/"If s1 + s2 ends in zero then the original number is in the form of a valid credit card number as verified by the Luhn test."/
    ^(s1 + s2) asString last = $0
---------------------------------
Once this step is completed, the next step will be to make the code right without altering the algorithm (refactoring). The result should be readable and follow the team's conventions.


P.S. code attached.
'From Squeak5.3 of 6 March 2020 [latest update: #19432] on 4 May 2020 at 
3:56:36 pm'!
Object subclass: #LuhnTest
        instanceVariableNames: ''
        classVariableNames: ''
        poolDictionaries: ''
        category: 'BabyUtilities-Utilities'!

!LuhnTest methodsFor: 'testing' stamp: 'TRee 5/4/2020 15:55'!
testLuhnTest
        " LuhnTest new testLuhnTest "
        (self luhnTest: '49927398716') ifTrue: [Transcript cr; show: 'Test 1 
OK'] ifFalse: [Transcript cr; show: 'Test 1 Error'].
        (self luhnTest: '4992 7398 716') ifTrue: [Transcript cr; show: 'Test 2 
error'] ifFalse: [Transcript cr; show: 'Test 2 OK'].
        (self luhnTest: '4916566735402344') ifTrue: [Transcript cr; show: 'Test 
3 OK'] ifFalse: [Transcript cr; show: 'Test 3 Error'].
        (self luhnTest: '5566157921842582') ifTrue: [Transcript cr; show: 
'Test4 OK'] ifFalse: [Transcript cr; show: 'Test 4 error'].

        
        ! !


!LuhnTest methodsFor: 'LuhnTest' stamp: 'TRee 5/4/2020 15:02'!
luhnTest: trialNumber
        | s1 odd s2 even charValue reverse |
" Luhn test according to Rosetta"
"Reverse the order of the digits in the number."
        reverse := trialNumber reversed.
"Take the first, third, ... and every other odd digit in the reversed digits 
and sum them to form the partial sum s1"
        s1 := 0.
        odd := true.
        reverse do: 
                [:char |
                        odd 
                                ifTrue: [
                                        s1 := s1 + char digitValue. 
                                ].
                                odd := odd not
                ].
"Taking the second, fourth ... and every other even digit in the reversed 
digits:
Multiply each digit by two and sum the digits if the answer is greater than 
nine to form partial sums for the even digits
Sum the partial sums of the even digits to form s2"
        s2 := 0.
        even := false.
        reverse do: 
                [:char |
                        even 
                                ifTrue: [
                                        charValue := char digitValue * 2.
                                        charValue > 9 ifTrue: [charValue := 
charValue - 9].
                                        s2 := s2 + charValue
                                ].
                                even := even not
                ].
"If s1 + s2 ends in zero then the original number is in the form of a valid 
credit card number as verified by the Luhn test."
        ^(s1 + s2) asString last = $0
        
! !

Reply via email to