Jose Fa

Greenhorn
+ Follow
since Feb 05, 2019
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
3
Given in last 30 days
3
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jose Fa

Campbell Ritchie wrote:

Jose Fa wrote:. . . for example sitch for a enum, and the precondition is that you always get this value of enum. . . .

Agree; if you are using enum elements as constants for the different cases, and you use every enum element as a label to acase, you will never reach default. But I can envisage two situations where that approach will fail:-
  • 1: Your user passes null.
  • 2: Somebody enhances the enum by adding another element.


  • Agree the first case can happen, but the second case really is that a human mistake, the develop forgot put the new values to the Switch, but do you think that those errors should be foreseen with code? there are many types of human errors, and in the development we work with preconditions, postconditions etc, because if we do not check everything everywhere, what do you think about this?

    Stephan van Hulst wrote:Most of my switch statements look roughly like this:



    I like this aproach, then you think that even if you have absolute control of data entry by preconditioning, it should be a correct option to set default?

    Campbell Ritchie wrote:Welcome to the Ranch

    Let's ask you a question. If your options run 1...7, what are you going to do it you get 0 or 8?Now, add a default to that. What are you going to do? Print a message, reassign a boolean, throw an exception? Maybe an exception is overkill, but without some other action, your user won't know to enter a new option.[code=java]do
    {
      int input = getInput(1, 7);
      switch (input)
      {
         case 1:
    ...



    But in this case you have a random input in this example you have otherwise input, but in a code that is under control, this not happen, for example sitch for a enum, and the precondition is that you always get this value of enum.
    I think that this example is different from the one.
    Thanks for your answer!
    I'm developing an app where I'm using a switch to handle events (it's a menu).

    Now there is a debate in the office about use or not to use the default, because it is a switch.

    I think that this is not like a mathematical function and not need an otherwise case, then I do not need put the default in the switch, like I not need put else in all my if.

    What do you think about this, if you know all cases for the switch, you would put the default or not?

    Thanks for the answers