Vì vậy, tôi có chức năng định dạng ngày để ép buộc thành DateType {CURRENT, START, END} điều gì sẽ là cách tốt nhất để xử lý giá trị trả lại với trường hợp sử dụng lệnh chuyển đổiNém ngoại lệ so với giá trị null trả về bằng lệnh chuyển đổi
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");
}
}
HOẶC ném excpetion vào cuối
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");
}
HOẶC trả về 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;
}
Thực hành tốt nhất ở đây là gì? Ngoài ra tất cả các giá trị enum sẽ được xử lý trong trường hợp tuyên bố
Nếu bạn có điều kiện không bao giờ xảy ra, tôi sẽ sử dụng AssertionError. –
Cảm ơn AssertionError có ý nghĩa hơn nhiều so với IllegalArgumentException – Greg