|
RE: Newbie: Error Checking in Dynamically typed Language: msg#00041lang.smalltalk.squeak.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 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'? 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.
|
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Newbie: Error Checking in Dynamically typed Language, Aditya Siram |
|---|---|
| Next by Date: | Re: Newbie: Error Checking in Dynamically typed Language, stephane ducasse |
| Previous by Thread: | Re: Newbie: Error Checking in Dynamically typed Language, Mathieu |
| Next by Thread: | Squeak 3.9, world menu, how to open it with a shortcut like F1?, Grant Rettke |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |