This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Chủ Nhật, 30 tháng 10, 2016

change image upload file when have a exists image

html:

<input id="profile-image-upload" class="hidden" type="file">
<div id="profile-image">click here to change profile image</div>


css:

input.hidden {
    position: absolute;
    left: -9999px;
}

#profile-image {
    cursor: pointer;
    background: #8adffb;
  width: 80px;
    height: 80px;
}


js:


$(function() {
    $('#profile-image').on('click', function() {
        $('#profile-image-upload').click();
    });
});

         


https://jsfiddle.net/ksnvphu/vq2u91w6/1/

Thứ Bảy, 29 tháng 10, 2016

How to Convert String to StringBuffer in Java and reverse

How to Convert String to StringBuffer in Java.

String str = "Hello";
StringBuffer sb = new StringBuffer();
sb.append(str);



How to Convert StringBuffer to String in Java

String sb = new StringBuffer();

String str = sb.toString();

Thứ Bảy, 22 tháng 10, 2016

Auto Comment in Eclipse

Class:
- Path: windown/preference/java/code style/code templates/types
/​**
 * ${file_name}
 *
 * version 1.0
 *
 * Date: ${date}
 *
 * copyright
 *
 * Modification Logs:
 * DATE                 AUTHOR          DESCRIPTION
 * -----------------------------------------------------------------------
 * ${date}              PhuNV3              Create
 */​

Method:
- Path: windown/preference/java/code style/code templates/methods



/**
 * @parame ${return_type}
 * @return ${return_type}
 * @throw ${return_type}
 */

Thứ Hai, 10 tháng 10, 2016

Using a Stored Procedure with Output Parameters



A SQL Server stored procedure that you can call is one that returns one or more OUT parameters, which are parameters that the stored procedure uses to return data back to the calling application. The Microsoft SQL Server 2005 JDBC Driver provides the SQLServerCallableStatement class, which you can use to call this kind of stored procedure and process the data that it returns.
When you call this kind of stored procedure by using the JDBC driver, you must use the call SQL escape sequence together with the prepareCallmethod of the SQLServerConnection class. The syntax for the call escape sequence with OUT parameters is the following:
{call procedure-name[([parameter][,[parameter]]...)]}
NoteNote:
For more information about the SQL escape sequences, see Using SQL Escape Sequences.
When you construct the call escape sequence, specify the OUT parameters by using the ? (question mark) character. This character acts as a placeholder for the parameter values that will be returned from the stored procedure. To specify a value for an OUT parameter, you must specify the data type of each parameter by using the registerOutParameter method of the SQLServerCallableStatement class before you run the stored procedure.
The value that you specify for the OUT parameter in the registerOutParameter method must be one of the JDBC data types contained in java.sql.Types, which in turn maps to one of the native SQL Server data types. For more information about the JDBC and SQL Server data types, seeUnderstanding the JDBC Driver Data Types.
When you pass a value to the registerOutParameter method for an OUT parameter, you must specify not only the data type to be used for the parameter, but also the parameter's ordinal placement or the parameter's name in the stored procedure. For example, if your stored procedure contains a single OUT parameter, its ordinal value will be 1; if the stored procedure contains two parameters, the first ordinal value will be 1, and the second ordinal value will be 2.
NoteNote:
The JDBC driver does not support the use of CURSOR, SQLVARIANT, TABLE, and TIMESTAMP SQL Server data types as OUT parameters.
As an example, create the following stored procedure in the SQL Server 2005 AdventureWorks sample database:
CREATE PROCEDURE GetImmediateManager
   @employeeID INT,
   @managerID INT OUTPUT
AS
BEGIN
   SELECT @managerID = ManagerID 
   FROM HumanResources.Employee 
   WHERE EmployeeID = @employeeID
