Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
improved formatting
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157
  1. databasePath, databaseFile, databaseBaseURL, , databaseBaseURLdatabaseVersionURL`, databaseVersionURL should be constants (all uppercase with words separated by underscores):

    private static final String DATABASE_PATH = "/data/data/com.sandbox.databaseone/databases/"; private static final String DATABASE_FILE = "dbone.sqlite"; private static final String DATABASE_BASE_URL = "http://www.redstalker.com/dbone/"; private static final String DATABASE_VERSION_URL = "version.txt";

     private static final String DATABASE_PATH = 
     "/data/data/com.sandbox.databaseone/databases/";
     private static final String DATABASE_FILE = "dbone.sqlite";
     private static final String DATABASE_BASE_URL = 
     "http://www.redstalker.com/dbone/";
     private static final String DATABASE_VERSION_URL =
     "version.txt";
    
  1. According to the previous Code Conventions, init_database should be initDatabase.

  2. If databaseBaseURL and databaseVersionURL hadn't be constant I'd named them as databaseBaseUrl and databaseBaseVersionUrl. From Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions:

    While uppercase may be more common, a strong argument can made in favor of capitalizing only the first letter: even if multiple acronyms occur back-to-back, you can still tell where one word starts and the next word ends. Which class name would you rather see, HTTPURLHTTPURL or HttpUrlHttpUrl?

  3.  public DatabaseManager()
     {
     database = null;
     }
    
  1. Close your streams in a finally block. In case of a previous errors they won't be closed.

  2. The code does databasePath + databaseFile more than once. Create a method for that.

  3. I don't know if it is applicable to Android or not, but in Java it's a good practice to pass the character set to the constructor of InputStreamReader. Without this InputStreamReader uses the default charset which could vary from system to system.

     BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    
  4. I'd use Commons IO's IOUtils for the copying. It has a copy(InputStream input, OutputStream output) which you could use in the DownloadDatabase task and you could use copy(InputStream input, Writer output) in the DatabaseVersionCheck task if you replace the StringBuilder to a StringWriter.

  5. In the DatabaseVersionCheck.onPostExecute I'd use guard clauses:

     @Override
     protected void onPostExecute(String result) {
     if(result == null) {
     return;
     }
     int version = Integer.parseInt(result);
     if(version <= currentVersion) {
     return;
     }
     nextVersion = version;
     DownloadDatabase d = new DownloadDatabase();
     d.execute();
     }
    
  1. databasePath, databaseFile, databaseBaseURL, databaseVersionURL` should be constants (all uppercase with words separated by underscores):

    private static final String DATABASE_PATH = "/data/data/com.sandbox.databaseone/databases/"; private static final String DATABASE_FILE = "dbone.sqlite"; private static final String DATABASE_BASE_URL = "http://www.redstalker.com/dbone/"; private static final String DATABASE_VERSION_URL = "version.txt";

  1. According to the previous Code Conventions, init_database should be initDatabase.

  2. If databaseBaseURL and databaseVersionURL hadn't be constant I'd named them as databaseBaseUrl and databaseBaseVersionUrl. From Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions:

    While uppercase may be more common, a strong argument can made in favor of capitalizing only the first letter: even if multiple acronyms occur back-to-back, you can still tell where one word starts and the next word ends. Which class name would you rather see, HTTPURL or HttpUrl?

  3.  public DatabaseManager()
     {
     database = null;
     }
    
  1. Close your streams in a finally block. In case of a previous errors they won't be closed.

  2. The code does databasePath + databaseFile more than once. Create a method for that.

  3. I don't know if it is applicable to Android or not, but in Java it's a good practice to pass the character set to the constructor of InputStreamReader. Without this InputStreamReader uses the default charset which could vary from system to system.

     BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    
  4. I'd use Commons IO's IOUtils for the copying. It has a copy(InputStream input, OutputStream output) which you could use in the DownloadDatabase task and you could use copy(InputStream input, Writer output) in the DatabaseVersionCheck task if you replace the StringBuilder to a StringWriter.

  5. In the DatabaseVersionCheck.onPostExecute I'd use guard clauses:

     @Override
     protected void onPostExecute(String result) {
     if(result == null) {
     return;
     }
     int version = Integer.parseInt(result);
     if(version <= currentVersion) {
     return;
     }
     nextVersion = version;
     DownloadDatabase d = new DownloadDatabase();
     d.execute();
     }
    
  1. databasePath, databaseFile, databaseBaseURL, databaseVersionURL should be constants (all uppercase with words separated by underscores):

     private static final String DATABASE_PATH = 
     "/data/data/com.sandbox.databaseone/databases/";
     private static final String DATABASE_FILE = "dbone.sqlite";
     private static final String DATABASE_BASE_URL = 
     "http://www.redstalker.com/dbone/";
     private static final String DATABASE_VERSION_URL =
     "version.txt";
    
  1. According to the previous Code Conventions, init_database should be initDatabase.

  2. If databaseBaseURL and databaseVersionURL hadn't be constant I'd named them as databaseBaseUrl and databaseBaseVersionUrl. From Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions:

    While uppercase may be more common, a strong argument can made in favor of capitalizing only the first letter: even if multiple acronyms occur back-to-back, you can still tell where one word starts and the next word ends. Which class name would you rather see, HTTPURL or HttpUrl?

  3.  public DatabaseManager()
     {
     database = null;
     }
    
  1. Close your streams in a finally block. In case of a previous errors they won't be closed.

  2. The code does databasePath + databaseFile more than once. Create a method for that.

  3. I don't know if it is applicable to Android or not, but in Java it's a good practice to pass the character set to the constructor of InputStreamReader. Without this InputStreamReader uses the default charset which could vary from system to system.

     BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    
  4. I'd use Commons IO's IOUtils for the copying. It has a copy(InputStream input, OutputStream output) which you could use in the DownloadDatabase task and you could use copy(InputStream input, Writer output) in the DatabaseVersionCheck task if you replace the StringBuilder to a StringWriter.

  5. In the DatabaseVersionCheck.onPostExecute I'd use guard clauses:

     @Override
     protected void onPostExecute(String result) {
     if(result == null) {
     return;
     }
     int version = Integer.parseInt(result);
     if(version <= currentVersion) {
     return;
     }
     nextVersion = version;
     DownloadDatabase d = new DownloadDatabase();
     d.execute();
     }
    
