Hi all,
This is my first time working with a dynamically typed environment and my first instinct is to error check all arguments. For example suppose I want to create an object to represent an octet in an IP address (a number between 0 and 256) I would do something like this: Object subclass: #Octet instanceVariables: 'octetValue' ...... initialize super initialize. octetValue := 0. setOctetValue: numBtw0And256 octetValue = numBtw0And256 <-------Here's where I have a problem How do I know the numBtw0And256 is actually an integer between 0 and 256 and not 'cat' or '3.14'? Since I came from statically typed languages I feel like I writing a whole bunch of error checking code along the lines of ifIsInteger: ... etc which doesn't seem to be in the spirit of Squeak. What am I doing wrong? Thanks Deech _________________________________________________________________ WIN up to $10,000 in cash or prizes enter the Microsoft Office Live Sweepstakes http://clk.atdmt.com/MRT/go/aub0050001581mrt/direct/01/ _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
>[Newbies] Newbie: Error Checking in Dynamically typed Language >Aditya Siram aditya_siram at hotmail.com >Sun Dec 10 03:22:09 UTC 2006 wrote: >Hi all, >This is my first time working with a dynamically typed environment and my >first instinct is to error check all arguments. For example suppose I want >to create an object to represent an octet in an IP address (a number between >0 and 256) I would do something like this: > >Object subclass: #Octet > instanceVariables: 'octetValue' > ...... > >initialize > super initialize. > octetValue := 0. > >setOctetValue: numBtw0And256 > octetValue = numBtw0And256 <-------Here's > >How do I know the numBtw0And256 is actually an integer between 0 and 256 and >not 'cat' or '3.14'? Octet>>setOctetValue: numBtw0And256 (self isOctet: numBtw0And256 ) ifTrue: [ octetValue := numBtw0And256 ] ifFalse: [ self error: 'ooch, ouch, eeach, ' , numBtw0And256 printString , ' was supposed to be an octet and it was not.' ] Octet>>isOctet: anOctet "True iff rcvr is an octet value anOctet isInteger ifFalse: [^false ] ^(anOctet min: 256 max: 0 ) = anOctet alternately you can define isOctet in two places Object>>isOctet "False for anything but a small integer" ^false SmallInteger>>isOctet isOctet "True iff rcvr is an octet value ^(self min: 256 max: 0 ) = self and replace the predicate (self isOctet: numBtw0And256 ) with: numBtw0And256 isOctet > >Since I came from statically typed languages I feel like I writing a whole >bunch of error checking code along the lines of ifIsInteger: ... etc which >doesn't seem to be in the spirit of Squeak. If you don't know where your data is coming from it is good to check it. Especialy as you write new stuff. You want errors to point to themselves early. Later in refactoring you can weed out redundant test if you need to get back the speed. Squeak has had a lot of cowboys who code up stuff w/o checking or taking small steps. Squeak has a lot of bugs introduced from letting that code in. Don't let the spirit of squeak mislead you. Good programing practices are good programming practices. Yours in service, -- Jerome Peace ____________________________________________________________________________________ Have a burning question? Go to www.Answers.yahoo.com and get answers from real people who know. _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
In reply to this post by Aditya Siram
Hi
normally you will not do explicit check. We only test when this is really important. > Hi all, > This is my first time working with a dynamically typed environment > and my first instinct is to error check all arguments. No good instinct for dynamically typed :) > For example suppose I want to create an object to represent an > octet in an IP address (a number between 0 and 256) I would do > something like this: > > Object subclass: #Octet > instanceVariables: 'octetValue' > ...... > > initialize > super initialize. > octetValue := 0. > > setOctetValue: numBtw0And256 > octetValue = numBtw0And256 <-------Here's where I have a > problem > > How do I know the numBtw0And256 is actually an integer between 0 > and 256 and not 'cat' or '3.14'? > > Since I came from statically typed languages I feel like I writing > a whole bunch of error checking code along the lines of > ifIsInteger: ... etc which doesn't seem to be in the spirit of Squeak. > > What am I doing wrong? Just write your code and tests! Initialize well your state. Do only check if you absolutely want in your constructor? > > Thanks > > Deech > > _________________________________________________________________ > WIN up to $10,000 in cash or prizes – enter the Microsoft Office > Live Sweepstakes http://clk.atdmt.com/MRT/go/aub0050001581mrt/ > direct/01/ > > _______________________________________________ > Beginners mailing list > [hidden email] > http://lists.squeakfoundation.org/mailman/listinfo/beginners > _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Hi
It's seem to me that is not really a dynamical typed problem but more a design problem. If you want to specifiy to the user witch object you expected you should well name them. And make unit test so your customer can understand(and you can also remember why). Math stephane ducasse a écrit : > Hi > > normally you will not do explicit check. > We only test when this is really important. > >> Hi all, >> This is my first time working with a dynamically typed environment and >> my first instinct is to error check all arguments. > > No good instinct for dynamically typed :) > >> For example suppose I want to create an object to represent an octet >> in an IP address (a number between 0 and 256) I would do something >> like this: >> >> Object subclass: #Octet >> instanceVariables: 'octetValue' >> ...... >> >> initialize >> super initialize. >> octetValue := 0. >> >> setOctetValue: numBtw0And256 >> octetValue = numBtw0And256 <-------Here's where I have a >> problem >> >> How do I know the numBtw0And256 is actually an integer between 0 and >> 256 and not 'cat' or '3.14'? >> >> Since I came from statically typed languages I feel like I writing a >> whole bunch of error checking code along the lines of ifIsInteger: >> ... etc which doesn't seem to be in the spirit of Squeak. >> >> What am I doing wrong? > > Just write your code and tests! > Initialize well your state. > Do only check if you absolutely want in your constructor? > >> >> Thanks >> >> Deech >> _______________________________________________ Beginners mailing list [hidden email] http://lists.squeakfoundation.org/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |