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 hierarchy 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().equals("Number")) {
   // Do something
 }
 

Checking membership (right)

 if (n instanceof Number) {
   // Do something
 }
 

See Also

This computer science-related article is a stub. Help Wikipedia by expanding it.

See also: Checking type instead of membership, Antipattern, Class (computer science), Computer programming, Computer science, Datatype, Hierarchy (object-oriented programming), Object (computer science)