END
This stored procedure returns a single OUT parameter (managerID), which is an integer, based on the specified IN parameter (employeeID), which is also an integer. The value that is returned in the OUT parameter is the ManagerID based on the EmployeeID that is contained in the HumanResources.Employee table.
In the following example, an open connection to the AdventureWorks sample database is passed in to the function, and the execute method is used to call the GetImmediateManager stored procedure:
public static void executeStoredProcedure(Connection con) {
   try {
      CallableStatement cstmt = con.prepareCall("{call dbo.GetImmediateManager(?, ?)}");
      cstmt.setInt(1, 5);
      cstmt.registerOutParameter(2, java.sql.Types.INTEGER);
      cstmt.execute();
      System.out.println("MANAGER ID: " + cstmt.getInt(2));
   }
   catch (Exception e) {
      e.printStackTrace();
   }
}
This example uses the ordinal positions to identify the parameters. Alternatively, you can identify a parameter by using its name instead of its ordinal position. The following code example modifies the previous example to demonstrate how to use named parameters in a Java application. Note that parameter names correspond to the parameter names in the stored procedure's definition:
public static void executeStoredProcedure(Connection con) {
   try {
      CallableStatement cstmt = con.prepareCall("{call dbo.GetImmediateManager(?, ?)}");
      cstmt.setInt("employeeID", 5);
      cstmt.registerOutParameter("managerID", java.sql.Types.INTEGER);
      cstmt.execute();
      System.out.println("MANAGER ID: " + cstmt.getInt("managerID"));
      cstmt.close();
   }
   catch (Exception e) {
      e.printStackTrace();
   }
}
NoteNote:
These examples use the execute method of the SQLServerCallableStatement class to run the stored procedure. This is used because the stored procedure did not also return a result set. If it did, the executeQuery method would be used.
Stored procedures can return update counts and multiple result sets. The Microsoft SQL Server 2005 JDBC Driver follows the JDBC 3.0 specification, which states that multiple result sets and update counts should be retrieved before the OUT parameters are retrieved. That is, the application should retrieve all of the ResultSet objects and update counts before retrieving the OUT parameters by using the CallableStatement.getter methods. Otherwise, the ResultSet objects and update counts that have not already been retrieved will be lost when the OUT parameters are retrieved. For more information about update counts and multiple result sets, see Using a Stored Procedure with an Update Count and Using Multiple Result Sets.

Chủ Nhật, 9 tháng 10, 2016

How to get return value in function sql by Java



Function in sql server
create function checkFunc(
 @MaSP varchar(10)
)
returns int
AS
BEGIN
 declare @kq int
 IF EXISTS(SELECT * FROM SANPHAM WHERE MaSP=@MaSP) set @kq=2
 ELSE 
  BEGIN
   set @kq = 0
  END
 return @kq
END
//execute
select dbo.checkFunc('sp02') 

How to get data by resultset in java

public int checkSanPham() {
  sql = "select dbo.checkFunc(?)";
  connection = connectDB.getConnect();
  int kq = 0;
  try {
   ps = connection.prepareStatement(sql);
   ps.setString(1, "sp02");
   rs = ps.executeQuery();
   if(rs.next()){
    System.out.println(rs.getInt(1));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    rs.close();
    ps.close();
    connection.close();
   } catch (Exception e2) {
   }
  }
  
  return kq;
 }


SQL.

How to use CallableStatement 

store-proceduce in sql

 
create proc sp_insert 
 @MaSP varchar(10),
 @TenSP varchar(50),
 @MaLoaiSP varchar(10),
 @GiaBan int,
 @SoLuong int
AS
BEGIN 
 --kiem tra Ma loai san pham co ton tai trong bang LOAISP
 IF EXISTS(SELECT TOP 1 MaLoaiSP FROM LOAISP WHERE MaLoaiSP = @MaLoaiSP)
  BEGIN
   -- Kiem tra neu k ton tai ma san pham thi cho insert
   IF NOT EXISTS(SELECT TOP 1 MaSP FROM SANPHAM WHERE MaSP = @MaSP)
    BEGIN
     INSERT INTO SANPHAM (MaSP,TenSP,MaLoaiSP,GiaBan,SoLuong) VALUES(@MaSP, @TenSP, @MaLoaiSP, @GiaBan, @SoLuong)
    END
   ELSE
    BEGIN
     print ('Trung khoa chinh')
    END
  END
 ELSE
  BEGIN
   print ('MaLoaiSP khong ton tai')
  END
