Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. False selection...

False selection...

Scheduled Pinned Locked Moved The Weird and The Wonderful
javagame-devregexlearning
62 Posts 21 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L lordofawesome

    I'd probably go for this approach

    /**
    Determines if two display modes "match". Two display
    modes match if they have the same resolution, bit depth,
    and refresh rate. The bit depth is ignored if one of the
    modes has a bit depth of DisplayMode.BIT_DEPTH_MULTI.
    Likewise, the refresh rate is ignored if one of the
    modes has a refresh rate of
    DisplayMode.REFRESH_RATE_UNKNOWN.
    */
    public boolean displayModesMatch(DisplayMode mode1,
    DisplayMode mode2)
    {
    boolean isEqualDimention = (mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight());
    boolean isEqualDepth = (mode1.getBitDepth() == mode2.getBitDepth());
    boolean isEqualRefreshRate = (mode1.getRefreshRate() == mode2.getRefreshRate());

    return (isEqualDimention && isEqualDepth && isEqualRefreshRate);
    }
    

    I think this is much more readable and much simpler.

    M Offline
    M Offline
    Member 4517240
    wrote on last edited by
    #53

    lordofawesome wrote:

    boolean isEqualDimention = (mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight()); boolean isEqualDepth = (mode1.getBitDepth() == mode2.getBitDepth()); boolean isEqualRefreshRate = (mode1.getRefreshRate() == mode2.getRefreshRate()); return (isEqualDimention && isEqualDepth && isEqualRefreshRate);

    Actually (as a firmware developer), I found the original code quite reasonable. The function was exited as quickly as possible, with as few calculations as possible. The quoted code above performs ALL the calculations before exiting the function, doing a lot of (possibly) unnecessary work.

    L 1 Reply Last reply
    0
    • M Member 4517240

      lordofawesome wrote:

      boolean isEqualDimention = (mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight()); boolean isEqualDepth = (mode1.getBitDepth() == mode2.getBitDepth()); boolean isEqualRefreshRate = (mode1.getRefreshRate() == mode2.getRefreshRate()); return (isEqualDimention && isEqualDepth && isEqualRefreshRate);

      Actually (as a firmware developer), I found the original code quite reasonable. The function was exited as quickly as possible, with as few calculations as possible. The quoted code above performs ALL the calculations before exiting the function, doing a lot of (possibly) unnecessary work.

      L Offline
      L Offline
      lordofawesome
      wrote on last edited by
      #54

      Pls read other posts, i'm getting tired of getting the same remark over and over again :p

      1 Reply Last reply
      0
      • G ghle

        /**
        Determines if two display modes "match". Two display
        modes match if they have the same resolution, bit depth,
        and refresh rate. The bit depth is ignored if one of the
        modes has a bit depth of DisplayMode.BIT_DEPTH_MULTI.
        Likewise, the refresh rate is ignored if one of the
        modes has a refresh rate of
        DisplayMode.REFRESH_RATE_UNKNOWN.
        */
        public boolean displayModesMatch(DisplayMode mode1,
        DisplayMode mode2)
        {
        return (mode1.getWidth() == mode2.getWidth() && // Same resolution?
        mode1.getHeight() == mode2.getHeight()) &&
        (mode1.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI || // & Same Bit Depth?
        mode2.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI ||
        mode1.getBitDepth() == mode2.getBitDepth()) &&
        (mode1.getRefreshRate() == // & Same Refresh Rate?
        DisplayMode.REFRESH_RATE_UNKNOWN ||
        mode2.getRefreshRate() ==
        DisplayMode.REFRESH_RATE_UNKNOWN ||
        mode1.getRefreshRate() == mode2.getRefreshRate());
        }

        Better? I don't think so. Faster? Maybe.

        Gary

        _ Offline
        _ Offline
        _Erik_
        wrote on last edited by
        #55

        How about this one?

        return
        mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && // False if BIT_DEPTH_MULTI
        mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
        mode1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && // False if REFRESH_RATE_UNKNOWN
        mode2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
        mode1.getBitDepth() == mode2.getBitDepth() &&
        mode1.getRefreshRate() == mode2.getRefreshRate();

        I think this should be better and faster. See you

        G 1 Reply Last reply
        0
        • L lordofawesome

          This is a piece of code i took from a book i read about java game development.

          /**
          Determines if two display modes "match". Two display
          modes match if they have the same resolution, bit depth,
          and refresh rate. The bit depth is ignored if one of the
          modes has a bit depth of DisplayMode.BIT_DEPTH_MULTI.
          Likewise, the refresh rate is ignored if one of the
          modes has a refresh rate of
          DisplayMode.REFRESH_RATE_UNKNOWN.
          */
          public boolean displayModesMatch(DisplayMode mode1,
          DisplayMode mode2)
          {
          if (mode1.getWidth() != mode2.getWidth() ||
          mode1.getHeight() != mode2.getHeight())
          {
          return false;
          }

              if (mode1.getBitDepth() != DisplayMode.BIT\_DEPTH\_MULTI &&
                  mode2.getBitDepth() != DisplayMode.BIT\_DEPTH\_MULTI &&
                  mode1.getBitDepth() != mode2.getBitDepth())
              {
                  return false;
              }
          
              if (mode1.getRefreshRate() !=
                  DisplayMode.REFRESH\_RATE\_UNKNOWN &&
                  mode2.getRefreshRate() !=
                  DisplayMode.REFRESH\_RATE\_UNKNOWN &&
                  mode1.getRefreshRate() != mode2.getRefreshRate())
               {
                   return false;
               }
          
               return true;
          }
          
          S Offline
          S Offline
          Simon Dufour
          wrote on last edited by
          #56

          I agree with you. There are cleaner ways to do this. However, none will make your book have more pages!

          public boolean displayModesMatch(DisplayMode mode1, DisplayMode mode2)
          {
          return
          mode1.getWidth() == mode2.getWidth() &&
          mode1.getHeight() == mode2.getHeight() &&

          mode1.getBitDepth()    == mode2.getBitDepth() &&
          mode1.getBitDepth()    != DisplayMode.BIT\_DEPTH\_MULTI &&
                  
          mode1.getRefreshRate() == mode2.getRefreshRate() &&
          mode1.getRefreshRate() != DisplayMode.REFRESH\_RATE\_UNKNOWN;
          

          }

          1 Reply Last reply
          0
          • A AspDotNetDev

            I was always told to only have one return statement in a procedure, so I can see this being a horror.

            [Forum Guidelines]

            F Offline
            F Offline
            FSANB
            wrote on last edited by
            #57

            aspdotnetdev wrote:

            I was always told to only have one return statement in a procedure, so I can see this being a horror.

            I spent time last century programming in Pascal (amongst many, many other languages) and one of the "rules" which I stubbornly held onto for many years afterwards was to only have a single return from a method... BUT I finally woke up to myself and (Thank You Martin Fowler!) realised that exiting early (using what I believe are called "guard clauses" by people who know) can really improve readability and performance. Although I do wish that (currently) Visual Studio would highlight every "return" because I still sometimes only notice the last one in a method on first read. In my OPINION (and without knowing much about the actual subject domain) I prefer the first posted "horror" example to anything I've seen posted since. That is just subjective opinion, but it is based on coding for a long time and working with many other developers with different levels of experience. In fact it's probably mostly based on being embarrassed when returning to my own code years later and not having a clue what I was trying to do... Thanks to LordOfAwesome for starting this thread and prompting me to post here for the first time (I think... My memory really is getting worse with age). I just also wanted to reply to one of your later comments:

            lordofawesome wrote:

            Nevertheless I think it is strange to return a boolean from the result of any if structure. It just seems strange to me.

            I agree with you that if there was a single "if" statement which returned a boolean, that would be silly, but in this case each of the "if" blocks might either return false AND exit the method, or fall through to the next statement. And it is that "falling through" that is very difficult to show clearly using in-line boolean operations, even with (or perhaps especially because of) the use of short-circuit boolean operators (like &&, etc.) So in the case we're discussing, my preference would be to write more code to be absolutely clear about what I was trying to do (and let's remember that the example was from a text book, so clarity is especially important), exit early to only execute the minimum required and if the method did end up being a performance bottleneck, go back and optimise it later, which would be much easier to do based on clear existing logic.

            G 1 Reply Last reply
            0
            • _ _Erik_

              How about this one?

              return
              mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && // False if BIT_DEPTH_MULTI
              mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
              mode1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && // False if REFRESH_RATE_UNKNOWN
              mode2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
              mode1.getBitDepth() == mode2.getBitDepth() &&
              mode1.getRefreshRate() == mode2.getRefreshRate();

              I think this should be better and faster. See you

              G Offline
              G Offline
              ghle
              wrote on last edited by
              #58

              _Erik_ wrote:

              How about this one?

              return
              mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && // False if BIT_DEPTH_MULTI
              mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
              mode1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && // False if REFRESH_RATE_UNKNOWN
              mode2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
              mode1.getBitDepth() == mode2.getBitDepth() &&
              mode1.getRefreshRate() == mode2.getRefreshRate();

              I think this should be better and faster.

              Faster. Absolutely NOT BETTER. Only faster because you skip half the elements to test X| . You need to check all the conditions in the original code, including height and width :-O . Also your test is just plain wrong. The original...

                  if (mode1.getBitDepth() != DisplayMode.BIT\_DEPTH\_MULTI &&
                      mode2.getBitDepth() != DisplayMode.BIT\_DEPTH\_MULTI &&
                      mode1.getBitDepth() != mode2.getBitDepth())
                  {
                      return false;
                  }
              

              Assume the case of mode1.getBitDepth() equals DisplayMode.BIT_DEPTH_MULTI. The original code *might* return TRUE in this condition, depending on subsequent testing of RefreshRate. Your code always returns FALSE under this condition. :rose:

              Gary

              _ 1 Reply Last reply
              0
              • G ghle

                _Erik_ wrote:

                How about this one?

                return
                mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && // False if BIT_DEPTH_MULTI
                mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI &&
                mode1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && // False if REFRESH_RATE_UNKNOWN
                mode2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN &&
                mode1.getBitDepth() == mode2.getBitDepth() &&
                mode1.getRefreshRate() == mode2.getRefreshRate();

                I think this should be better and faster.

                Faster. Absolutely NOT BETTER. Only faster because you skip half the elements to test X| . You need to check all the conditions in the original code, including height and width :-O . Also your test is just plain wrong. The original...

                    if (mode1.getBitDepth() != DisplayMode.BIT\_DEPTH\_MULTI &&
                        mode2.getBitDepth() != DisplayMode.BIT\_DEPTH\_MULTI &&
                        mode1.getBitDepth() != mode2.getBitDepth())
                    {
                        return false;
                    }
                

                Assume the case of mode1.getBitDepth() equals DisplayMode.BIT_DEPTH_MULTI. The original code *might* return TRUE in this condition, depending on subsequent testing of RefreshRate. Your code always returns FALSE under this condition. :rose:

                Gary

                _ Offline
                _ Offline
                _Erik_
                wrote on last edited by
                #59

                Yes, you're right. I think I misunderstood the initial post. I thought it should return false if one of the modes was BIT_DEPTH_MULT or REFRESH_RATE_UNKNOWN. Instead, what I understand now is that bit depth matches if both are equal or one of them is BIT_DEPTH_MULTI, and refresh rate matches if both are equal or one of them is REFRESH_RATE_UNKNOWN. Am I right? I am sure you agree that looking at the original code does not help too much to figure out what it has to do, and that is exactly what we all are talking about here:

                return mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight() &&
                (mode1.getBitDepth() == mode2.getBitDepth() ||
                mode1.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI ||
                mode2.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI)
                &&
                (mode1.getRefreshRate() == mode2.getRefreshRate() ||
                mode1.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN ||
                mode2.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN);

                I think this would be much more readable, even without any comment explaining what it has to do. Edit That said, I think it would be still better to split this into three methods (sizeMatch, bitDepthMatch, refreshRateMatch), so displayModesMatch would still become much easier to read.

                return sizeMatch(mode1, mode2) && bitDepthMatch(mode1, mode2) && refreshRateMatch(mode1, mode2);

                You see, there are many ways to get the same result, and I think the given example here in the first post is a coding horror becouse it is one of the worst approaches. See you.

                modified on Monday, October 18, 2010 9:41 AM

                G 1 Reply Last reply
                0
                • _ _Erik_

                  Yes, you're right. I think I misunderstood the initial post. I thought it should return false if one of the modes was BIT_DEPTH_MULT or REFRESH_RATE_UNKNOWN. Instead, what I understand now is that bit depth matches if both are equal or one of them is BIT_DEPTH_MULTI, and refresh rate matches if both are equal or one of them is REFRESH_RATE_UNKNOWN. Am I right? I am sure you agree that looking at the original code does not help too much to figure out what it has to do, and that is exactly what we all are talking about here:

                  return mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight() &&
                  (mode1.getBitDepth() == mode2.getBitDepth() ||
                  mode1.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI ||
                  mode2.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI)
                  &&
                  (mode1.getRefreshRate() == mode2.getRefreshRate() ||
                  mode1.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN ||
                  mode2.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN);

                  I think this would be much more readable, even without any comment explaining what it has to do. Edit That said, I think it would be still better to split this into three methods (sizeMatch, bitDepthMatch, refreshRateMatch), so displayModesMatch would still become much easier to read.

                  return sizeMatch(mode1, mode2) && bitDepthMatch(mode1, mode2) && refreshRateMatch(mode1, mode2);

                  You see, there are many ways to get the same result, and I think the given example here in the first post is a coding horror becouse it is one of the worst approaches. See you.

                  modified on Monday, October 18, 2010 9:41 AM

                  G Offline
                  G Offline
                  ghle
                  wrote on last edited by
                  #60

                  Erik. A 2 vote? REALLY? Thanks buddy. I see you don't handle criticism very well. The original code was very straight forward. Some developers don't like multiple returns. That does not make it a "horror". Likely you did not read the comments in the code or analyze the code properly. I made this mistake upon first inspection, assuming I knew what it was doing, but did not. Now, your latest example: Perform a routine, which performs 3 routines - that has to be the slowest rewrite that was presented in this thread. Loading and clearing the register stack 4 times. Wow! Too much overhead. Yes, clear, but doesn't belong in time-sensitive logic (if this is time-sensitive). But I have to wonder how the bitdepth and refreshrate code would be written. You haven't gotten rid of the nuance, just move it and added some overhead. You'd have better comments? In short, your final logic does the same as the original but is slower.

                  Gary

                  _ 1 Reply Last reply
                  0
                  • F FSANB

                    aspdotnetdev wrote:

                    I was always told to only have one return statement in a procedure, so I can see this being a horror.

                    I spent time last century programming in Pascal (amongst many, many other languages) and one of the "rules" which I stubbornly held onto for many years afterwards was to only have a single return from a method... BUT I finally woke up to myself and (Thank You Martin Fowler!) realised that exiting early (using what I believe are called "guard clauses" by people who know) can really improve readability and performance. Although I do wish that (currently) Visual Studio would highlight every "return" because I still sometimes only notice the last one in a method on first read. In my OPINION (and without knowing much about the actual subject domain) I prefer the first posted "horror" example to anything I've seen posted since. That is just subjective opinion, but it is based on coding for a long time and working with many other developers with different levels of experience. In fact it's probably mostly based on being embarrassed when returning to my own code years later and not having a clue what I was trying to do... Thanks to LordOfAwesome for starting this thread and prompting me to post here for the first time (I think... My memory really is getting worse with age). I just also wanted to reply to one of your later comments:

                    lordofawesome wrote:

                    Nevertheless I think it is strange to return a boolean from the result of any if structure. It just seems strange to me.

                    I agree with you that if there was a single "if" statement which returned a boolean, that would be silly, but in this case each of the "if" blocks might either return false AND exit the method, or fall through to the next statement. And it is that "falling through" that is very difficult to show clearly using in-line boolean operations, even with (or perhaps especially because of) the use of short-circuit boolean operators (like &&, etc.) So in the case we're discussing, my preference would be to write more code to be absolutely clear about what I was trying to do (and let's remember that the example was from a text book, so clarity is especially important), exit early to only execute the minimum required and if the method did end up being a performance bottleneck, go back and optimise it later, which would be much easier to do based on clear existing logic.

                    G Offline
                    G Offline
                    ghle
                    wrote on last edited by
                    #61

                    FSANB wrote:

                    I agree with you that if there was a single "if" statement which returned a boolean, that would be silly, but in this case each of the "if" blocks might either return false AND exit the method, or fall through to the next statement. And it is that "falling through" that is very difficult to show clearly using in-line boolean operations, even with (or perhaps especially because of) the use of short-circuit boolean operators (like &&, etc.)

                    Um, use ELSE to replace the fall-through. Would that make it more acceptable? I think nesting if's with the else's would be more unreadable, but that is me. Funny, we could all use the same coding "standards" but write this little routine so differently. That's what makes us arteests. :cool:

                    Gary

                    1 Reply Last reply
                    0
                    • G ghle

                      Erik. A 2 vote? REALLY? Thanks buddy. I see you don't handle criticism very well. The original code was very straight forward. Some developers don't like multiple returns. That does not make it a "horror". Likely you did not read the comments in the code or analyze the code properly. I made this mistake upon first inspection, assuming I knew what it was doing, but did not. Now, your latest example: Perform a routine, which performs 3 routines - that has to be the slowest rewrite that was presented in this thread. Loading and clearing the register stack 4 times. Wow! Too much overhead. Yes, clear, but doesn't belong in time-sensitive logic (if this is time-sensitive). But I have to wonder how the bitdepth and refreshrate code would be written. You haven't gotten rid of the nuance, just move it and added some overhead. You'd have better comments? In short, your final logic does the same as the original but is slower.

                      Gary

                      _ Offline
                      _ Offline
                      _Erik_
                      wrote on last edited by
                      #62

                      The 2 vote was another mistake of mine, sorry for that. I wanted to go to page 2 to see the original post, clicked the vote 2 instead, and forgot to make the correct vote to your post. I've just corrected it with the vote it really deserves, a 5. I know my final logic does the same as the original but is slower. It is obvious. And it is much clearer, and this is also obvious. The sample I give just before does the same also, and is faster. Both of them improve the original in readability, and the first one also improves it in speed. If you had to manage a team of developers, some of them newcomers with very little experience, tell me, having the original code and the two samples I am giving, which one would you choose? I would choose the last one, though I know it is slower, but I also know that the overhead is just negligible in this case, becouse this operation will not be used over and over again within de context of a long and complex batch process. I don't mind to have several "return" in the same method... if it improves readability and/or performance. In the case presented here, it does not improve readablity neither performance, so that is the reason why I consider it a horror. Nobody is obligated to agree, I am just sharing my point of view. Edit Sorry again, I was forgetting the las part. The implementation for bitdepth and refreshrate is given just before, but like you wonder how they should be, I will give some extra clues:

                      return mode1.getWidth() == mode2.getWidth() && mode1.getHeight() == mode2.getHeight() && // This checks sizes
                      (mode1.getBitDepth() == mode2.getBitDepth() || // These three lines
                      mode1.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI || // are checking if
                      mode2.getBitDepth() == DisplayMode.BIT_DEPTH_MULTI) // bit depths match
                      &&
                      (mode1.getRefreshRate() == mode2.getRefreshRate() || // These three lines
                      mode1.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN || // are checking if
                      mode2.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN); // refresh rates match

                      Thank you

                      modified on Tuesday, October 19, 2010 9:28 AM

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups