13 Replies Latest reply: Sep 5, 2002 6:49 AM by 843853 RSS

    Why not introduce a "but" condition?

    843853
      Hi,
      why not introduce a "but" condition into the Java language?

      Here's an example:

      if(earth.isUnderAttack()){
      ���go to 10
      }but if(earth.getAttack().getAttacker().equals("alien") && earth.getStatus().equals("nice place")){
      ���go to 20
      }else{
      ���go to 30
      }

      Doesn't it look nicer, rather then doing it like this?

      if(earth.isUnderAttack()){
      ���if(earth.getAttack().getAttacker().equals("alien") && earth.getStatus().equals("nice place")){
      �������go to 20
      ���}else{
      �������go to 10
      ���}
      }else{
      ���go to 30
      }

      Cheers,
      Alexander H�glind
      alexander.hoglind@solberg.se
        • 1. Re: Why not introduce a "but" condition?
          843853
          Looks nice. It's the sort of thing Perl is fond of. but frankly, we're having a hard enough time getting blatantly useful features like foreach into Java.
          • 2. Re: Why not introduce a "but" condition?
            843853
            Hi,
            why not introduce a "but" condition into the Java
            language?

            Here's an example:
            what about 'else if'
            • 3. Re: Why not introduce a "but" condition?
              843853
              Well, the "else if" condition only excecutes if the first "if" condition is false. The "but if" condition will only excecute the first "if" condition if the "but if" condition is false... do you get it?

              I think this is the way we speek normaly:

              If earth is under attack then we would protect us the normal way. But if the attacker is an alien and earth is a nice place to defend, then we would attack them with our anti-alien force. Else If the earth isn�t under attack then take a bud and watch the game.

              rather then saying like this:

              if earth is under attack, then, if the attacker is and alien and earth is a nice place to defend then we would use our anti-alien force, else then we would protect us the normal way.
              Else If the earth isn�t under attack then take a bud and watch the game.


              Alexander H�glind
              alexander.hoglind@solberg.se
              • 4. Re: Why not introduce a "but" condition?
                843853
                if this: do A
                but if that: do B
                is surely the same as
                if that: do B
                else if this: do A
                Be careful about implying that the language that you code in should reflect the structure of the language that you speak, (particularly with a Swedish address). If you were to listen to the speech pattern of the South London geezers that I'm surrounded by you'd end up with a well dodgy language syntax, (whilst if you reflected the nuances of RP you'd find that you had a simply splendid means of expressing abstract thoughts).
                • 5. Re: Why not introduce a "but" condition?
                  843853
                  ok... I'll try to explain it on more time...

                  If the first "if" condition is true AND the "but if" condition is true then excecute "but if". But if the first "if" condition is true and the "but if" condition is false then excecute the first "if".
                  If the first "if" condition is false, then we don�t have to test the "but if" condition.

                  At the momement the only way to do that is like this:
                  if (isTrue){
                  if(isTrue)
                  doCommand(2)
                  else
                  doComand(1)
                  }

                  My idea is this:
                  if(isTrue)
                  doCommand(1)
                  but if(isTrue)
                  doCommand(2)


                  Remember, this is only an idea I got when I was programming and the code was hard to read.

                  shrink_laureate said something about Perl having it implemented in some kind of way. If they have, then for sure there�s a reason, don�t you think....?

                  And you should also be careful to imply that there is some universal idea of how programming code should look like. And for whatever it�s worth saying it... don�t you think that Java or C etc. is reflecting the structure of the language that you speak more than assembly does?


                  Alexander H�glind
                  alexander.hoglind@solberg.se
                  • 6. Re: Why not introduce a "but" condition?
                    843853
                    I can actually see your point but for some reason it's just not a comfortable construct for me. I think that it's because you're breaking up the sequence of execution, forcing the thread to look ahead to a following statement, (which may not even be there), before determining if the current conditional block should be entered. I'm not saying that this is necessarily a reason to avoid the construct but I'm unconvinced that it'll prove intuitive enough to do anything but cause maintenance problems. I don't know Perl but I'd be interested to hear if it causes problems there. Anyway, isn't this simply a clear place for decomposition? Take the clunky conditional code and stick it somewhere else. Back to work...
                    if (isTrue){
                      if(isTrue){
                        doCommand(2);
                      }
                      else{
                        doComand(1);
                      }
                    }
                    My idea is this:
                    if(isTrue){
                      doCommand(1);
                    }
                    but if(isTrue){
                      doCommand(2);
                    }
                    • 7. Re: Why not introduce a "but" condition?
                      843853
                      Yes, the code I wrote was a little bit clumsy... But whatever. Good that you finally got my point anyway! And yes, I don�t know if the "but if" condition is a good idea always... It may depend on what kind of programming you�re doing. And yes, it could actually be harder to read than the way we�re doing it now...

                      The reason for me asking for a "but if" condition is that with the "but if" condition, if you�re using correctly, you�ll have the commands you want to do in the right order. It may make it easier to read...

                      I'm more interested if the Perl implementation (if it exists) doesn�t causes problemns there.... ;)

                      ok.. back to holiday! Cheers!



                      • 8. Re: Why not introduce a "but" condition?
                        843853
                        Can you direct me to some website that show some examples of Perl or whatever language using this kind of condition?

                        What do you think of it. Could it be useful to have a "but if" condition or would it only confuse? If not now, could it be possible to have it implemented in the future.

                        Do you know if there is some way for me to help implementing the "but if" condition?
                        • 9. Re: Why not introduce a "but" condition?
                          843853
                          'but if' is a rediculous idea!

                          I'll tell you why. It means that you have to read all of the 'but if' statements before you know what the program is going to do. If you have 15 of them, you have to read all 15 before you know which code is likely to excecute.

                          With 'else if' you know exactly where you are each time you read the new if statement.

                          For many people who write extremely bad code, this could mean that the 'but if' could be in a completely different page. (Unortunately, there are such people).

                          So, if you want to make a language more unreadable, then 'but if' is a good idea. But maybe not for Java.

                          Rhys
                          • 10. Re: Why not introduce a "but" condition?
                            843853
                            Well... You should only be allowed to use "but if" one time, just as you are only allowed to use "else" one time.

                            The hard way:

                            if ( isTest1 ){
                                if ( isTest3 ){
                                 doCommand(3);
                               } else{
                                  doCommand(1);
                               }
                            } else ( isTest2 ){
                               if ( isTest3 ){
                                  doCommand(3);
                               } else {
                                  doCommand(2);
                               }
                            } else {
                               doCommand(4);
                            }


                            The "But if" way:

                            if ( isTest1 ){
                                doCommand(1);
                            }else if ( isTest2 ){
                               doCommand(2);
                            }but if ( isTest3 ){
                               doCommand(3);
                            } else {
                               doCommand(4);
                            }


                            Alex
                            • 11. Re: Why not introduce a "but" condition?
                              843853
                              He doesn't mean that Perl actually has it. But it's the sort of thing that Perl is fond of, and for which people are fond of perl.

                              For example:

                              print "Error!" unless (error == 0);

                              as a shortcut for:

                              if (error != 0) print "Error!";
                              • 12. Re: Why not introduce a "but" condition?
                                843853
                                ok! thanx!
                                that explains... Yes it's verry alike. It would be almost the same if you put it in a condition:

                                if(ERROR) print "error"
                                unless(SKIP)
                                • 13. Re: Why not introduce a "but" condition?
                                  843853
                                  Isn't 'but if' just a specific case of the preceeding if? In that case isn't it more elegant to start with the generic case and drill down to the specific:

                                  if (earth.isUnderAttack()) {
                                  if (earth.getAttack().isAttackerBlahBlahBlah()) {
                                  // do a specific thing
                                  }
                                  else {
                                  // do the generic thing
                                  }
                                  }

                                  'but if', 'unless', 'provided' all sound backwards, almost like an afterthought while programming to me.

                                  - Berni.