How to reorganize this method, which has several if / else statements

I have the feeling that this if / else should be reorganized, but I'm not sure what I can do, or I just have to let it be what it is ...

private String someReportUrl(HttpServletRequest request, HttpServletResponse response) { String url; if (isBackToReportsSummary(request)) { url = SUMMARY_PAGE; getReportsSummary(request, response); } else if (isComingFromPageA(request)) { url = getTabUrl(request, REPORT_URL_FOR_PAGE_A); } else { url = "/standAlone/reportUrl.jsp"; } return url; } 

Basically, I have a report summary page that lists three to four reports. Firstly, if the condition is when the user wants to return to this page, the second condition refers to when the user selected this particular report, and the third condition to when the user selects this report as a separate report (and not from summary page)

+4
source share
4 answers

If you absolutely want to change it, you can initialize the url to the default value and only change it if one of the two conditions is true:

 private String someReportUrl(HttpServletRequest request, HttpServletResponse response) { String url = "/standAlone/reportUrl.jsp"; if (isBackToReportsSummary(request)) { url = SUMMARY_PAGE; getReportsSummary(request, response); } else if (isComingFromPageA(request)) { url = getTabUrl(request, REPORT_URL_FOR_PAGE_A); } return url; } 

But actually itโ€™s normal as it is.

+5
source

First take a look at the Design Pattern Command . It should reorganize the responsibility of if/else , making it more organized and much more convenient for maintenance. And then the code should look like this:

Example

 class ExampleServlet { private HashMap commandMap = new HashMap(); public ExampleServlet() { commandMap.put("create", new ActionTypeCreate()); commandMap.put("replace", new ActionTypeReplace()); commandMap.put("update", new ActionTypeUpdate()); commandMap.put("delete", new ActionTypeDelete()); } //endconstructor } //endclass: ExampleServlet private void performTask(String action) { ActionType cmd = (ActionType)commandMap.get(action); cmd.execute(); } //endmethod: performTask 

HERE You can get more knowledge in the command template

+6
source

How about this "protected" style? This often simplifies reading the method from top to bottom.

 private String someReportUrl(HttpServletRequest request, HttpServletResponse response) { if (isBackToReportsSummary(request)) { getReportsSummary(request, response); return SUMMARY_PAGE; } if (isComingFromPageA(request)) { return getTabUrl(request, REPORT_URL_FOR_PAGE_A); } return "/standAlone/reportUrl.jsp"; } 
+4
source

You code the code correctly. But you can also explore the use of the :: operator if you want to achieve the same on a single line.

Example:

 class round{ public static void main(String args[]){ int sampleInt=3; if(sampleInt==1){ sampleInt = 5; System.out.println("One"); } else if(sampleInt==2){ sampleInt = 3; System.out.println("Two"); } else{ sampleInt = 4; System.out.println("Else"); } sampleInt = sampleInt==1?5:(sampleInt==2?3:4); System.out.println("sampleInt "+sampleInt); } } 

At the end, your code will look something like this:

  url = isBackToReportsSummary(request)==true?SUMMARY_PAGE:(isComingFromPageA(request)==true?getTabUrl(request, REPORT_URL_FOR_PAGE_A):"/standAlone/reportUrl.jsp"); 
0
source

All Articles