END

--execute
exec sp_insert 'sp09','kaka','type001','10','10'

Insert method in Java
public void themSanPham(String maSP, String tenSP, String maLoaiSP,
   String giaBan, String soLuong) {
  sql = "{call sp_insert(?,?,?,?,?)}";
  connection = connectDB.getConnect();
  CallableStatement cs = null;
  try {
   cs = connection.prepareCall(sql);
   cs.setString(1, maSP);
   cs.setString(2, tenSP);
   cs.setString(3, maLoaiSP);
   cs.setInt(4, Integer.valueOf(giaBan));
   cs.setInt(5, Integer.valueOf(soLuong));
   int i = cs.executeUpdate();
   System.out.println(i);
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    cs.close();
    connection.close();
   } catch (Exception e2) {
   }
  }
  
 }

Khi update, or insert data by store-procedure thì khi update, insert thành công thì nó sẽ trả về giá trị 1, fail thì trả về -1. ta lấy giá trị này để xác định là thành công hay chưa.
                        int i = cs.executeUpdate();

System.out.println(i);

How to get return value in function sql by Java



Function in sql server
create function checkFunc(
 @MaSP varchar(10)
)
returns int
AS
BEGIN
 declare @kq int
 IF EXISTS(SELECT * FROM SANPHAM WHERE MaSP=@MaSP) set @kq=2
 ELSE 
  BEGIN
   set @kq = 0
  END
 return @kq
END
//execute
select dbo.checkFunc('sp02') 

How to get data by resultset in java

public int checkSanPham() {
  sql = "select dbo.checkFunc(?)";
  connection = connectDB.getConnect();
  int kq = 0;
  try {
   ps = connection.prepareStatement(sql);
   ps.setString(1, "sp02");
   rs = ps.executeQuery();
   if(rs.next()){
    System.out.println(rs.getInt(1));
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    rs.close();
    ps.close();
    connection.close();
   } catch (Exception e2) {
   }
  }
  
  return kq;
 }


SQL.

How to use CallableStatement 

store-proceduce in sql

 
create proc sp_insert 
 @MaSP varchar(10),
 @TenSP varchar(50),
 @MaLoaiSP varchar(10),
 @GiaBan int,
 @SoLuong int
AS
BEGIN 
 --kiem tra Ma loai san pham co ton tai trong bang LOAISP
 IF EXISTS(SELECT TOP 1 MaLoaiSP FROM LOAISP WHERE MaLoaiSP = @MaLoaiSP)
  BEGIN
   -- Kiem tra neu k ton tai ma san pham thi cho insert
   IF NOT EXISTS(SELECT TOP 1 MaSP FROM SANPHAM WHERE MaSP = @MaSP)
    BEGIN
     INSERT INTO SANPHAM (MaSP,TenSP,MaLoaiSP,GiaBan,SoLuong) VALUES(@MaSP, @TenSP, @MaLoaiSP, @GiaBan, @SoLuong)
    END
   ELSE
    BEGIN
     print ('Trung khoa chinh')
    END
  END
 ELSE
  BEGIN
   print ('MaLoaiSP khong ton tai')
  END
END

--execute
exec sp_insert 'sp09','kaka','type001','10','10'

