I know that null value check and Empty string value check is mandatory for us being a programmer.
I also know that we have null value check and Empty string check methods inside JAVA already.
But i want to know we need to make program all the time to check for null values and empty strings manually.
This is increasing the number of lines of program as well. So do we have any way to get rid of this ?
Let say by example:
Here in above code, i need to check for Empty String all time manually and also i need to write code for null value check before empty strings checking.
So, i am writing same code again and again for that purpose specially.
Why not write a program to do the job, and then simply call this program with the variable arguments? Here is an example. The Consumer<String> process is your actual program. The rest is just testing environment:
Ya it is the right approach. But still we need to check for the values because it may happen while we are working on such system which database is modifying most of the time by number of external systems.
I suggest it might be a good idea to monitor places where nulls enter your program. If they come in, treat them like a wasp trying to eat your dinner:-
1: Kill it straight off. Or throw a NullPointerException as soon as the null appears.
2: Put a glass over it. You can hide nulls away safely in some sorts of data structure. An Optional<T> object may be a good place to hide it. Keep it there where it can't do any harm.
3: At least keep an eye on it. There are some data structures, e.g. trees, singly linked lists, where nulls are unavoidable. At least be aware of the possibility; this is where null tests are a good idea.
4: Let it sting you. Rest assured, it will find somewhere where it really hurts
There are some data structures which can be used to avoid nulls. For example, if you have a Person class you can design it like this:-...or like this:-If I go into the first class, what are you going to do about the middle name I haven't got? Mark it null? But in the second version, you can have an array with all middle names in; in my case you can use a zero‑length array. Abracadabra! No nulls More details in books like Effective Java by Joshua Bloch.