Science Fair Project Encyclopedia
Checking type instead of membership
Checking type instead of membership is a bad computer programming practice that means checking the class type of an object instead of checking for its membership of a class.
This ties the code to a single class, instead of to the hierachy of a class (i.e. a class and its subclasses) which is much more flexible and allows expansion of the class.
Checking type instead of membership is an example of Antipattern.
Example in Java
Checking type (wrong)
if (n.getClass().getName() == "Number") {
// Do something
}
Checking membership (right)
if (n instanceof Number) {
// Do something
}
See Also
Last updated: 06-06-2005 02:03:15
10-26-2009 08:16:03
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details
The contents of this article is licensed from www.wikipedia.org under the GNU Free Documentation License. Click here to see the transparent copy and copyright details