Source Link
palacsint
  • 30.4k
  • 9
  • 82
  • 157

Some generic Java notes, since I'm not too familiar with Android.

  1. databasePath, databaseFile, databaseBaseURL, databaseVersionURL` should be constants (all uppercase with words separated by underscores):

    private static final String DATABASE_PATH = "/data/data/com.sandbox.databaseone/databases/"; private static final String DATABASE_FILE = "dbone.sqlite"; private static final String DATABASE_BASE_URL = "http://www.redstalker.com/dbone/"; private static final String DATABASE_VERSION_URL = "version.txt";

Reference: Code Conventions for the Java Programming Language, 9 - Naming Conventions

  1. According to the previous Code Conventions, init_database should be initDatabase.

  2. If databaseBaseURL and databaseVersionURL hadn't be constant I'd named them as databaseBaseUrl and databaseBaseVersionUrl. From Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions:

    While uppercase may be more common, a strong argument can made in favor of capitalizing only the first letter: even if multiple acronyms occur back-to-back, you can still tell where one word starts and the next word ends. Which class name would you rather see, HTTPURL or HttpUrl?

  3.  public DatabaseManager()
     {
     database = null;
     }
    

Initializing fields with null is unnecessary since null is the default value of references.

  1. I'd call the StringBuilder in the doInBackground method as result.

     StringBuilder builder = new StringBuilder();
    

It would say what's the purpose of the object. For the same reason I'd rename the

- `boolean result` to `boolean success`,
- `in` to `responseStream`, and
- `d` to `downloadDatabase`.
  1. Close your streams in a finally block. In case of a previous errors they won't be closed.

  2. The code does databasePath + databaseFile more than once. Create a method for that.

  3. I don't know if it is applicable to Android or not, but in Java it's a good practice to pass the character set to the constructor of InputStreamReader. Without this InputStreamReader uses the default charset which could vary from system to system.

     BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
    
  4. I'd use Commons IO's IOUtils for the copying. It has a copy(InputStream input, OutputStream output) which you could use in the DownloadDatabase task and you could use copy(InputStream input, Writer output) in the DatabaseVersionCheck task if you replace the StringBuilder to a StringWriter.

  5. In the DatabaseVersionCheck.onPostExecute I'd use guard clauses:

     @Override
     protected void onPostExecute(String result)
     {
     if(result == null) {
     return;
     }
     int version = Integer.parseInt(result);
     if(version <= currentVersion) {
     return;
     }
     nextVersion = version;
     DownloadDatabase d = new DownloadDatabase();
     d.execute();
     }
    

It makes the code flatten and more readable. References: Replace Nested Conditional with Guard Clauses in Refactoring: Improving the Design of Existing Code; Flattening Arrow Code

  1. e.printStackTrace() is not the best practice. Maybe you should inform the user about the error and/or log it.
  1. Integer.parseInt could throw NumberFormatException. Maybe the code should handle it.
lang-java

AltStyle によって変換されたページ (->オリジナル) /