From this perspective, I think using correct form of looping can enhance the readability of ones code. Most of the programming language provide three types of looping construct.
- for(init exp;exit condition;increment exp) loop
- while(boolean exp){ }
- do{ }while(boolean exp)
Lets take the scenario where program prompt user to provide input till he provides a valid input.
Here is various version using various looping constructs:
public String readUserInput(String userInstruction){
System.out.println(userInstruction);
String x = null;
//Requires minimum of one mental evaluation of while condition
//Clearly convey the message that first read is must and non-conditional
do{
x = readConsole(); //Reads a line from a console.
}while(null == x || x.length == 0); //Exit condition is outside the body
return x;
}
public String readUserInput(String userInstruction){
System.out.println(userInstruction);
String x = null;
//Requires minimum of two mental evaluations of while condition
//First when you enter first and second when you exist
//Does not convey the message that first read is must and non-conditional
while(null == x || x.length == 0){
x = readConsole(); //Reads a line from a console.
}
return x;
}
//More complex one
public String readUserInput(String userInstruction){
System.out.println(userInstruction);
String x = null;
boolean askAgain = true;
//Requires three mental evaluations of conditions
//First when you enter first and second when you exist
while(askAgain){
x = readConsole(); //Reads a line from a console.
if(null == x || x.length == 0){//Exit condition is burried inside body
askAgain = true;
}
}
return x;
}
public String readUserInput(String userInstruction){
System.out.println(userInstruction);
String x = null;
//Requires two mental evaluations of while condition
//First when you enter first and second when you exist
//Does not convey the message that first read is must and non-conditional
for(;null == x || x.length == 0;){
x = readConsole(); //Reads a line from a console.
}
return x;
}
Now lets consider another example where we are reading comma seperated data from csv file...
String data = readLine();
StringTokenizer tokenizer = new StringTokenizer(data,",");
while(tokenizer.hasMoreElements()){
String token = tokenizer.nextToken();
//Do further processng on token
}
//Do-While doesnot make any sense here at all
//Looks very unnatural..For loop is more natural
//when index is important for processing
String data = readLine();
StringTokenizer tokenizer = new StringTokenizer(data,",");
for(int i=0;i<tokenizer.countTokens();i++){
String token=tokenizer.nextToken();
//Do further processing
}
From the above examples, it is very clear that for is natural choice where index play important role and there are implicit or explicit upper limit but while is better choice if exit condition doesnot has explicit or implicit upper limit. do-while similarly is better choice where while is applicable except test has to be performed after execution on loop body and it should be executed at least once.
No comments:
Post a Comment