Ich möchte meine Fehlermeldungen und Zeug in einer Datei zusammenführen, und meinen Code wenn möglich lesbarer machen.Verwenden der Aufzählung, um Fehlermeldungen lesbar darzustellen - ist es eine gute Übung?
Hier ist ein Beispiel dessen, was ich in meiner Enum-Datei:
public enum ZipErrorType {
// START: define exception messages (alphabetical order)
EMPTY_FILE_NAME_IN_LIST {
public String toString() {
return "One or more null/empty filename(s) found";
}
},
FILE_DOESNT_EXIST {
public String who(String sThisFile) {
return "[" + sThisFile + "] does not exist";
}
},
FILE_LIST_IS_NULL {
public String toString() {
return "File list is null/empty";
}
},
FILENAME_NOT_ABSOLUTE {
public String who(String sThisFile) {
return "[" + sThisFile + "] is not absolute";
}
},
MUST_BE_DIR {
public String who(String sThisFile) {
return "[" + sThisFile + "] must be a directory";
}
},
MUST_BE_FILE {
public String who(String sThisFile) {
return "[" + sThisFile + "] must be a file";
}
},
NULL_OR_EMPTY {
public String who(String sThisFile) {
return "[" + sThisFile + "] is null/empty";
}
},
OUTPUT_FILE_ALREADY_EXISTS {
public String who(String sThisFile) {
return "[" + sThisFile + "] already exists";
}
},
OUTPUT_FILENAME_EMPTY {
public String toString() {
return "Output filename is null/empty";
}
},
OUTPUT_PATH_EMPTY {
public String toString() {
return "Output path is null/empty";
}
},
// END: define exception messages
NONE {};
public String who(String sThisFile) { return ""; }
}
Dann in meinem Programm habe ich Code wie:
private static ZipErrorType getFileErrorsIfAny(String sFilename, boolean shouldBeFile) {
// check if given filename is absolute
File file = new File(sFilename);
if (!file.isAbsolute()) {
return ZipErrorType.FILENAME_NOT_ABSOLUTE;
}
// check if file exists
if (!file.exists()) {
return ZipErrorType.FILE_DOESNT_EXIST;
}
// check if corresponding file is a file when it shouldn't be...
if (file.isFile() && !shouldBeFile) {
return ZipErrorType.MUST_BE_DIR;
}
// ...or a directory when it should be a file
else if (file.isDirectory() && shouldBeFile) {
return ZipErrorType.MUST_BE_FILE;
}
return ZipErrorType.NONE;
}
... und ein Beispiel dafür, wie ich Gebrauch machen meine enum:
// check input files
for (String sFile : files) {
if (sFile == null || sFile.trim().length() == 0) {
throw new NullPointerException("One or more filename is null/empty");
}
errorIfAny = getFileErrorsIfAny(sFile.trim(), true);
if (!errorIfAny.equals(ZipErrorType.NONE)) {
throw new ZipInputException(errorIfAny.who(sFile.trim()));
}
}
Jetzt weiß ich, es ist schwer, allein nur durch diesen Code-Schnipsel zu beurteilen, aber das ist in Ordnung, aus einem ge Nerale Perspektive? Ist das, was ich tue, nicht die Mühe wert, und gibt es einen Weg, dies zu verbessern?
Stellen Sie diese Frage unter http://codereview.stackexchange.com/. Es scheint, dass Sie keine Probleme oder Fragen haben, aber nur Peer-Review benötigen. – mthmulders
Ich bin nicht sicher, ob es besser ist oder nicht, aber in meinem aktuellen Projekt verwenden wir Enums auf die gleiche Weise ... aber die von jedem zurückgegebene Zeichenfolge ist nur eine Kennung für i18n –