0

In a rather loose way this question follows on from my previous one. The context here is building Android APKs with Phonegap CLI configured, via build-extras.gradle to use Java 7. Here is my code

public boolean execute(String action, JSONArray data, 
CallbackContext cbc) throws JSONException 
{
 Context ctxt = cordova.getActivity().getApplicationContext();
 // return doSave(data,cbc,ctxt);
 //the above compiles correctly 
 //doSave is a private method in the same class 
 switch(action)
 {
  case "save":return doSave(data,cbc,ctxt);break;
  //the compiler complains about an `unreachable statement`
  //other case statements ommitted for clarity
  default:cbc.error("Unknown action: "  + action);return false;
 }
 return false;
 //without this return the compiler is upset.
}

I am having some difficulty understanding two issues here

  1. As far as I can tell even without that last return I have defined a clear path of execution thanks to the switch...default clause so I cannot see why it requires a return statement there
  2. So inside the switch statement the private doSave method in the same class somehow becomes invisible?

I am coming back to Java after a long gap where I did only JS and PHP. However, I have done a great deal of Delphi coding at one time so I appreciate the rigor imposed by the Java compiler. In this instance though it seems to me that it is a bit excessive. Or perhaps I am misunderstanding something?

halfer
  • 19,824
  • 17
  • 99
  • 186
DroidOS
  • 8,530
  • 16
  • 99
  • 171
  • Side note: you are aware, that from a code quality point of view ... that the above is ... well, bad code? – GhostCat May 02 '16 at 07:56
  • @Jägermeister - thank you for the tip. Could you elaborate on what aspects of the code are bad. As I mentioned I am coming back to Java after a loooong break so I am probably doing several things not quite right. – DroidOS May 02 '16 at 07:58
  • Downvoter could you care to comment? – DroidOS May 02 '16 at 08:18
  • 1
    How you "fetch" your ctxt violates "law of emeter". Then: never do switch statements like that - do not use strings to represent commands (if at all, use enums instead); and beyond that: use polymorphism instead of such switch constructs (otherwise you are putting your logic on 'this command should do that' all over the place - but it should be in one place). If "save" is somehow a constant that has a certain meaning - at least make it a constant. Returning booleans ... normally bad idea. Finally: your names tell me nothing. Dont abbreviate; you are allowed to call a variable "callbackContext" – GhostCat May 02 '16 at 08:27

1 Answers1

2
return doSave(data,cbc,ctxt);break;

Your break statement is unreachable.

You should remove that statement, as well as the final return false; statement, which is also unreachable.

Eran
  • 387,369
  • 54
  • 702
  • 768