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 Offline
    L Offline
    lordofawesome
    wrote on last edited by
    #1

    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;
    }
    
    L P D L A 6 Replies 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;
      }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I remember seeing very much the same thing in some DirectX sample. It certainly is not the greatest code ever written, but where do you see the horror?

      A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

      L 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;
        }
        
        P Offline
        P Offline
        Phil J Pearson
        wrote on last edited by
        #3

        So what's wrong with that? I think it's the tidiest piece of code I have ever seen in Coding Horrors.

        Phil


        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

        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;
          }
          
          D Offline
          D Offline
          David Skelly
          wrote on last edited by
          #4

          I must be missing something obvious. What's your point?

          1 Reply Last reply
          0
          • L Lost User

            I remember seeing very much the same thing in some DirectX sample. It certainly is not the greatest code ever written, but where do you see the horror?

            A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

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

            The code will work of course, but it should be obvious that the if structures are redundant... simply return the combined value of all the booleans

            L 1 Reply Last reply
            0
            • L lordofawesome

              The code will work of course, but it should be obvious that the if structures are redundant... simply return the combined value of all the booleans

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Really? It would become quite an ugly expression and it would be much harder to read. I remember writing similar code when playing with DirectX, but I wanted to filter display modes out of a list and any parameter could also have a 'don't care' value. This would have gotten even messier, so I also went for the more readable variant.

              A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

              L 1 Reply Last reply
              0
              • L Lost User

                Really? It would become quite an ugly expression and it would be much harder to read. I remember writing similar code when playing with DirectX, but I wanted to filter display modes out of a list and any parameter could also have a 'don't care' value. This would have gotten even messier, so I also went for the more readable variant.

                A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

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

                uhum that makes sence, but i'd probably try to simplify the expression using some describing variable names.

                L 1 Reply Last reply
                0
                • L lordofawesome

                  uhum that makes sence, but i'd probably try to simplify the expression using some describing variable names.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  When putting together a graphics engine, you first have to set up a rendering context of some kind. To do that you will have to sort out what kind of graphics capabilities the host computer has and which not. In short: You will be writing lots of such code and there will be a few errors. Then it's really nice if you don't have to pull apart more complex logical terms and just can make your correction at the right place. It's just more easy to read and to maintain. And also the day will come when you have to look at the code and don't quite remember why you once decided to do things just the way they are, much less a stranger who works with your code for the first time.

                  A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    When putting together a graphics engine, you first have to set up a rendering context of some kind. To do that you will have to sort out what kind of graphics capabilities the host computer has and which not. In short: You will be writing lots of such code and there will be a few errors. Then it's really nice if you don't have to pull apart more complex logical terms and just can make your correction at the right place. It's just more easy to read and to maintain. And also the day will come when you have to look at the code and don't quite remember why you once decided to do things just the way they are, much less a stranger who works with your code for the first time.

                    A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

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

                    I believe i understand what you are saying, but i don't think the above solution is a good solution to that problem.

                    L 1 Reply Last reply
                    0
                    • L lordofawesome

                      I believe i understand what you are saying, but i don't think the above solution is a good solution to that problem.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      It sure does its job, wich itself is not really complicated. So why don't you just post your more fitting version?

                      A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                      L S 2 Replies Last reply
                      0
                      • L Lost User

                        It sure does its job, wich itself is not really complicated. So why don't you just post your more fitting version?

                        A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

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

                        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.

                        L S D E J 10 Replies Last reply
                        0
                        • 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.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Nice and well, but what happened to the BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN business? Including those two will take away some of the neatness again. And what if we don't just want to check three things? What if there are 20? Then you will have 20 variables which are used in one fat term at the end.

                          A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                          L 1 Reply Last reply
                          0
                          • 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.

                            S Offline
                            S Offline
                            Stryder_1
                            wrote on last edited by
                            #13

                            Hi, One reason for the original approach is it reduces processing time, which is of primary importance for a game engine. Your proposal will require the function to process each variable before returning while the original will check the most likely areas of failure first, then return -- eliminating the need to process the further checks.

                            L L E 3 Replies Last reply
                            0
                            • L Lost User

                              Nice and well, but what happened to the BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN business? Including those two will take away some of the neatness again. And what if we don't just want to check three things? What if there are 20? Then you will have 20 variables which are used in one fat term at the end.

                              A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

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

                              I believe BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN don't need to be there. they are constants. even if they should be there, it still would be a lot cleaner. I'm not saying this IS the best approach i'm just saying how i think is the cleanest solution. Even with 50 things to check it's much more readable using variables then doing some if structures :p

                              L J 2 Replies Last reply
                              0
                              • S Stryder_1

                                Hi, One reason for the original approach is it reduces processing time, which is of primary importance for a game engine. Your proposal will require the function to process each variable before returning while the original will check the most likely areas of failure first, then return -- eliminating the need to process the further checks.

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

                                Assuming it really is faster, this method isn't really part of the engine itself. it's not like this function will be run over and over again. Me personally, i wouldn't compromise readability over such a small performance increasement. But i'm severely starting to doubt myself, a lot of people seem to disagree this is a codehorror :s

                                S E 2 Replies Last reply
                                0
                                • S Stryder_1

                                  Hi, One reason for the original approach is it reduces processing time, which is of primary importance for a game engine. Your proposal will require the function to process each variable before returning while the original will check the most likely areas of failure first, then return -- eliminating the need to process the further checks.

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  Runtime behavior would be worth a look for any code executed inside the rendering loop. Display modes usually are checked beforehand, so yes, technically this would be faster, but the effect would be unnoticable.

                                  A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                                  1 Reply Last reply
                                  0
                                  • 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.

                                    D Offline
                                    D Offline
                                    David Skelly
                                    wrote on last edited by
                                    #17

                                    What you have posted here is exactly what DisplayMode's equals method does, so you could replace the whole lot with:

                                    return mode1.equals(mode2);

                                    You can't get much simpler than that. But that is not what the original code does. The original code takes BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN into account, which your code and the equals method does not.

                                    L 1 Reply Last reply
                                    0
                                    • L lordofawesome

                                      I believe BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN don't need to be there. they are constants. even if they should be there, it still would be a lot cleaner. I'm not saying this IS the best approach i'm just saying how i think is the cleanest solution. Even with 50 things to check it's much more readable using variables then doing some if structures :p

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #18

                                      Both may be constants, but they do play an important role here. Your version would wrongly return false in some cases when those two constants appear. Besides that, your approach works and is neat enough. Unfortunately things are not always as simple as they are here. This can very quickly turn into a bowl of spaghetti and some compromises may have to be made. Unfortunately. Anyway, the original version still is not so bad that it's a horror.

                                      A while ago he asked me what he should have printed on my business cards. I said 'Wizard'. I read books which nobody else understand. Then I do something which nobody understands. After that the computer does something which nobody understands. When asked, I say things about the results which nobody understand. But everybody expects miracles from me on a regular basis. Looks to me like the classical definition of a wizard.

                                      1 Reply Last reply
                                      0
                                      • L lordofawesome

                                        I believe BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN don't need to be there. they are constants. even if they should be there, it still would be a lot cleaner. I'm not saying this IS the best approach i'm just saying how i think is the cleanest solution. Even with 50 things to check it's much more readable using variables then doing some if structures :p

                                        J Offline
                                        J Offline
                                        josda1000
                                        wrote on last edited by
                                        #19

                                        lordofawesome wrote:

                                        I believe BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN don't need to be there.

                                        According to the original function comments, that is incorrect. You can't just change the premise of the code on a whim. You can make it cleaner in any way you feel you want (even though it worked), but you can't change the unit requirements after it has already been in use like that.

                                        lordofawesome wrote:

                                        even if they should be there, it still would be a lot cleaner.

                                        Fine, but you must include those constants if they were already there. incorporate them into your code.

                                        lordofawesome wrote:

                                        Even with 50 things to check it's much more readable using variables then doing some if structures

                                        Some would agree with you, some would disagree. Let's put it this way: if someone wrote code that works, leave it alone. Why reinvent the wheel?

                                        Josh Davis
                                        This is what plays in my head when I finish projects.

                                        1 Reply Last reply
                                        0
                                        • D David Skelly

                                          What you have posted here is exactly what DisplayMode's equals method does, so you could replace the whole lot with:

                                          return mode1.equals(mode2);

                                          You can't get much simpler than that. But that is not what the original code does. The original code takes BIT_DEPTH_MULTI and REFRESH_RATE_UNKNOWN into account, which your code and the equals method does not.

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

                                          But aren't these simply constants? So the value would still have to be the same right? if so, this would mean that you don't need to take them into account. Or am i missing the point :s

                                          D E 2 Replies 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