Insert method in Java
public void themSanPham(String maSP, String tenSP, String maLoaiSP,
   String giaBan, String soLuong) {
  sql = "{call sp_insert(?,?,?,?,?)}";
  connection = connectDB.getConnect();
  CallableStatement cs = null;
  try {
   cs = connection.prepareCall(sql);
   cs.setString(1, maSP);
   cs.setString(2, tenSP);
   cs.setString(3, maLoaiSP);
   cs.setInt(4, Integer.valueOf(giaBan));
   cs.setInt(5, Integer.valueOf(soLuong));
   int i = cs.executeUpdate();
   System.out.println(i);
   if(i > 0){
    System.out.println("update thanh cong");
   } else {
    System.out.println("fail update!");
   }
  } catch (SQLException e) {
   e.printStackTrace();
  } finally {
   try {
    cs.close();
    connection.close();
   } catch (Exception e2) {
   }
  }
  
 }

Khi update, or insert data by store-procedure thì khi update, insert thành công thì nó sẽ trả về giá trị 1, fail thì trả về -1. ta lấy giá trị này để xác định là thành công hay chưa.
                        int i = cs.executeUpdate();

System.out.println(i);

Thứ Bảy, 1 tháng 10, 2016

Một số kĩ thuật tối ưu hoá mã nguồn Java - Phần 1

Trong Java việc tối ưu hoá mã nguồn Java là công việc rất quan trọng, nó không chỉ giúp mã nguồn thông thoáng hơn, giúp tiêu tốn ít tài nguyên hệ thống hơn, mà các kĩ thuật được trình bày dưới đây sẽ giúp nâng cao hiệu suất (performance) làm việc của Java khi chạy chương trình!
Một LTV Java có kinh nghiệm luôn coi việc tối ưu hoá mã nguồn như là 1 phần quan trọng của công việc lập trình, trang bị những kĩ thuật, thủ thuật tối ưu sẽ thể hiện một LTV có trình độ, và coi nó như 1 kĩ năng không thể thiếu khi làm việc với Java.
Các kĩ thuật dưới đây không phải là những giải thuật toán học cao siêu, cũng không phải triển khai 1 cách phức tạp, đôi khi chúng rất dễ để thực hiện nhưng rất nhiều LTV không để ý hoặc chưa biết cách để triển khai nó.

I. Các vòng lặp

1. Tránh việc gọi phương thức trong vòng lặp

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Nếu có thể hãy tránh sử dụng cuộc gọi tới các phương thức như length(), size() ... trong vòng lặp, nó sẽ giúp cải thiện hiệu suất
  • Lý do: Việc sử dụng các phương thức trên trong vòng lặp sẽ gây ra rất nhiều cuộc gọi giống nhau và không cần thiết làm tăng xử lý cho chương trình
Ví dụ:
Java
package com.rule;
class Avoid_method_calls_in_loop_violation
{
public void method()
{
        String str = "Hello";
        for (int i = 0; i < str.length(); i++)   // Vi Phạm
        {
          i++;
        }
}
}
Nên được viết thành:
Java
package com.rule;
class Avoid_method_calls_in_loop_correction
{
    public void method()
    {
        String str = "Hello";
        int len = str.length();     // Đẩy size ra bên ngoài vòng lặp
        for (int i = 0; i < len ; i++)
        {
            i++;
        }
 }
}

2. Đưa các tính tóan bất biến ra ngoài vòng lặp

  • Mức độ nghiêm trọng: Trung Bình
  • Nguyên tắc: Các mã nguồn luôn cho ra một kết quả giống nhau qua từng lần lặp nên được di chuyển ra ngoài vòng lặp
  • Lý do: Các tính tóan cho ra kết quả giống nhau, bất biến là không cần thiết phải gọi lại nhiều lần gây ra sự chậm trễ cho chương trình
