Writing code, in my opinion, usually involves 2 kinds of code: logical and functional code.
While the logical part of the code always differs between every app and its goals, often the functional part doesn't change that much (especially the low level simple functional code) and is often reused a lot in the same project.
For example, in my app I use a lot of File related code. So I created a dedicated util class like this:
public final class FileUtils {
/* Constants */
private static int DEFAULT_BUFFER_SIZE = 1024;
public static final String PACKAGE_FILES_DIR = MyApplication.getContext().getFilesDir().getAbsolutePath(); // Usually: /data/data/package name/files
public static final String MAIN_LOCAL_STORAGE = Environment.getExternalStorageDirectory().getAbsolutePath(); // Usually:
/*
* ================
* Constructor
* ================
*/
private FileUtils() {}
/*
* ================
* Helper methods
* ================
*/
/**
* Write text to file.
* <a>{@see javadoc at the beginning of this class}</a>
*
* @param path the path
* @param fileName the file name
* @param text the text
*/
public static void writeTextToFile(String path, String fileName, String text) {
File file = new File(path, fileName.toString());
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
out.write(text.toString());
out.close();
fos.close();
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Check and create sub dierctory.
* <a>{@see javadoc at the beginning of this class}</a>
*
* @param path the path
* @param folderName the folder name
*/
public static void checkAndCreateSubDierctory(String path, String folderName) {
// Check and if needed, create logs subdirectory
File subDir = new File(path, folderName);
if (!subDir.exists()) {
subDir.mkdir();
}
}
public static void createZip(){
// create a zip including several files
}
}
The thing is, I haven't seen much code written this way. The pro's and con's that I see in this approach are:
Pro's:
Makes the logical part of the code much clearer
Functional code is not rewritten and is accessible from anywhere
- To my knowledge, the point of
static
is code that doesn't need to be instantiated and is common to all.
Con's:
- It can result in a very large amount of static code.
Another possible approach would be:
public class FileUtils {
/* Constants */
private static int DEFAULT_BUFFER_SIZE = 1024;
public static final String PACKAGE_FILES_DIR = MyApplication.getContext().getFilesDir().getAbsolutePath(); // Usually: /data/data/package name/files
public static final String MAIN_LOCAL_STORAGE = Environment.getExternalStorageDirectory().getAbsolutePath(); // Usually:
/*
* ================
* Helper methods
* ================
*/
/**
* Write text to file.
* <a>{@see javadoc at the beginning of this class}</a>
*
* @param path the path
* @param fileName the file name
* @param text the text
*/
public void writeTextToFile(String path, String fileName, String text) {
File file = new File(path, fileName.toString());
try {
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
out.write(text.toString());
out.close();
fos.close();
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Check and create sub dierctory.
* <a>{@see javadoc at the beginning of this class}</a>
*
* @param path the path
* @param folderName the folder name
*/
public void checkAndCreateSubDierctory(String path, String folderName) {
// Check and if needed, create logs subdirectory
File subDir = new File(path, folderName);
if (!subDir.exists()) {
subDir.mkdir();
}
}
public void createZip(){}
}
Pro's:
- Doesn't use static code
Con's:
- Every time I would like to use the code, I would need to instantiate the class, something that could lead to a creation of a lot of objects
Of course, there is always the option to just write the code locally in every class, but that to my knowledge also violate OOP principles and results in a greater amount of code that could be saved...
I searched about this a lot, and couldn't find concrete answers or approach.
Is this an acceptable approach to Object Oriented Programming?
Can someone explain why and what are alternatives to this approach?