Diagnostic Java code: Impostor type error mode

Source: Internet
Author: User

When you use special tags in a field to distinguish between object types, you may produce errors that mark incorrectly labeling related data--known as the impostor Type error pattern. In this part of diagnosing Java code, Eric Allen analyzes the symptoms and causes of this error, explains in detail how to prevent errors, and discusses an attractive hybrid implementation that does not use impostor type, but in the end there are many of the same drawbacks. Please share your views on this article with the author and other readers in the discussion forum.

Some data types are manipulated in the program except for the most insignificant part. A static type system provides a way to ensure that a program does not improperly manipulate data of a given type. One of the advantages of the Java language is that it is strictly differentiated, so the type error is eliminated before the program runs. As a developer, we can use this type of system to provide more robust and error-free code. However, we often do not allow the type system to maximize its potential.

Impostor Type error mode

Many programs can use static type systems more, but they do not, but rely on special fields that contain tags that distinguish data types.

By relying on these special fields to differentiate data types, such programs discard the protective measures that the type system specifically provides them. When one of these tags mistakenly labels its data, it produces the error I call the impostor type.

Symptoms

A common symptom of impostor type errors is that many conceptually different types of data are handled in the same (and wrong) way. Another common symptom is that the data does not match any of the specified types.

The first rule is that if the conceptual data type does not match the method it is being processed by the program, it can be doubted whether the pattern error has occurred.

To illustrate how easy it is to introduce an error in this pattern, let's consider a simple example. Suppose we need to deal with a variety of Euclidean geometry shapes, such as circles, squares, and so on. These geometries have no coordinates, but they contain a scale variable, so you can calculate their area.

Listing 1. Using imposter type to implement various geometric shapes

public class Form {
   String shape;
   double scale;
   public Form(String _shape, double _scale) {
     this.shape = _shape;
     this.scale = _scale;
   }
   public double getArea() {
     if (shape.equals("square")) {
       return scale * scale;
     }
     else if (shape.equals("circle")) {
       return Math.PI * scale * scale;
     }
     else { // shape.equals("triangle"), an equilateral triangle
       return scale * (scale * Math.sqrt(3) / 4);
     }
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.