Ví dụ 1:
Java
package com.rule;
class Loop_invariant_code_motion_violation
{
    public void method(int x, int y, int[] z)
    {
        for(int i = 0; i < z.length; i++)
        {
            z[i] = x * Math.abs(y);     // Vi phạm
        }
    }
}
Nên được viết thành:
Java
package com.rule;
class Loop_invariant_code_motion_correction
{
    public void method(int x, int y, int[] z)
    {
                    int t1 = x * Math.abs(y);       // Điều chỉnh
        for(int i = 0; i < z.length; i++)
        {
            z[i] = t1;
        }
    }
}
Ví dụ 2:
Java
public NamedElement find(NamedElement namedElement)
{ 
     for (NamedElement otherNamedElement : getNamedElements()) { 
          if (namedElement.getName().equals(otherNamedElement.getName())) { //Vi phạm
               return otherNamedElement; 
          }
     } 
               return null; 
}
Nên viết thành:
Java
public NamedElement find(NamedElement namedElement)
{ 
     String name = namedElement.getName(); // Chỉnh sửa
     for (NamedElement otherNamedElement : getNamedElements()) { 
         if (name.equals(otherNamedElement.getName())) { // Chỉnh sửa
             return otherNamedElement; 
         }
     } 
             return null; 
}
Tham khảo:
  • Java Performance Tunning by Jack Shirazi
  • The Art of Java Performance Tuning by Ed Merks (Page 20)

3. Tránh nối chuỗi (String) trong vòng lặp

  • Mức độ nghiêm trọng: Rất nghiêm trọng
  • Nguyên tắc: Sử dụng StringBuffer hoặc StringBuilder thay vì sử dụng nối chuỗi String trong vòng lặp
  • Lý do: String là 1 đối tượng bất biến, mỗi khi nối chuỗi gây ra tạo mới đối tượng String, số lần lặp càng lớn càng gây chậm trễ chương trình, tốn tài nguyên xử lý
Ví dụ:
Java
package com.rule;
class String_concatenation_violation
{
    public void concatValues()
    {
        String result = "";
        for (int i = 0; i < 20; i++) 
        {
              result += getNextString();                // Vi phạm
        }
    }
}
Nên được viết thành:
Java
package com.rule;
class String_concatenation_correction
{
    public void concatValues(String strMainString, String strAppend1, String strAppend2)
    {
        String result = "";
StringBuilder builder = new StringBuilder();
for (int i = 0; i < 20; i++) 
 {
     builder.append(getNextString()); // Sử dụng StringBuilder
 }
 result = buffer.toString(); 
 }
}

4. Sử dụng biến tạm thay cho truy cập giá trị của phần tử trong mảng

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Sử dụng biến tạm để lưu trữ dữ liệu và tính toán thay vì truy cập tới giá trị của phần tử trong mảng
  • Lý do: Việc truy cập tới 1 phần tử trong mảng luôn làm tốn chi phí hơn là truy cập tới 1 biến tạm bởi vì máy ảo Java (VM) phải kiểm tra các ràng buộc truy cập tới phần tử đó cũng như phải kiểm tra độ dài của mảng
Ví dụ:
Java
public void setUpArray(int REPEAT) {
     for(int i = 0; i < REPEAT; i++) {
        countArr[0] += 10; // VI PHẠM
     }     
}
Nên viết thành:
Java
public void setUpArray(int REPEAT) {
     int tempCound = coundArr[0]; // ĐẨY VÀO BIẾN TẠM
     for(int i = 0; i < REPEAT; i++) {
       tempCound += 10;  // SỬA CHỮA
        countArr[0] = tempCount; // SỮA CHỮA
     }     
}
Tham khảo:
  • Java Performance Tunning 2nd, by Jack Shirazi (Chapter 7 - Page: 194)

5. Sử dụng Int

  • Mức độ nghiêm trọng: Trung Bình
  • Nguyên tắc: Sử dụng kiểu dữ liệu int cho các chỉ số thay vì sử dụng các kiểu dữ liệu khác
  • Lý do: Sử dụng kiểu dữ liệu Int cho các chỉ số là nhanh nhất so với việc sử dụng bất kì kiểu dữ liệu nào khác. VM được tối ưu để sử dụng kiểu Int.
Ví dụ:
Sử dụng int :
Java
        for(int i = 0; i < Repeat; i++)
là nhanh hơn so với sử dụng bất kì kiểu dữ liệu nào khác
Java
     for(long i = 0; i < Repeat; i++)
     for(double i = 0; i < Repeat; i++)
     for(char i = 0; i < Repeat; i++)
