Re: IsNumber function
- From: Salad <oil@xxxxxxxxxxx>
- Date: Sun, 12 Apr 2009 18:17:07 -0700
Mark Space wrote:
Thanks to all that contributed to this thread. I'd respond to each person's response but I hope all of them know I found their replies enlightening.
Salad wrote:.
I know what you are saying but some functions I've written in the past I've never cared what type of data gets passed to it in a VBA function (method) and have used a variant...which basically is what you did there with the overloading.
Stefan had some interesting code. He didn't use method overloading but instead auto-boxing. Primitives (ints, bytes, booleans, etc) can be boxed into Objects (Integers, Bytes, Booleans, etc) and then used in the type system. The disadvantage of this is that creating these Objects takes system resources and is slower than just passing a primitive directly.
What I did is not overloading, so much. I declared one method that took a double and used *widening* to pass all the numeric types to it. The primitive double is the widest type of number in Java, so all numbers can be passed to that method. Then I added one overloaded method to catch the booleans, and I was done. The disadvantage of this is that widening also takes system resources and in my opinion is poor practice, since it creates code that can be hard to read or debug. Methods that take a specific type are preferred.
The bigger issue is, as mentioned by myself and now three other folks on this thread, is that you should never need to test an object that you thought you didn't care about for its type. You can overload methods just the way I did, avoid auto-boxing, avoid the run time overhead of testing for a type, and always just know what type you have.
Stefan's code also used widening (from auto-boxed primitives up to Object) and you see that folks are complaining about it. Widening should not be used unless the type you really want is in fact Object. And in that case, you should only use Object's methods, you shouldn't need to be testing for other types.
My code used constructors, yours didn't. I need to learn when to use constructors and when not to.
I also added a trick where I declared the default constructor private, thus preventing the user from accidentally calling a constructor that did nothing. It's a documentation and user-friendly-API thing. Something to be on the look-out for as you write your own code.
> I'm coming from VB. I was in a VB debug window and entered
> ? IsNumeric(123)
> True
> ? IsNumeric("Mary")
> False
My Java debugger has a GUI, where the type of each variable is displayed right next to the name, so I never have to wonder, it's right there in front of me. Maybe you should try a better debugger. Just sayin'....
- References:
- IsNumber function
- From: Salad
- Re: IsNumber function
- From: Mark Space
- Re: IsNumber function
- From: Salad
- Re: IsNumber function
- From: Mark Space
- IsNumber function
- Prev by Date: Re: Clearing the cmd window
- Next by Date: Cheap ED Hardy Clothing, Coogi Clothing, urban kid wear wholesale
- Previous by thread: Re: IsNumber function
- Next by thread: Re: IsNumber function
- Index(es):
Relevant Pages
|
Loading