Quindi ho una funzione che formatta una data per forzare a enum DateType dato {CURRENT, START, END} quale sarebbe il modo migliore per gestire il valore restituito con i casi che utilizzano l'istruzione switchEccezione di lancio e restituzione di valore nullo con istruzione switch
public static String format(Date date, DateType datetype) {
..validation checks
switch(datetype){
case CURRENT:{
return getFormattedDate(date, "yyyy-MM-dd hh:mm:ss");
}
...
default:throw new ("Something strange happend");
}
}
o gettare excpetion alla fine
public static String format(Date date, DateType datetype) {
..validation checks
switch(datetype){
case CURRENT:{
return getFormattedDate(date, "yyyy-MM-dd hh:mm:ss");
}
...
}
//It will never reach here, just to make compiler happy
throw new IllegalArgumentException("Something strange happend");
}
O restituire null
public static String format(Date date, DateType datetype) {
..validation checks
switch(datetype){
case CURRENT:{
return getFormattedDate(date, "yyyy-MM-dd hh:mm:ss");
}
...
}
return null;
}
Quale sarebbe la migliore pratica qui? Anche tutti i valori di enum saranno trattati nella dichiarazione del caso
Se si dispone di una condizione che non dovrebbe mai accadere, userei un'AsserzioneErrore. –
Grazie AssertionError ha molto più senso di IllegalArgumentException – Greg