Tham khảo:
  • Java Performance Tunning 2nd, by Jack Shirazi (Chapter 7 - 7.1.4)

6. Đặt Try Catch ra ngoài vòng lặp

  • Mức độ nghiêm trọng: Trung Bình
  • Nguyên tắc: Đặt các khối Try/Catch/Finally bên trong vòng lặp có thể làm chậm quá trình thực thi của chương trình
Ví dụ:
Java
package com.rule;
import java.io.InputStream;
import java.io.IOException;
class Place_try_catch_out_of_loop_violation
{
    void method (InputStream is)
     {
         int ZERO = 0;
         int TEN = 10;
         int count = 0;
         for (int i = ZERO; i < TEN; i++)
         {
             try            // VIOLATION
             {
                 count += is.read();
             }
             catch (IOException ioe)
             {
                ioe.printStackTrace();
             }
         }
 }
}
Nên viết thành:
Java
package com.rule;
import java.io.InputStream;
import java.io.IOException;
class Place_try_catch_out_of_loop_correction
{
  void method (InputStream is)
     {
         int ZERO = 0;
         int TEN = 10;
         int count = 0;
         try            // Bao Try Catch ra ngoài vòng lặp
         {
             for (int i = ZERO; i < TEN; i++)
             {
                     count += is.read ();
             }
         }
         catch (IOException ioe)
         {
            ioe.printStackTrace();
         }
 }
}

II/ Làm việc với chuỗi

1. Sử dụng String.length() để kiểm tra chuỗi rỗng (empty)

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Sử dụng String.length() để kiểm tra chuỗi rỗng thay vì sử dụng String.equal()
  • Lý do: Phương thức String.equals () là quá mức cần thiết để kiểm tra một chuỗi rỗng. Kiểm tra độ dài của chuỗi là 0 sử dụng String.length() sẽ nhanh hơn.
Ví dụ:
Java
package com.rule;
class Use_String_length_to_compare_empty_string_violation
{
    public boolean isDocEmpty()
    {
                return doc.getContents().equals("");      // VI PHẠM
    }
}
Nên viết thành:
Java
package com.rule;
class Use_String_length_to_compare_empty_string_correction
{
    public boolean isDocEmpty()
    {
                return doc.getContents().length() == 0;     // SỬ DỤNG LENGTH
    }
}
Tham khảo:

2. Sử dụng StringBuffer hoặc StringBuilder (Tham khảo 3. Tránh nối chuỗi (String) trong vòng lặp)

3. Sử dụng equalsIgnoreCase

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Sử dụng phương thức equalsIgnoreCase() để so sánh chuỗi mà không phân biệt hoa thường
Ví dụ:
Java
public class Use_String_equalsIgnoreCase_violation
{
    public void method()
    {
        String str = "APPPERFECT";
        String str1 = "appperfect";
        if(str1.toUpperCase().equals(str))      // Violation
        {
            System.out.println("Strings are equals");
        }   
    }
}
Nên được viết thành:
Java
package com.rule;
public class Use_String_equalsIgnoreCase_correction
{
    public void method()
    {
        String str = "APPPERFECT";
        String str1 = "appperfect";
        if(str1.equalsIgnoreCase(str))      // Correction.
        {
            System.out.println("Strings are equals");
        }   
    }
}

4. Tránh sử dụng startsWith()

  • Mức độ nghiêm trọng: Rất Cao
  • Nguyên tắc: Tránh gọi String.startsWith () vì lý do hiệu suất
  • Lý do: Trong native mã nguồn Java String.startsWith () xử lý khá nhiều thứ truớc khi cho ra kết quả, vì vậy giới hạn vùng kiểm tra bằng cách kiểm tra ký tự ở vị trí đầu tiên
