Can it do this way ?
Hello, I have now a challenge where I have to validate a ISBN number. Can I do something like this on the class side : (string isEmpty) Â Â ifTrrue: [ ^ false] Â ifFalse:Â [ digits:= something. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â controlDigit := something. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self validateISBNNumber] where on the validateISBNNumber I use the instance variables digits and controlDigit to validate the ISBN number on the instance side. Roelof
Roelof, I don't think so. A Class cannot access instance variables. Why? because a Class doesn't know which of its instances to ask for it. Ask yourself: who is "self" in a Class method? Is it the Class or is it an individual instance of the Class? (Hint: in a Class, #self is the Class, while in an instance, #self is the instance) . "Who" do you want to validate the ISBN number? An individual instance of your Class (like Book)? Or is validating an ISBNNumber a Task that is independent of an individual item? If it is independent, then why would you want to use semething specific in that method? You need to understand the differences between Class and Instance variables. I suggest reading Chapter 4 of "The Art and Science of Smalltalk <http://stephane.ducasse.free.fr/FreeBooks/Art/artAdded174186187Final.pdf>" or better: read the whole book. It's easy to read and sparks quite a few A-HA's per hour. HTH Joachim Am 02.09.20 um 08:18 schrieb Roelof Wobben:
Hello,
I have now a challenge where I have to validate a ISBN number.
Can I do something like this on the class side :
(string isEmpty) Â Â ifTrrue: [ ^ false] Â ifFalse:Â [ digits:= something. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â controlDigit := something. Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â self validateISBNNumber]
where on the validateISBNNumber I use the instance variables digits and controlDigit to validate the ISBN number on the instance side.
Roelof
-- ----------------------------------------------------------------------- Objektfabrik Joachim Tuchel mailto:jtuchel@objektfabrik.de Fliederweg 1 http://www.objektfabrik.de D-71640 Ludwigsburg http://joachimtuchel.wordpress.com Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1
Everything Joachim Tuchel wrote about this is fine EXCEPT that this is an Exercism task, where Roelof has to implement an API that he is given and is not at liberty to do it right. Thanks to the way that IsbnVerifierTest is structured, - the IsbnVerifier class has NO use for ANY variables of ANY kind: no instance variables, no class variables, and no class instance variables. - the IsbnVerifier class has only ONE method that is called, and that is on the INSTANCE side. That is, it's a simple pure function disguised as an object. A good design might have an ISBN class, and would put #isValidISBN10: and #isValidISBN13: on the class side. But to solve the Exercism exercises, you HAVE to implement the API that the test class is expecting, not something better. If it passes the tests, it is by definition right. If it doesn't, it is by definition wrong, even if it is a far better design. So - one class, IsbnVerifier - with no variables of any kind - with one and only one method, #isValidIsbn, on the instance side. It all gets so much clearer when you realise that the Exercism tasks are NOT designed to teach Object-Oriented Design. The problems have to serve many programming languages, some of which, like bash and x86 assembly code, are not object-oriented. On Wed, 2 Sep 2020 at 18:42, jtuchel@objektfabrik.de < jtuchel@objektfabrik.de> wrote:
Roelof,
I don't think so.
A Class cannot access instance variables. Why? because a Class doesn't know which of its instances to ask for it. Ask yourself: who is "self" in a Class method? Is it the Class or is it an individual instance of the Class?
(Hint: in a Class, #self is the Class, while in an instance, #self is the instance) .
"Who" do you want to validate the ISBN number? An individual instance of your Class (like Book)? Or is validating an ISBNNumber a Task that is independent of an individual item? If it is independent, then why would you want to use semething specific in that method?
You need to understand the differences between Class and Instance variables. I suggest reading Chapter 4 of "The Art and Science of Smalltalk <http://stephane.ducasse.free.fr/FreeBooks/Art/artAdded174186187Final.pdf>" or better: read the whole book. It's easy to read and sparks quite a few A-HA's per hour.
HTH
Joachim
Am 02.09.20 um 08:18 schrieb Roelof Wobben:
Hello,
I have now a challenge where I have to validate a ISBN number.
Can I do something like this on the class side :
(string isEmpty) ifTrrue: [ ^ false] ifFalse: [ digits:= something. controlDigit := something. self validateISBNNumber]
where on the validateISBNNumber I use the instance variables digits and controlDigit to validate the ISBN number on the instance side.
Roelof
-- ----------------------------------------------------------------------- Objektfabrik Joachim Tuchel mailto:jtuchel@objektfabrik.de <jtuchel@objektfabrik.de> Fliederweg 1 http://www.objektfabrik.de D-71640 Ludwigsburg http://joachimtuchel.wordpress.com Telefon: +49 7141 56 10 86 0 Fax: +49 7141 56 10 86 1
Correct. This is a exercism task. and yes, there are no variables given but that does not in my oponion means that I cannot add a instance or a class variable I myself was thinking about adding two instance variables named digits which hold the first 9 characters and a variable named controlDigit Can I do it then this way 1) Check if the string is empty , if so return false otherwise made a new object like this in IsValidIsbn isValidIsbn: aString    if aString isEmpty      ifTrue: [False]     ifFalse: [ self new                         digits := something.                         controlDigit := something.] I can also do what I think you are describing isValidIsbn: AString   if aString isEmpty       ifTrue: [ ^false]       ifFalse: [ISBN isValidIsbn10] and then as you describe so have a isValidISBN10 method in the class side where I do the check there so the class ISBN has the two instance variables digits and controlDigit Roelof
participants (3)
-
jtuchel@objektfabrik.de -
Richard O'Keefe -
Roelof Wobben