1 2 3 4 5 Previous Next 70 Replies Latest reply on Mar 10, 2010 6:33 PM by 796440 Go to original post
      • 30. Re: Ternary operator
        843807
        jduprez wrote:
        AFAIC, I'm happy that you're giving your point of view about your personal preference, or style. But I side with the others that there is nothing illogical in this design choice of the Java language per se (what is somewhat illogical is that the way return values can be silently ignored is not consistent with this).
        1+1
        Because what is java supposed to do with it?
        Well, just execute it
        Execute what? an operation that has no side-effect whatsoever?
        Well, maybe I would like to check how fast adding is executed without assigning?
        • 31. Re: Ternary operator
          843807
          jduprez wrote:
          Your example is another sort of Java's stronger checking, I see nothing awkward in it, but my mind is sure formatted after working with Java for a while (with a C >background mind you, and I'm happy I made the shift).
          I'm sorry but if someone tells me that s/he is happy after switching from C/C++ to Java/C# etc. I just feel like they choosing easy, safe ride which is unfortunately also very boring and without any excitement (just like driving ford fiesta) on the other hand, yes, driving ferrari (read C/C++) you may injure yourself or even die, but the emotions and power is something that is worth dying for. Thats why I prefer to drive ferrari to ford fiesta.
          • 32. Re: Ternary operator
            796440
            atch7 wrote:
            jduprez wrote:
            Your example is another sort of Java's stronger checking, I see nothing awkward in it, but my mind is sure formatted after working with Java for a while (with a C >background mind you, and I'm happy I made the shift).
            I'm sorry but if someone tells me that s/he is happy after switching from C/C++ to Java/C# etc. I just feel like they choosing easy, safe ride which is unfortunately also very boring and without any excitement (just like driving ford fiesta) on the other hand, yes, driving ferrari (read C/C++) you may injure yourself or even die, but the emotions and power is something that is worth dying for. Thats why I prefer to drive ferrari to ford fiesta.
            I don't drive a car for excitement. I drive it to get from point A to point B.

            And I don't write software for the cowboy thrill of it. I write it to produce something useful and to earn a living.

            And I use software for various reasons, and as a user of software, I would prefer that the creators of the software were able to focus more on solving the business problem at hand and less on the thrill of using the tool that they use to do their jobs.
            • 33. Re: Ternary operator
              796440
              atch7 wrote:
              It's another of java's illogicalities. In my code mentioned in original post if I have to use a ternary operator to do what I want it to do:
              1. I have to make sure that I'm using methods which return some value different than void and
              2. I have to assign it to some completely unnecessary variable.

              So following this logic I can't say:

              int == 0 ? setVisible(true) : setVisible(false);

              but providing that I have method definition:

              int setSelectable(int a)
              {
              //do something
              //and just for sake of making this to work I have to have return statement
              return a;
              }

              having that now I can write

              int a = i == 0 ? setSelectable(1) : setSelectable(0);

              Pathetic, illogical and completely java like behavior.
              This is the rant of a child who's bitter because the world doesn't work the way he assumes it should.

              The behavior only seems illogical to you because you have the mistaken assumption that the purpose of the operator is to be an equivalent to an if statement. That's not its purpose.
              • 34. Re: Ternary operator
                796440
                atch7 wrote:
                Another thing is that it is an operator just like '+' or '-' and as long as it have correct operands on each of its side it should work without making any problems.
                And that's exactly what it does.

                What you're attempting is the equivalent of this:
                public class Foo() {
                  public void x() {}
                
                  public void y() {}
                }
                
                ...
                
                Foo foo = new Foo();
                int z = foo.x() + foo.y();
                • 35. Re: Ternary operator
                  796440
                  atch7 wrote:
                  kevinaworkman wrote:
                  atch7 wrote:
                  Another thing is that it is an operator just like '+++' or '-' and as long as it have correct operands on each of its side it should work without making any problems.
                  What? Why? Says who? This doesn't make any sense.
                  Exactly. Who and What. As Java never has been standardized and for some strange reasons it doesn't look like it will be in near by future.
                  What do you even mean by this? In what way is Java "not standardized" that C++ is?
                  • 36. Re: Ternary operator
                    796440
                    jduprez wrote:
                    although I'd be glader still if it forbid mere doSomething(); lines when this latter's return type is anything other than void
                    Why? There are plenty of times when I only want the side-effect of a method, and don't need the return value. Why should I have to assign it if I'm not going to use it?
                    • 37. Re: Ternary operator
                      796440
                      atch7 wrote:
                      jduprez wrote:
                      AFAIC, I'm happy that you're giving your point of view about your personal preference, or style. But I side with the others that there is nothing illogical in this design choice of the Java language per se (what is somewhat illogical is that the way return values can be silently ignored is not consistent with this).
                      1+1
                      Because what is java supposed to do with it?
                      Well, just execute it
                      Execute what? an operation that has no side-effect whatsoever?
                      Well, maybe I would like to check how fast adding is executed without assigning?
                      So, the language should support useless syntax in order to enable you to do a useless benchmark?
                      • 38. Re: Ternary operator
                        843807
                        Guys...hallo? The discussion should have had ended with Darryl's answer.
                        • 39. Re: Ternary operator
                          jduprez
                          although I'd be glader still if it forbid mere doSomething(); lines when this latter's return type is anything other than void
                          Why? There are plenty of times when I only want the side-effect of a method, and don't need the return value. Why should I have to assign it if I'm not going to use it?
                          Mmm.
                          First, one may question whether the callee is appropriately designed: there was a trend at some point that argued that methods should either read/compute something or change something, but not both (bear with my vague memory, I'm not sure I'll be able to find this article in the Internet history). Although as all rules, that should not be taken to the extreme and blindly obeyed (in particular "tell don't ask" would sometimes conflict with it), I find following it makes methods simpler, and helps avoiding that a method do "too much".

                          Second, if a method nevertheless both has a side effect and returns a result, disregarding that latter may be an oversight merely than a deliberate choice.
                          For example, a very common mistake is to forget to check the result of File.delete(). In this case, the "side-effect" is the main purpose of the methods (deleting an actual file), but only the return value indicates whether the effect was, well, effected. As the compiler liberally lets you get away with ignoring the return value, and as failures of delete are relatively rare, problems end up appearing only monthes later, with no trace whatsoever of the ineffectiveness of the delete() call.

                          In this case, the compiler doesn't help pointing out the ignoring of the return value. Forcing you to handle it (triggering a compile-error) might have appeared dense, but no more than forcing to deal with checked exception (maye we're talking of an example where the return value would have been better designed as a pass vs exception); and just suggesting to handle it (issuing a warning) would sure have avoided lots of surprises.

                          If you were forced (or at least, strongly advised, many shops will not accept even warnings in the build system) to handle the return value, deliberately ignoring it would be no harder than how you ignore exceptions now (which, let's be clear, you should never ever do unless you know what you're doing, but just for the purpose of illustrating):
                          try {
                              doSomethingYouKnowIsSafe();
                          }
                          catch(SomeConservativeException canNeverOccur) {}
                          So, it'd go like:
                          int ignored = doSomethingIDonTCareTheReturnValueOf();
                          String forgetIt = doSomethingWhoseResultCanBeDitched();
                          SomeClass resultIsIrrelevant = doSomethingThatGeneratesAReferenceYouDonTNeedToKeep();
                          Object trashedAnyway = theMap.remove(key);
                          That way you document an informed decision with nothing heavier than a meaningful variable name (or OK, a comment, if you want to tell more).
                          • 40. Re: Ternary operator
                            jschellSomeoneStoleMyAlias
                            atch7 wrote:
                            I want to say that I'm not trolling. Just because I have objection to some constructs being preferred in java and slightly criticizing it for being illogical sometimes it doesn't mean that I'm trolling. Secondly, guys with full respect but how unsure you have to be if on the slightest criticism you react like little weak boys from prime school. I program mostly in C++ and I love this language but I also now that there are some bad things in C++. If someone would pointed out to me that, I definitely wouldn't get agitated or feel attacked by this person, especially if that person would explain why he/she think so. But you guys have some issues, probably because in the deepest of your heart, you know that java is just mediocre, quite weak language with many unsolved and unsolvable problems. I pity guys like you, who can't take bit of criticism.
                            So if you do a code review in C++ an you see the following statement you pass on it without comment?

                            i+1;

                            And you expect those that review your code to do the same?

                            Having previously programmed in C++ for years and C before that I can assure you that if I was reviewing your code and the above was in your code I would file a bug on it.

                            And I would file a bug if you used the ternary operator the way that you want to as well. In that case it is much better to use an if.

                            The fact that both C++ and java both allow the following syntax means absolutely nothing to me in terms of writing "logical" code because in both languages the author is wrong to attempt it and further doesn't know how to write maintainale code.

                            i = i++;
                            • 41. Re: Ternary operator
                              jschellSomeoneStoleMyAlias
                              atch7 wrote:
                              jduprez wrote:
                              Your example is another sort of Java's stronger checking, I see nothing awkward in it, but my mind is sure formatted after working with Java for a while (with a C >background mind you, and I'm happy I made the shift).
                              I'm sorry but if someone tells me that s/he is happy after switching from C/C++ to Java/C# etc. I just feel like they choosing easy, safe ride which is unfortunately also very boring and without any excitement (just like driving ford fiesta) on the other hand, yes, driving ferrari (read C/C++) you may injure yourself or even die, but the emotions and power is something that is worth dying for. Thats why I prefer to drive ferrari to ford fiesta.
                              I suspect that you would also attempt to go four wheeling in your ferrari while pulling a horse trailer.

                              Others might prefer to pick a vehicle based on the needs of the journey rather than blindly insisting on a single one.

                              Not to mention of course that there are probably many more people that can claim that they can drive a ferrari versus those that can actually do so in a way that actually uses the power appropriately.
                              • 42. Re: Ternary operator
                                jschellSomeoneStoleMyAlias
                                jduprez wrote:
                                although I'd be glader still if it forbid mere doSomething(); lines when this latter's return type is anything other than void
                                Why? There are plenty of times when I only want the side-effect of a method, and don't need the return value. Why should I have to assign it if I'm not going to use it?
                                Mmm.
                                First, one may question whether the callee is appropriately designed: there was a trend at some point that argued that methods should either read/compute something or change something, but not both (bear with my vague memory, I'm not sure I'll be able to find this article in the Internet history). Although as all rules, that should not be taken to the extreme and blindly obeyed (in particular "tell don't ask" would sometimes conflict with it), I find following it makes methods simpler, and helps avoiding that a method do "too much".

                                Second, if a method nevertheless both has a side effect and returns a result, disregarding that latter may be an oversight merely than a deliberate choice.
                                For example, a very common mistake is to forget to check the result of File.delete(). In this case, the "side-effect" is the main purpose of the methods (deleting an actual file), but only the return value indicates whether the effect was, well, effected. As the compiler liberally lets you get away with ignoring the return value, and as failures of delete are relatively rare, problems end up appearing only monthes later, with no trace whatsoever of the ineffectiveness of the delete() call.

                                In this case, the compiler doesn't help pointing out the ignoring of the return value. Forcing you to handle it (triggering a compile-error) might have appeared dense, but no more than forcing to deal with checked exception (maye we're talking of an example where the return value would have been better designed as a pass vs exception); and just suggesting to handle it (issuing a warning) would sure have avoided lots of surprises.
                                ++++;
                                int ignored = doSomethingIDonTCareTheReturnValueOf();
                                I prefer it with a comment, like the following.

                                // At this point it doesn't matter whether it failed or not because
                                // we are going to to exit regardless.
                                int ignored = doSomethingIDonTCareTheReturnValueOf();
                                • 43. Re: Ternary operator
                                  jschellSomeoneStoleMyAlias
                                  atch7 wrote:
                                  Well, maybe I would like to check how fast adding is executed without assigning?
                                  I suggest that you buy a profiler and learn how to profile applications, not operations.
                                  • 44. Re: Ternary operator
                                    796440
                                    JBStonehenge wrote:
                                    Guys...hallo? The discussion should have had ended with Darryl's answer.
                                    The discussion ends when everybody decides to stop posting.