Ví dụ:
Java
package com.rule;
class Avoid_startsWith_violation
{
    public void method()
    {
        String sTemp="Data";
        if (sTemp.startsWith("D")) // VIOLATION
        {
            sTemp = "data";
        }
    }
}
Nên được viết thành
Java
package com.rule;
class Avoid_startsWith_correction
{
    public void method()
    {
        final int ZERO = 0;
        final char D = 'D';
        String sTemp="Data"; 
        if (sTemp.length () > ZERO && sTemp.charAt(ZERO) == D)  // CORRECTION
        {
            sTemp = "data"; 
        }
    }
}

III/ Các trường hợp khác

1. Sử dụng System.arraycopy()

  • Nguyên tắc: Sử dụng System.arraycopy() cho việc sao chép mảng
  • Lý do: Sử dụng System.arraycopy() là nhanh hơn so với việc sử dụng vòng lặp để sao chép 1 mảng
Ví dụ:
Java
package com.rule;
class Use_System_arrayCopy_violation
{
    public int[] copyArray (int[] array)
    {
        int length = array.length;
        int[] copy = new int [length];
        for(int i=0;i<length;i++)
        {
            copy [i] = array[i];        // VIOLATION
        }
        return copy;
    }
}
Nên viết thành:
Java
package com.rule;
class Use_System_arrayCopy_correction
{
    public int[] copyArray (int[] array)
    {
        final int ZERO = 0;
        int length = array.length;
        int[] copy = new int [length];
        System.arraycopy(array, ZERO, copy, ZERO, length);      // CORRECTION
        return copy;
    }
}

2. Tránh lặp lại việc Casting (ép kiểu)

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Chỉ ép kiểu 1 lần sau đó giữ tham chiếu của nó
  • Lý do: Việc lặp đi lặp lại nhiều lần việc ép kiểu khiến việc xử lý chậm trễ hơn
Ví dụ:
Java
package com.rule;
import java.awt.Component;
import java.awt.TextField;
class Avoid_repeated_casting_violation
{
    public void method(Component comp)
    {
        ((TextField) comp).setText("");       // VIOLATION
        ((TextField) comp).setEditable(false);  // VIOLATION
    }
}
Nên viết thành:
Java
package com.rule;
import java.awt.Component;
import java.awt.TextField;
class Avoid_repeated_casting_correction
{
    public void method(Component comp)
    {
        final TextField tf = (TextField) comp;  // CORRECTION
        tf.setText("");
        tf.setEditable(false);
    }
}
Tham khảo: Java Performance tunning by Jack Shirazi

3. Hủy các cấu trúc if else không cần thiết

  • Mức độ nghiêm trọng: Trung Bình
  • Nguyên tắc: Đơn giản hóa để nâng cao hiệu quả của mã và giảm kích thước của nó.
