stricter typing



All timestamps are based on your local time of:

Posted by: stak
Posted on: 2006-10-28 22:37:10

so one of the things that falls under the "slightly better design" in my previous rant is stricter typing. most of the input validation i had to do was on a namespace variable that would get passed in to a class' constructor, and would be used to load appropriate config data from a config file. the namespace would be a String, but I'd always have to check it to make sure it wasn't null or empty (only whitespace). this could have been made unnecessary by having a Namespace object that just wrapped a String and did the checking in one spot.

taking this further, it's probably possible to replace most of the primitive variables that get passed around with more typesafe wrapper classes that not only do error-checking and input validation for you, but can also provide custom functionality. i guess that's kind of the point of object-oriented programming, but in order to take it to the level i'm thinking of, it would have to be supported by the platform from the ground up. everything from the UI down to the innards of the system code would have to use stricter types, rather than using ints and chars and constantly checking them for validity.

so, for example, let's assume we wanted to get a port number for some TCP connection we wanted to make. instead of using an int, we'd use a special PortNumber class that wrapped an int, and it would restrict the value of the int to a valid port number [1025, 65535]. there would be a custom renderer that would draw the UI element for a PortNumber, such that the user wouldn't be able to enter a value outside the range. the actual renderer could be done using a text field, or a number spinner, or whatever, but it wouldn't accept anything except a valid port number. the PortNumber object would be passed around all the way down to the network stack, which would serialize it directly into the TCP packets that get written out. once created, the PortNumber object would never have to be checked for validity, saving a lot of checks along the way at the expense of slightly less efficient (yet more modular) code.

a lot of the variables being passed around in most code have semantic information beyond the syntactic type that they're represented by. the more of this semantic information that can be captured by the type, the more checks you can do at compile-time, and the easier it is to debug. this is kind of the point of hungarian notation, but getting at it in a different way.

[ Add a new comment ]

 
 
(c) Kartikaya Gupta, 2004-2019. User comments owned by their respective posters. All rights reserved.
You are accessing this website via IPv4. Consider upgrading to IPv6!