3

I have code that defines behavior based on which input the code is handeling at that moment. The code looks something like:

switch(inputOption.name) {
 case 'NAME_1':
  switch(inputOption.type.toLowerCase()) {
    case 'radio':
      //some code
    case 'text':
      //some code
    ...
    case 'image':
      //some code

    default:
      return inputOption.value;
      break;
   }
  break;

  default:
    break;
 }

The code also includes some cascading cases. The default option causes the error. The error is listed as

    the default case is already defined

What is causing this error? The error shows up on the package folder, but the file shows no errors in the package view, but it shows errors when I open the file. I assumed it had something to do with the second default declaration, but it had no effect removing it.

user3334871
  • 1,251
  • 2
  • 14
  • 36
  • Which browser is this? Because it's most certainly a bug, the labels should be local to their scope. – Niels Keurentjes Oct 16 '14 at 22:32
  • [No you're not](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch#What_happens_if_I_forgot_a_break). – Niels Keurentjes Oct 16 '14 at 22:33
  • Not a browser error, it's showing as an error in eclipse itself. – user3334871 Oct 16 '14 at 22:33
  • Is it an error or a warning? I can understand the second (a missing `break` is *usually* a bug, or at least 'smelly code'), but not an error. That's also why the browsers don't complain. – Niels Keurentjes Oct 16 '14 at 22:34
  • It's listed as an error in eclipse. I'm pretty sure the browser isn't complaining, the submission is working as intended, I just don't understand why eclipse itself is telling me the default case has already been defined. – user3334871 Oct 16 '14 at 22:36
  • Eclipse is a steaming pile of poo. If it's giving bad errors like this, just [disable code inspection](http://stackoverflow.com/a/7931839/1729885). And then get a proper IDE like WebStorm of PhpStorm, depending on your main platform. – Niels Keurentjes Oct 16 '14 at 22:38
  • It's probably an eclipse bug. However, for clarity, I would break the inner switch into a separate function, then you won't confuse Eclipse. @NielsKeurentjes Eclipse is pretty bad at detecting JS errors. WebStorm is very good at detecting JS errors. – Ruan Mendes Oct 16 '14 at 22:38
  • @NielsKeurentjes Hmm...guess that's it then. I will let me team know. Thanks for the help! – user3334871 Oct 16 '14 at 22:40
  • @JuanMendes you're just putting nicely that Eclipse is indeed a steaming pile of poo compared to JetBrains' offerings ;) – Niels Keurentjes Oct 16 '14 at 22:41
  • @NielsKeurentjes Yes, I am forced to use euphemisms to keep others happy. And yes, I am an IntelliJ fan boy – Ruan Mendes Oct 16 '14 at 22:42
  • @NielsKeurentjes Unfortunately, eclipse is the standard where I work, and it's pretty strict at that. Hopefully saying "big pile of poo" will convince the team to use something else :) – user3334871 Oct 16 '14 at 22:45
  • If nothing else, download a trial. I was forced to use Eclipse until Android SDK support was built into IntelliJ. Downloaded the community edition, uninstalled Eclipse 2 days later, bought cake for the office 2 more days later to celebrate. Crappiest and most unstable unreliable IDE ever. – Niels Keurentjes Oct 16 '14 at 22:46
  • @user3334871 See this answer http://stackoverflow.com/questions/15931089/alternative-to-nested-switch-statements-in-java Many developers try to avoid nested switch statements and switch statements altogether. – Ruan Mendes Oct 17 '14 at 15:59

1 Answers1

0

You miss break statement for your outer case 'NAME_1'

switch(inputOption.name) {
 case 'NAME_1':
  switch(inputOption.type.toLowerCase()) {
    case 'radio':
      //some code
    case 'text':
      //some code
    ...
    case 'image':
      //some code

    default:
      return inputOption.value;
      break;
   }
   break; // <-------------------------------------------------- ADD THIS

  default:
    break;
 }
Maksym Kozlenko
  • 10,273
  • 2
  • 66
  • 55