Ví dụ:
Java
package com.rule;
class Use_ternary_operator_correction
{
    public boolean test(String value)
    {
        if(value.equals("AppPerfect"))        // VIOLATION
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
Nên viết thành:
Java
package com.rule;
class Use_ternary_operator_correction
{
    public boolean test(String value)
    {
          return value.equals("AppPerfect"); // CORRECTION
        }
 }

4. Luôn sử dụng static với hằng số

  • Mức độ nghiêm trọng: Bình thường
  • Nguyên tắc: Nên khai báo static với các biến là hằng số
Ví dụ:
Java
package com.rule;
public class Always_declare_constant_field_static_violation
{
    final int MAX = 1000; // VIOLATION
    final String NAME = "Noname"; // VIOLATION
}
Nên viết thành:
Java
package com.rule;
public class Always_declare_constant_field_static_correction
{
    static final int MAX = 1000; // CORRECTION
    static final String NAME = "Noname"; // VIOLATION
}

5. Tránh check null truớc khi sử dụng so sánh InstanceOf

  • Mức độ nghiêm trọng: Trung Bình
  • Nguyên tắc: Không nên check NULL truớc khi so sánh InstanceOf
  • Lý do: Không cần thiết phải check NULL trong truờwng hợp này, InstanceOf chỉ được so sánh khi đối tượng != NULL, nên việc so sánh là không cần thiết
Ví dụ:
Java
package com.rule;
public class Avoid_null_check_before_instanceof_violation
{
    public void method(Object o)
    {
        if(o != null &&  o instanceof Object)       // Violation.
        {
            // Do Something.
        }   
    }
}
Nên viết thành:
Java
package com.rule;
public class Avoid_null_check_before_instanceof_correction
{
    public void method(Object o)
    {
        if(o instanceof Object)     // Correction
        {
            // Do Something.
        }   
    }
}

6. Sử dụng mảng dữ liệu nguyên thủy

  • Mức độ nghiêm trọng: Trung Bình
  • Nguyên tắc: Sử dụng mảng dữ liệu nguyên thủy thay vì sử dụng Collection
  • Lý do: Collection có thể chứa trong nó nhiều kiểu dữ liệu (kiểu object, nguyên thủy ...), vì vậy sử dụng mảng nguyên thủy giúp nâng cao hiệu suất chương trình
Ví dụ:
Java
package com.rule;
import java.util.Vector;
public class Use_array_of_primitive_type_insteadof_collection_violation
{
    public void method()
    {
        Vector vInt = new Vector(); // VIOLATION
        vInt.add(new Integer(1));
        vInt.add(new Integer(2));
        vInt.add(new Integer(3));
    }
}
Nên viết thành:
Java
package com.rule;
public class Use_array_of_primitive_type_insteadof_collection_correction
{
    public void method()
    {
        int[] arrInt = new int[3]; // CORRECTION
        arrInt[0] = 1;
        arrInt[1] = 2;
        arrInt[2] = 3;
    }
}

7. Tránh show Debug code

  • Mức độ nghiêm trọng: Thấp
  • Nguyên tắc: Nên check Debug truớc khi show Log
  • Lý do: Việc Debugger vốn bản chất chỉ dành cho nhà phát triển, vì vậy hãy loại bỏ hoặc kiểm tra chúng truớc khi thực thi
Ví dụ:
Java
package com.rule;
public void Avoid_debugging_code_violation
{
    private int count =0;
    public int getCount()
    {
        //...
        System.out.println("count ="+count);  // Violation
        return count;
    }
}
Nên viết thành:
Java
package com.rule;
public void Avoid_debugging_code_correction
{
    private int count =0;
    public int getCount()
    {
        if (Debug.ON) 
        {
            System.out.println("count ="+count);  //correction
        }
        return count;
    }
}

8. Tránh việc sử dụng Thread.sleep()

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Không nên sử dụng Thread.sleep vì lý do hiệu suất
Ví dụ:
Java
package com.rule;
class Avoid_call_to_Thread_sleep_violation
{
    public void doTest() throws InterruptedException
    {
        //......
        Thread.sleep(100); // VIOLATION
        //......
    }
}
Nên viết thành:
Java
package com.rule;
class Avoid_call_to_Thread_sleep_correction
{
    public void doTest()
    {
        //......
        this.wait(100); // CORRECTION
        //......
    }
}

8. Tránh sử dụng String.charAt()

  • Mức độ nghiêm trọng: Cao
  • Nguyên tắc: Sử dụng mảng char[] thay vì sử dụng String.charAt();
Ví dụ:
Java
package com.rule;
class Avoid_using_String_charAt_violation
{
    public void method(String str)
    {
        for(int i=0; i<str.length(); i++)
        {
            System.out.println(str.charAt(i));  // VIOLATION
        }
    }
}
Nên viết thành:
Java
package com.rule;
class Avoid_using_String_charAt_correction
{
    public void method(String str)
    {
        char[] carr = str.toCharArray();        // CORRECTION
        for(int i=0; i<carr.length; i++)
        {
            System.out.println(carr[i]);        // CORRECTION
        }
    }
}
Tham khảo:

Trong bài này có sử dụng tài liệu tham khảo:

Nguồn: