java coding practices r.santhana gopalan. java coding practices avoid call method inside the...
TRANSCRIPT
![Page 1: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/1.jpg)
Java Coding Practices
R.SANTHANA GOPALAN
![Page 2: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/2.jpg)
Java Coding Practices
Avoid call method inside the “for” loop Wrong
for( int i =0; i < vec.size(); i++){
...}
Unnecessary vec.size() will be called every time the loop continues. We can store the vec.size() in a “int” datatype and we can use in a “for” loop.
Correctint vec_size=vec.size();for(int i =0; i < vec_size; i++){
...}
![Page 3: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/3.jpg)
Wrong Example:boolean b = threadObj.isAlive();
for( ; b; )
{
….
}
![Page 4: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/4.jpg)
Java Coding Practices
Avoid Recursive Method If possible try to avoid Recursive method
calling. If you don’t know the limit then the
StackOverFlow error will happen.
![Page 5: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/5.jpg)
Java Coding Practices
isVisible() -or- getVisible() If the method returns the boolean value then
the method name should be starts with “is”. If the method returns non-boolean value
then the method name should be starts with “get”. Eg. getBackground().
![Page 6: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/6.jpg)
Java Coding Practices
Converter Method Initial lowercase word is "to", remaining
subwords are capitalized.Example:
toGenie(), toTiger(),
![Page 7: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/7.jpg)
Java Coding Practices
Getters Methodmethods that query state. Initial lowercase word is "get", remaining
sub words are capitalized and, if possible, are exactly the same as the name of the instance field to which they relate.
Example:getDjinnName(), getCatColor()
![Page 8: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/8.jpg)
Java Coding Practices
Setters Methodmethods that set state Initial lowercase word is "set", remaining
sub-words are capitalized and, if possible, are exactly the same as the name of the instance field to which they relate.
Example: setDjinnLocation()
![Page 9: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/9.jpg)
Java Coding Practices
Method – pass by reference (or) value?Be very careful by passing the Objects to
the another method. If the method changes the value in the
method then make sure that is documented.
![Page 10: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/10.jpg)
Java Coding Practices
Do you know the answer?public void tricky(Point arg1, Point arg2){
arg1.x = 100;arg1.y = 100;
Point temp = arg1; arg1 = arg2; arg2 = temp;
}
public static void main(String [] args) {
Point pnt1 = new Point(0,0); Point pnt2 = new Point(0,0); System.out.println("X: " + pnt1.x + " Y: " +pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y); System.out.println(" "); tricky(pnt1,pnt2); System.out.println("X: " + pnt1.x + " Y:" + pnt1.y); System.out.println("X: " + pnt2.x + " Y: " +pnt2.y);
}
![Page 11: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/11.jpg)
Java Coding Practices
Answer:X: 0 Y: 0
X: 0 Y: 0
X: 100 Y: 100
X: 0 Y: 0
Avoid this kind of programming.
![Page 12: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/12.jpg)
Java Coding Practices
If you override Object.equals(), also override Object.hashCode(), and vice-versa.
Reason:Essentially all containers and other utilities
that group or compare objects in ways depending on equality rely on hashcodes to indicate possible equality.
![Page 13: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/13.jpg)
Java Coding Practices
Import classes one by one instead of importing a whole package, thus it becomes clearer.
Example: import com.adventnet.beans.graph.LineGraph; import com.adventnet.beans.graph.BarGraph;
![Page 14: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/14.jpg)
Java Coding Practices
Don’t declare variables as package level access. The default access level is package.Wrong
int status;Correct
private int status;
![Page 15: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/15.jpg)
Java Coding Practices
if” (or) “else if” - Document it perfect.Example:
if(prop.containsKey("caseid")){ //Do some action}if(prop.containsKey("casemessage")){ //Do some action}
![Page 16: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/16.jpg)
Java Coding Practices
Keep the block level less than 4.Wrong Example:
if (condition1){ if(condition2) { if(condition3) { if(condition 4) { if (condition5) {
![Page 17: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/17.jpg)
Java Coding Practices
Don’t call the method unnecessarilyWrong Example:
if(ae.getA().equals("Default"))
{
}
else if(ae.getA().equals(“One”))
{
}
![Page 18: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/18.jpg)
Java Coding Practices
CorrectString aVal=ae.getA();
if(aVal.equals("Default"))
{
}
else if(aVal.equals(“One”))
{
}
![Page 19: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/19.jpg)
Java Coding Practices
Don’t use “\n” for the new line characterWrong Example:
textarea.append(message + “\n”);Correct Example:
Declare private static String lineseparator = System.getProperty(“lineseparator”);
textarea.append(message + lineseparator);
![Page 20: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/20.jpg)
Java Coding Practices
Use File.separator for the file separator. If it is really required then use “/”.Wrong
fname=“help/index.html”Correct
fname=“help” +File.separator + “index.html”
![Page 21: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/21.jpg)
Java Coding Practices
Be careful while add the debug statements in StringTokenizer.nextToken()Wrong Example
StringTokenizer st = new StringTokenizer(strValue)while(st.hasMoreElements()){ String s = st.nextToken(); System.out.println(st.nextToken()); -----}
![Page 22: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/22.jpg)
Java Coding Practices
StringBuffer - Performance If you know the size then instantiate
StringBuffer(int n). This will give high performance.
![Page 23: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/23.jpg)
Java Coding Practices
FileOutput Stream is costly. Wrong
fos = new FileOutputStream(f);
fos.write(htmltagstart.getBytes());
fos.write(headtagstart.getBytes());
fos.write(metatag.getBytes());
![Page 24: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/24.jpg)
Java Coding Practices
Correct
StringBuffer sb = new StringBuffer(1000);sb.append(htmltagstart);sb.append(headtagstart);sb.append(metatag);
fos = new FileOutputStream(f);fos.write(sb.toString().getBytes());
![Page 25: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/25.jpg)
Java Coding Practices
Do proper close operation. Otherwise it will create lot of problem in the runtime.Example:
FileOutStream.close()ResultSet.close()
![Page 26: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/26.jpg)
Java Coding Practices - Question
FileInputStream: read() Vs. read(byte[]) . Which will give more performance?
FileOutputSteam: write() Vs. write(byte[]). Which will give more performance?
![Page 27: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/27.jpg)
Java Coding Practices
SerializableSerialization can be very costly.Using the transient keyword reduces the
amount of data serialized.
Wrong Example:private JFrame;
Correct Example:private transient JFrame;
![Page 28: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/28.jpg)
Java Coding Practices
Explicitly set the layout of the panel/Frame/Dialog. Don’t leave it to default.Panel.setLayout(new BorderLayout());
![Page 29: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/29.jpg)
Java Coding Practices
Use the Constant to add the component in BorderLayout.Wrong Example:
panel.add(“Center”,textarea);Correct Example:
panel.add(textarea,BorderLayout.CENTER);
![Page 30: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/30.jpg)
Java Coding Practices
Don’t call UIManager.setLookAndFeel() in your program. Only the main class can call this method.UIManager.getCrossPlatformLookAndFeelC
lassName());
![Page 31: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/31.jpg)
Java Coding Practices - Question
What is wrong?
private int sum;
public CalculateSum(int sum)
{
sum=sum;
}
![Page 32: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/32.jpg)
Java Coding Practices
JOptionPane(Object,…) - What is the difference between passing “null” value and passing the exact JFrame object?
![Page 33: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/33.jpg)
Java Coding Practices
Don’t write anonymous classesWrong:
textarea.addActionListener(new ActionListener(){
…….
…….
};
![Page 34: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/34.jpg)
Java Coding Practices
Don’t use “goto” command and “labelled” statements.
![Page 35: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/35.jpg)
Java Coding Practices
Use Bean Builder to develop UI.….features
![Page 36: Java Coding Practices R.SANTHANA GOPALAN. Java Coding Practices Avoid call method inside the “for” loop Wrong for( int i =0; i < vec.size(); i++) {](https://reader036.vdocuments.site/reader036/viewer/2022062518/5697bf7a1a28abf838c82c0a/html5/thumbnails/36.jpg)
Java Coding Practices
Common MistakeComparing String using “==“ operator.