databasePath
,databaseFile,
databaseBaseURL,, databaseBaseURL
databaseVersionURL`,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";
According to the previous Code Conventions,
init_database
should beinitDatabase
.If
databaseBaseURL
anddatabaseVersionURL
hadn't be constant I'd named them asdatabaseBaseUrl
anddatabaseBaseVersionUrl
. 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
HTTPURL
or HttpUrlHttpUrl
?-
public DatabaseManager() { database = null; }
Close your streams in a
finally
block. In case of a previous errors they won't be closed.The code does
databasePath + databaseFile
more than once. Create a method for that.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 thisInputStreamReader
uses the default charset which could vary from system to system.BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
I'd use Commons IO's IOUtils for the copying. It has a
copy(InputStream input, OutputStream output)
which you could use in theDownloadDatabase
task and you could usecopy(InputStream input, Writer output)
in theDatabaseVersionCheck
task if you replace theStringBuilder
to aStringWriter
.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(); }
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";
According to the previous Code Conventions,
init_database
should beinitDatabase
.If
databaseBaseURL
anddatabaseVersionURL
hadn't be constant I'd named them asdatabaseBaseUrl
anddatabaseBaseVersionUrl
. 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?
-
public DatabaseManager() { database = null; }
Close your streams in a
finally
block. In case of a previous errors they won't be closed.The code does
databasePath + databaseFile
more than once. Create a method for that.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 thisInputStreamReader
uses the default charset which could vary from system to system.BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
I'd use Commons IO's IOUtils for the copying. It has a
copy(InputStream input, OutputStream output)
which you could use in theDownloadDatabase
task and you could usecopy(InputStream input, Writer output)
in theDatabaseVersionCheck
task if you replace theStringBuilder
to aStringWriter
.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(); }
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";
According to the previous Code Conventions,
init_database
should beinitDatabase
.If
databaseBaseURL
anddatabaseVersionURL
hadn't be constant I'd named them asdatabaseBaseUrl
anddatabaseBaseVersionUrl
. 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
orHttpUrl
?-
public DatabaseManager() { database = null; }
Close your streams in a
finally
block. In case of a previous errors they won't be closed.The code does
databasePath + databaseFile
more than once. Create a method for that.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 thisInputStreamReader
uses the default charset which could vary from system to system.BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
I'd use Commons IO's IOUtils for the copying. It has a
copy(InputStream input, OutputStream output)
which you could use in theDownloadDatabase
task and you could usecopy(InputStream input, Writer output)
in theDatabaseVersionCheck
task if you replace theStringBuilder
to aStringWriter
.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(); }
Some generic Java notes, since I'm not too familiar with Android.
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
According to the previous Code Conventions,
init_database
should beinitDatabase
.If
databaseBaseURL
anddatabaseVersionURL
hadn't be constant I'd named them asdatabaseBaseUrl
anddatabaseBaseVersionUrl
. 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?
-
public DatabaseManager() { database = null; }
Initializing fields with null
is unnecessary since null
is the default value of references.
I'd call the
StringBuilder
in thedoInBackground
method asresult
.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`.
Close your streams in a
finally
block. In case of a previous errors they won't be closed.The code does
databasePath + databaseFile
more than once. Create a method for that.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 thisInputStreamReader
uses the default charset which could vary from system to system.BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
I'd use Commons IO's IOUtils for the copying. It has a
copy(InputStream input, OutputStream output)
which you could use in theDownloadDatabase
task and you could usecopy(InputStream input, Writer output)
in theDatabaseVersionCheck
task if you replace theStringBuilder
to aStringWriter
.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
e.printStackTrace()
is not the best practice. Maybe you should inform the user about the error and/or log it.
- Why is exception.printStackTrace() considered bad practice?
- Is it a bad idea to use printStackTrace() in Android Exceptions?
Integer.parseInt
could throwNumberFormatException
. Maybe the code should handle it.