Data types

Basic and Advanced

Created by bpch.vector - Freepik.com

Disclaimer : The content of this blog are my views and understanding of the topic. I do not intend to demean anything or anyone. I am only trying to share my views on the topic so that you will get a different thought process and angle to look at this topic.

Introduction

Any programming language has some Basic Structural Elements for defining the data which is stored. Java has similar provision. We know that Basic DataTypes like Integer, String, Double, etc. are to let the compiler know what kind of data a particular reference can point to and also store in memory. Most of the Software Developers find these basic datatypes sufficient but we need to understand their significance and take our thought process to more abstract representations. Let us see how

Details

Java POJO has state and behaviour. A state is used to represent the data about some logical real life entity or sometimes metadata about entities or program. We have some state floating around the code too during method invocation as an argument. For an instance you have a Customer class with below fields

Basic Customer Entity

Having name, emailId as String is sufficient for most of us. But if we look back on what does a String datatype represents ? It is a simple group of characters. Its too generic and basic representation. Name and EmailId although are captured as a String but they have some meaning which distinguishes them. A Name of a person is a noun and ideally it is containing only Alphabets. Email on the other had has different parts username@domain Ex. abc@example.com. When we model our POJOs with basic datatype we end up losing this depiction and restriction. We end up externalising the validations for emailId which defies the encapsulation principle. Some may argue that validation is a cross cutting concern and should be separated out. I found that it does not work out effectively that way. If we make an object self-sufficient then we can localise the change and it also easy to locate the change and build reasoning around it. Take an example of Date Of Birth field in the Customer. It has LocalDate as the datatype which clearly mentions that it a Date Information and we have to build it in certain format. It does not capture time information for our use case.

Creating such meaningful datatypes and localising the attributes, validations and behaviour defines a new set of datatypes which are not provided by Java inherently but we can build them using basic datatypes. We call them Advanced Datatypes. These are more abstract and closer to real life mapping of the data. Below is how the new Customer class will look like with Advanced Datatypes

Now the question would be How do we differentiate between Entities and Advanced Data Types ? We have Customer class which represents a real life entity because a Customer is some physical entity which we can correlate. Name and Email are abstract and they are used to define a Customer. A Name and Email has no independent existence unless they are associated to a Customer. This logical reasoning could be used to define advanced datatypes.