6
\$\begingroup\$

I'm working on an android application, but this is basically plain 'ole java.

The aim is to do the right thing and not repeat myself.

public enum Datafile {
    ACTIVITIES() {
        @Override public String filepath() {
            return "activities.yaml";
        }
    }, 
    OPEN_LOOPS() {
        @Override public String filepath() {
            return "open_loops.yaml";
        }
    };

    public abstract String filepath();
}

Later on, when I want to edit the file I try this

File file = getDataFile(Datafile.OPEN_LOOPS);

Which calls this...

private File getDataFile(Datafile datafile) {
    File file = null;

    String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    file = new File(baseDir + "/" + datafile.filepath());

    return file;
}

Points for a better name for the enum and good advice regarding who responsibility for the getFileData method (currently all this code is in an Activity) really lies (a new class).

\$\endgroup\$
  • \$\begingroup\$ where is/was your Datafile enum used, other than in getDataFile ? \$\endgroup\$ – David Oct 3 '11 at 7:07
  • \$\begingroup\$ Sorry, only just read your comment David, it is only used in getDataFile. \$\endgroup\$ – bluekeys Oct 5 '11 at 7:58
  • \$\begingroup\$ Ok, thanks, then I can +1 Eric Karl's answer :) \$\endgroup\$ – David Oct 5 '11 at 8:06
5
\$\begingroup\$

If it is truly static, then yes, the enum is probably just fine. However, it's not very flexible. What if your path changes? Change the enum, recompile, and redistribute? Youch!

Or, you can use an XML file as a config. Change? Change it in the XML file and distribute. Smaller file transfers, among other benefits.

\$\endgroup\$
  • \$\begingroup\$ I'm leaning toward this answer with regard best-practice. In pratice, the app is only running (i use the term 'run' loosely) in debug mode on my own phone but in the future it couldn't be reasonably redistributed without a config file. \$\endgroup\$ – bluekeys Oct 5 '11 at 8:02
  • \$\begingroup\$ if you code it in javascript you won't have to recompile it ever. what if the file is not needed any more? xml won't fix that. \$\endgroup\$ – IAdapter Oct 14 '11 at 21:09
14
\$\begingroup\$

Enum are a good way to do this in my opinion if this is really static.

Might I suggest you do:

public enum Datafile {
    ACTIVITIES("activities.yaml"),
    OPEN_LOOPS("open_loops.yaml");

private String filepath;

private Datafile(String filepath) {
    this.filepath = filepath;
}

public String getFilepath() {
    return filepath;
}

This is a bit more readable than the overriden methods in each declaration, especially with long enums.

If the values of the filepaths are subject to changes from time to time. I suggest using a properties file. These can be reloaded without recompiling you package, meaning non-developers could maintain them. That is useful in some team setups.

EDIT: For properties, I suggest you read the java properties tutorial: http://download.oracle.com/javase/tutorial/essential/environment/properties.html. There are also a lot of already well written tutorials about them and I could never hope to match their quality here. Also, never having worked on Android, I do not know if google has provided something that is more user friendly.

For small applications, I would go for enums personally, as they are very easy to manage and usually do the job pretty well.

\$\endgroup\$
  • \$\begingroup\$ Thanks, that is more readable. Can you talk a little more about using a properties file, newb here. \$\endgroup\$ – bluekeys Oct 2 '11 at 18:56
  • \$\begingroup\$ I have updated my answer with a link to the properties page of the java tutorial in the hope you find it helpful. \$\endgroup\$ – Eric-Karl Oct 2 '11 at 22:10
5
\$\begingroup\$

I shot myself in the foot several times when I hard-coded file names etc.

Now I put such "static" data usually in property files. That way it can be injected easily, too, e.g. using Guice.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.