Java String Best Practices - Part 4

අද මම කතා කරන්න යන්නෙ Java String Best Practices ගැන අපි කතා කරන්න  පටන් ගත්තු points වල තියෙන හතරවෙනි point එක ගැන. ඒ points ටික මතක් වෙන්න ආපහු පහලින් දාන්නම් මම.

  1. Strings දෙකක් එකතු කරන්න තියෙනවා නම් + operator වෙනුවට StringBuilder හරි StringBuffer හරි පාවිච්චි කරන්න. 
  2. Strings දෙකක් සමානද කියල බලන්න තියෙනවා නම් == operator එක වෙනුවට equals() method එක පාවිච්චි කරන්න. 
  3. Strings දෙකක් සමානද කියල බලන්න equals() method එක පාවිච්චි කරනවා නම්, අපි දන්න constant string එකකට දාන්න, නොදන්න variable එකකට පාවිච්චි කරන්න එපා. 
  4. පුළුවන් හැම වෙලාවකම if else-if නැතුව switch-case පාවිච්චි කරන්න බලන්න, ඒ වගේම ඒ තැන් වල toString() වෙනුවට String.valueOf() පාවිච්චි කරන්න බලන්න. 
  5. String එකක් හදනකොට new keyword එකෙන් Object එකක් හදන්නේ නැතුව literls("") විදියටම හදන්න. 
  6. String එකක null check එකක් වගේ කරන්න තියෙනවා නම් StringUtils එකේ තියෙන methods පාවිච්චි කරන්න. 
එහෙනම් අපි වැඩේ පටන් ගමු.

4.පුළුවන් හැම වෙලාවකම if else-if නැතුව switch-case පාවිච්චි කරන්න බලන්න, ඒ වගේම ඒ තැන් වල toString() වෙනුවට String.valueOf() පාවිච්චි කරන්න බලන්න. 

මම හිතනවා ඔයාල දැනටමත් switch-case පාවිච්චි කරන්නේ කොහොමද කියල දන්නවා ඇති කියල. මේ පොස්ට් එකෙන් මම කියන්න හදන්නේ String කොහොමද හරි විදියට switch-case වලදී  පාවිච්චි කරන්නේ කියන එකයි.

හිතන්න මෙන්න මෙහෙම අවස්ථාවක් එනවා කියල. ඔයාලට සිද්ද වෙනවා Strings කීපයක් සංසන්දනය කරන්න. එවගේ වෙලාවක if-else-if කීපයක් පාවිච්චි කරන්නෙ නැතුව swich-case එකක් පාවිච්චි කරන්න.

ඔයාල කොහොමත් දන්නවා if-else-if කීපයක් තියෙන වෙලාවක swich-case එකක් පාවිච්චි කරන එක තමා best practice එක කියල.මෙතැනදී අපි බලන්න යන්නේ String comparison එකක් කරන්න තියෙන වෙලාවක swich-case එකක් පාවිච්චි කලාම performance වැඩි වෙන්නේ කොහොමද කියල.

අපි මේක පොඩි example එකකින්ම විසදගන්න බලමු.

public class StringsInSwitchCase {
public static void main(String[] args) {

StringsInSwitchCase stringsInSwitchCase = new StringsInSwitchCase();

long startTime = System.nanoTime();
stringsInSwitchCase.convertStringToIntegerWithSwitch("FOUR");
long endTime = System.nanoTime();
System.out.println(String.format("String comparison with Switch took [%d] nano seconds.", (endTime - startTime)));

startTime = System.nanoTime();
stringsInSwitchCase.convertStringToIntegerWithIf("FOUR");
endTime = System.nanoTime();
System.out.println(String.format("String comparison with If took [%d] nano seconds.", (endTime - startTime)));

}
private int convertStringToIntegerWithSwitch(String stringNumber) {
switch(stringNumber) {
case "ZERO" :
return 0;
case "ONE":
return 1;
case "TWO":
return 2;
case "THREE":
return 3;
default :
return -1;
}
}
private int convertStringToIntegerWithIf(String stringNumber) {
if("ZERO".equals(stringNumber)) {
return 0;
} else if("ONE".equals(stringNumber)) {
return 1;
} else if("TWO".equals(stringNumber)) {
return 2;
} else if("THREE".equals(stringNumber)) {
return 3;
} else {
return -1;
}
}
}

බලන්න මම methods දෙකක් ලියල තියෙනවා. එකක් swich-case දාල අනිත් එක multiple if-else-if දාල.එතකොට ඔයාලට පේනවා multiple if-else-if දාපු එකේ equals() methods කීපයක් දාල තියෙනවා. switch-case එකේ එහෙම නෑ.මේක run කරලා බලමුද output එක මොනවගේ වෙයිද කියල බලන්න.

String comparison with Switch took [7900] nano seconds.

String comparison with If took [11200] nano seconds.

මේ බලන්නකෝ output එක. multiple if-else-if  වලට වඩා ගොඩක් අඩු වෙලාවක් තමා switch-case එකට ගිහින් තියෙන්නේ. 

මෙතැනදී ඔයාල ඔලුවට දාගන්න ඕන point එක තමා, multiple if-else-if ඇතුලේ equals() පාවිච්චි කරලා String comparison එකක් කරනවා නම් හැමවෙලාවෙම switch-case එකකට යන්න ඕන කියන එක.

මේකටත් මම පොඩි demo එකක් කරලා පෙන්නනවා පහලින් තියෙන වීඩියෝ එකේ.එකත් අනිවාර්යෙන්ම බලන්න.ඊලග පොස්ට් එකෙන් හම්බෙමු,එතකන් ජය.

Post a Comment

Previous Post Next Post