I am developing a simple java program which is run from command prompt using the JAR that I built. My program involves a lot of if conditions. My scenario is like, even if one if condition fails there is no point in running the remaining program.
When an if condition fails I want to do the following.
- I want to show the reason for stopping the program
- I want to quit running the program.
When an if condition validates to true, I want to continue with my execution and check the next if condition
Approach 1
I am aware that this can be achieved by using if else construct. But it leads to a lot of if else cascading and makes the code less readable and maintainable. Please find the sample below
if(name!=null){
//some code here before next if condition
if(gender!=null){
//some code here before next if condition
if(age==20){
//some code here before next if condition
if(zip!=null){
//some code here before next if condition
if(country!=null){
//some code here before next if condition..........
}
else{
System.out.println("country can't be null");
}
}
else{
System.out.println("zip can't be null");
}
}
else{
System.out.println("age should be 20");
}
}
else{
System.out.println("gender can't be null");
}
}
else{
System.out.println("name can't be null");
}
Approach 2:
I was wondering if using System.exit(0) could be a good alternative to avoid cascading. Please find the sample below.
if(name==null){
System.out.println("name can't be null");
System.exit(0);
}
//some code here before next if condition
if(gender==null){
System.out.println("gender can't be null");
System.exit(0);
}
//some code here before next if condition
if(age==0){
System.out.println("age should be 20");
System.exit(0);
}
//some code here before next if condition
if(zip==null){
System.out.println("zip can't be null");
System.exit(0);
}
//some code here before next if condition
if(country==null){
System.out.println("country can't be null");
System.exit(0);
}
Approach 3:
After some research I got to know that a custom exception can be thrown as well. Please find the below code.
try {
if (name == null) {
throw new MyCustomException("name can't be null");
}
// some code here before next if condition
if (gender == null) {
throw new MyCustomException("gender can't be null");
}
// some code here before next if condition
if (age == 0) {
throw new MyCustomException("age should be 20");
}
// some code here before next if condition
if (zip == null) {
throw new MyCustomException("zip can't be null");
}
// some code here before next if condition
if (country == null) {
throw new MyCustomException("country can't be null");
}
} catch (MyCustomException cusExep) {
}
Approach 4:
I believe this is the best approach though, your thoughts please
if (name == null) {
return("name can't be null");
}
// some code here before next if condition
if (gender == null) {
return("gender can't be null");
}
// some code here before next if condition
if (age == 0) {
return("age should be 20");
}
// some code here before next if condition
if (zip == null) {
return("zip can't be null");
}
// some code here before next if condition
if (country == null) {
return("country can't be null");
}
My question are
- Which is a better practice. Approach 1,2,3 or 4?
-
Can System.exit(0) be used in scenarios like mine? If no, on what scenarios should I use System.exit(0). of course I did some research. Please find the few of the many links I deemed useful.
Thanks for being patient.
Aucun commentaire:
Enregistrer un commentaire