The need to add data classes to the language

Imagine securing the borders of a country, which are normally guarded by defense forces. Will the level of security change based on the relationships with the neighboring countries (cordial, neutral, or tense)? What happens if the borders are porous (for example, the borders in Western Europe, for the Schengen countries)? Now, compare guarding the borders of a country with guarding our homes or securing the contents of a cabinet in a room.

Although each instance in the preceding example concerns the security of an entity and its protection from a physical attack, the instances have varying requirements.

Similarly, until now, the classes in Java have been used to model a wide range of requirements. While this works well for a lot of cases, it doesn't work for some. If you want to make the same size fit all, you'll need a lot of adjustments, for most of them.

Compare this to using the same trouser size for individuals with varying heights and waist sizes, as shown in the following diagram:

In the past, enums were added to the Java language (version 5). Even though a class can be programmed to create an enumeration of primitives or objects, enums simplified the process for a developer.

Enums reduced the coding for developers. At the same time, they made the intent of each enum explicit to the users.

In the preceding section, the Emp POJO is just a carrier of its data. However, seasoning a class to behave like a data class requires a developer to define multiple methods—constructs, accessors, mutators, and other methods from the object class. You might argue that you can use an IDE to easily generate all of these methods for your class. You are right! And it's quite simple to do so.

However, that only takes care of the writing part of the code. What happens to the reading part of the code, for the users of the class? Us developers understand that a piece of code might be written just once, but will be read multiple times. That is why experienced programmers stress good coding practices, for comprehending, reading, and maintaining code.

When the definition of data classes is induced in the language, the readers of the code will know its explicit intent of being a data class. The developers need not dig their claws deep into finding the code that was in addition to being a data class, so that they don't miss any important information.

This will also prevent the developers from using half-baked classes as data classes. At times, developers use such classes as a data class, which do not include all of the relevant methods (such as equals() or hashCode()). This will surely lead to inserting subtle bugs in your applications. A collection class, such as Map, requires a class to implement its equals() and hashCode() methods to function properly and efficiently.

Introducing data classes with a change in the language would decrease the verbosity of the language, broadcasting the intent of the structure to all.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.69.199