Re: IsNumber function



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'....
.



Relevant Pages

  • Re: IsNumber function
    ... I've never cared what type of data gets passed to it in a VBA function and have used a variant...which basically is what you did there with the overloading. ... 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. ... Stefan's code also used widening (from auto-boxed primitives up to Object) and you see that folks are complaining about it. ... I also added a trick where I declared the default constructor private, thus preventing the user from accidentally calling a constructor that did nothing. ...
    (comp.lang.java.help)
  • Re: Copy Constructor Craziness
    ... There is no copy constructor involved at all. ... because I don't see any justification for this autogeneration. ... If you're overloading a mutator, there needs to be a copy ...
    (comp.lang.perl.misc)
  • Re: still crabby about copy constuctor craziness
    ... When the copy constructor gets ... or if a mutator has been explicitly overloaded. ... newest version explicitly overloads them. ... > no such thing as a copy constructor, and no operator overloading. ...
    (comp.lang.perl.misc)
  • Re: default parameters
    ... every permutation of default params in a C++ constructor, ... can someone explain the reasoning behind why it does not? ... overloading, and which is indeed what people do today, but yes, you?re ... If you do overloads, let?s say you have void f, and ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Overloading new and delete in C++.
    ... > In most tutorials on overloading 'new', they usually put malloc in the ... > malloc doesn't call the constructor and free won't call the destructor.. ...
    (comp.lang.cpp)

Loading