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. Logics is really hard to learn

Logics is really hard to learn

Scheduled Pinned Locked Moved The Weird and The Wonderful
tutorialquestion
16 Posts 10 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.
  • V Offline
    V Offline
    Vladimir Svyatski
    wrote on last edited by
    #1

    I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

            if (tabIndex == 0)
            {
                panelGeneral.Visible = true;
                panelTolerances.Visible = false;
                btnProjSetNext.Enabled = true;
                btnProjSetPrev.Enabled = false;
            }
            else
            {
                panelGeneral.Visible = false;
                panelTolerances.Visible = true;
                btnProjSetNext.Enabled = false;
                btnProjSetPrev.Enabled = true;
            }
    

    Why don't write something like

            bool tabIndexZero = tabIndex == 0;
            
            panelGeneral.Visible = tabIndexZero;
            panelTolerances.Visible = !tabIndexZero;
            btnProjSetNext.Enabled = tabIndexZero;
            btnProjSetPrev.Enabled = !tabIndexZero;
    

    This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

    M Sander RosselS B C N 7 Replies Last reply
    0
    • V Vladimir Svyatski

      I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

              if (tabIndex == 0)
              {
                  panelGeneral.Visible = true;
                  panelTolerances.Visible = false;
                  btnProjSetNext.Enabled = true;
                  btnProjSetPrev.Enabled = false;
              }
              else
              {
                  panelGeneral.Visible = false;
                  panelTolerances.Visible = true;
                  btnProjSetNext.Enabled = false;
                  btnProjSetPrev.Enabled = true;
              }
      

      Why don't write something like

              bool tabIndexZero = tabIndex == 0;
              
              panelGeneral.Visible = tabIndexZero;
              panelTolerances.Visible = !tabIndexZero;
              btnProjSetNext.Enabled = tabIndexZero;
              btnProjSetPrev.Enabled = !tabIndexZero;
      

      This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

      M Offline
      M Offline
      musefan
      wrote on last edited by
      #2

      or why not...

      bool tabIndexZero = tabIndex == 0;
      panelGeneral.Visible = btnProjSetNext.Enabled = tabIndexZero;
      panelTolerances.Visible = btnProjSetPrev.Enabled = !tabIndexZero;

      or

      panelTolerances.Visible = btnProjSetPrev.Enabled = !(panelGeneral.Visible = btnProjSetNext.Enabled = tabIndex == 0);

      ...anyway, the code you claim to be so bad is certainly much more readable, and also convenient if you need to change the logic (i.e. tabIndex = 0 should now set all values to true) I am not saying it is good practice however

      I may or may not be responsible for my own actions

      modified on Wednesday, April 6, 2011 12:18 PM

      L 1 Reply Last reply
      0
      • V Vladimir Svyatski

        I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

                if (tabIndex == 0)
                {
                    panelGeneral.Visible = true;
                    panelTolerances.Visible = false;
                    btnProjSetNext.Enabled = true;
                    btnProjSetPrev.Enabled = false;
                }
                else
                {
                    panelGeneral.Visible = false;
                    panelTolerances.Visible = true;
                    btnProjSetNext.Enabled = false;
                    btnProjSetPrev.Enabled = true;
                }
        

        Why don't write something like

                bool tabIndexZero = tabIndex == 0;
                
                panelGeneral.Visible = tabIndexZero;
                panelTolerances.Visible = !tabIndexZero;
                btnProjSetNext.Enabled = tabIndexZero;
                btnProjSetPrev.Enabled = !tabIndexZero;
        

        This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

        Sander RosselS Offline
        Sander RosselS Offline
        Sander Rossel
        wrote on last edited by
        #3

        VUnreal wrote:

        why can't people learn elementary logic rules?

        Maybe they are pengiuns![^] :-O

        It's an OO world.

        1 Reply Last reply
        0
        • M musefan

          or why not...

          bool tabIndexZero = tabIndex == 0;
          panelGeneral.Visible = btnProjSetNext.Enabled = tabIndexZero;
          panelTolerances.Visible = btnProjSetPrev.Enabled = !tabIndexZero;

          or

          panelTolerances.Visible = btnProjSetPrev.Enabled = !(panelGeneral.Visible = btnProjSetNext.Enabled = tabIndex == 0);

          ...anyway, the code you claim to be so bad is certainly much more readable, and also convenient if you need to change the logic (i.e. tabIndex = 0 should now set all values to true) I am not saying it is good practice however

          I may or may not be responsible for my own actions

          modified on Wednesday, April 6, 2011 12:18 PM

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

          musefan wrote:

          or why not...

          Because that repeats the evaluation and a single answer is easier to update (invert as per your example) than a multitude of lines. It would also reduce the canche of introducing a bug because of a typo in the expression that's going to be evaluated. Nitpicking again..

          I are Troll :suss:

          M 1 Reply Last reply
          0
          • L Lost User

            musefan wrote:

            or why not...

            Because that repeats the evaluation and a single answer is easier to update (invert as per your example) than a multitude of lines. It would also reduce the canche of introducing a bug because of a typo in the expression that's going to be evaluated. Nitpicking again..

            I are Troll :suss:

            M Offline
            M Offline
            musefan
            wrote on last edited by
            #5

            Eddy Vluggen wrote:

            Because that repeats the evaluation

            good point :doh:

            I may or may not be responsible for my own actions

            L 1 Reply Last reply
            0
            • V Vladimir Svyatski

              I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

                      if (tabIndex == 0)
                      {
                          panelGeneral.Visible = true;
                          panelTolerances.Visible = false;
                          btnProjSetNext.Enabled = true;
                          btnProjSetPrev.Enabled = false;
                      }
                      else
                      {
                          panelGeneral.Visible = false;
                          panelTolerances.Visible = true;
                          btnProjSetNext.Enabled = false;
                          btnProjSetPrev.Enabled = true;
                      }
              

              Why don't write something like

                      bool tabIndexZero = tabIndex == 0;
                      
                      panelGeneral.Visible = tabIndexZero;
                      panelTolerances.Visible = !tabIndexZero;
                      btnProjSetNext.Enabled = tabIndexZero;
                      btnProjSetPrev.Enabled = !tabIndexZero;
              

              This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

              B Offline
              B Offline
              BillW33
              wrote on last edited by
              #6

              But, people like to use if statements! I am happy if they at least use an else instead of another if. :sigh:

              Just because the code works, it doesn't mean that it is good code.

              1 Reply Last reply
              0
              • V Vladimir Svyatski

                I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

                        if (tabIndex == 0)
                        {
                            panelGeneral.Visible = true;
                            panelTolerances.Visible = false;
                            btnProjSetNext.Enabled = true;
                            btnProjSetPrev.Enabled = false;
                        }
                        else
                        {
                            panelGeneral.Visible = false;
                            panelTolerances.Visible = true;
                            btnProjSetNext.Enabled = false;
                            btnProjSetPrev.Enabled = true;
                        }
                

                Why don't write something like

                        bool tabIndexZero = tabIndex == 0;
                        
                        panelGeneral.Visible = tabIndexZero;
                        panelTolerances.Visible = !tabIndexZero;
                        btnProjSetNext.Enabled = tabIndexZero;
                        btnProjSetPrev.Enabled = !tabIndexZero;
                

                This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

                C Offline
                C Offline
                Chris Meech
                wrote on last edited by
                #7

                I think that both pieces of code are acceptable. While the original is much more verbose, that will lend itself to being understood by someone else. It additionally allows for easier maintenance should UI changes be needed. :)

                Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra] posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]

                1 Reply Last reply
                0
                • M musefan

                  Eddy Vluggen wrote:

                  Because that repeats the evaluation

                  good point :doh:

                  I may or may not be responsible for my own actions

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

                  musefan wrote:

                  good point

                  Meh, too much VB, didn't notice the single equation-character :(

                  I are Troll :suss:

                  L 1 Reply Last reply
                  0
                  • L Lost User

                    musefan wrote:

                    good point

                    Meh, too much VB, didn't notice the single equation-character :(

                    I are Troll :suss:

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

                    That's a brilliantly simple way to do things. In short, there are two ways to solve a problem: Just do it :) Just do it, but do it in a better way!

                    - Bits and Bytes Rules! 10(jk)

                    L 1 Reply Last reply
                    0
                    • V Vladimir Svyatski

                      I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

                              if (tabIndex == 0)
                              {
                                  panelGeneral.Visible = true;
                                  panelTolerances.Visible = false;
                                  btnProjSetNext.Enabled = true;
                                  btnProjSetPrev.Enabled = false;
                              }
                              else
                              {
                                  panelGeneral.Visible = false;
                                  panelTolerances.Visible = true;
                                  btnProjSetNext.Enabled = false;
                                  btnProjSetPrev.Enabled = true;
                              }
                      

                      Why don't write something like

                              bool tabIndexZero = tabIndex == 0;
                              
                              panelGeneral.Visible = tabIndexZero;
                              panelTolerances.Visible = !tabIndexZero;
                              btnProjSetNext.Enabled = tabIndexZero;
                              btnProjSetPrev.Enabled = !tabIndexZero;
                      

                      This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

                      N Offline
                      N Offline
                      Nathan D Cook
                      wrote on last edited by
                      #10

                      "Logics is really hard to learn?" Don't you mean "Logics are really hard to learn?" LOL, I'm just kidding. Anyways, personally, I actually prefer the first one just because it's easier. My job involves 40 hrs a week of trying to decipher other people's written code and fix bugs. I love it, but I'll gladly take the simple-written code over the stuff I have to think over for a minute or so.

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        That's a brilliantly simple way to do things. In short, there are two ways to solve a problem: Just do it :) Just do it, but do it in a better way!

                        - Bits and Bytes Rules! 10(jk)

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

                        good point

                        1 Reply Last reply
                        0
                        • N Nathan D Cook

                          "Logics is really hard to learn?" Don't you mean "Logics are really hard to learn?" LOL, I'm just kidding. Anyways, personally, I actually prefer the first one just because it's easier. My job involves 40 hrs a week of trying to decipher other people's written code and fix bugs. I love it, but I'll gladly take the simple-written code over the stuff I have to think over for a minute or so.

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

                          Even i like to go with first method, which makes the code more readable.

                          1 Reply Last reply
                          0
                          • V Vladimir Svyatski

                            I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

                                    if (tabIndex == 0)
                                    {
                                        panelGeneral.Visible = true;
                                        panelTolerances.Visible = false;
                                        btnProjSetNext.Enabled = true;
                                        btnProjSetPrev.Enabled = false;
                                    }
                                    else
                                    {
                                        panelGeneral.Visible = false;
                                        panelTolerances.Visible = true;
                                        btnProjSetNext.Enabled = false;
                                        btnProjSetPrev.Enabled = true;
                                    }
                            

                            Why don't write something like

                                    bool tabIndexZero = tabIndex == 0;
                                    
                                    panelGeneral.Visible = tabIndexZero;
                                    panelTolerances.Visible = !tabIndexZero;
                                    btnProjSetNext.Enabled = tabIndexZero;
                                    btnProjSetPrev.Enabled = !tabIndexZero;
                            

                            This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

                            L Offline
                            L Offline
                            Luc Pattyn
                            wrote on last edited by
                            #13

                            tabIndexZero is a poor choice for the identifier here, it should convey functionality; and statement reordering and better horizontal alignment could be used to improve readability, so maybe:

                                    bool summaryMode = tabIndex == 0;
                                    
                                    panelGeneral.Visible =     summaryMode;
                                    btnProjSetNext.Enabled =   summaryMode;
                            
                                    panelTolerances.Visible = !summaryMode;
                                    btnProjSetPrev.Enabled =  !summaryMode;
                            

                            :)

                            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                            R 1 Reply Last reply
                            0
                            • V Vladimir Svyatski

                              I've been wondering for a long time and I still do: why can't people learn elementary logic rules? Are they really that hard? For example, I constantly meet code snippets like this:

                                      if (tabIndex == 0)
                                      {
                                          panelGeneral.Visible = true;
                                          panelTolerances.Visible = false;
                                          btnProjSetNext.Enabled = true;
                                          btnProjSetPrev.Enabled = false;
                                      }
                                      else
                                      {
                                          panelGeneral.Visible = false;
                                          panelTolerances.Visible = true;
                                          btnProjSetNext.Enabled = false;
                                          btnProjSetPrev.Enabled = true;
                                      }
                              

                              Why don't write something like

                                      bool tabIndexZero = tabIndex == 0;
                                      
                                      panelGeneral.Visible = tabIndexZero;
                                      panelTolerances.Visible = !tabIndexZero;
                                      btnProjSetNext.Enabled = tabIndexZero;
                                      btnProjSetPrev.Enabled = !tabIndexZero;
                              

                              This is twice as shorter! Benefit from knowledge of basic Boolean rules, which are, I believe, "must know" for every developer.

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #14

                              VUnreal wrote:

                              This is twice as shorter!

                              What the heck does that mean? Twice as shorter than what? :confused:

                              1 Reply Last reply
                              0
                              • L Luc Pattyn

                                tabIndexZero is a poor choice for the identifier here, it should convey functionality; and statement reordering and better horizontal alignment could be used to improve readability, so maybe:

                                        bool summaryMode = tabIndex == 0;
                                        
                                        panelGeneral.Visible =     summaryMode;
                                        btnProjSetNext.Enabled =   summaryMode;
                                
                                        panelTolerances.Visible = !summaryMode;
                                        btnProjSetPrev.Enabled =  !summaryMode;
                                

                                :)

                                Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                R Offline
                                R Offline
                                Richard A Dalton
                                wrote on last edited by
                                #15

                                You beat me to it, I was about to say both examples are bad. I don't give a rats behind about how long the code it, but if it't doesn't tell me what it's doing then it's bad. Also, the second example makes explicit the oppositeness of PanelGeneral.Visible and PanelTolerances.visible btnProjSetNext.Enabled and btnProjSetPrev.Enabled That oppositeness might be coincidental rather than fundamental. If so the first code is actually better than the second. -Richard

                                Hit any user to continue.

                                L 1 Reply Last reply
                                0
                                • R Richard A Dalton

                                  You beat me to it, I was about to say both examples are bad. I don't give a rats behind about how long the code it, but if it't doesn't tell me what it's doing then it's bad. Also, the second example makes explicit the oppositeness of PanelGeneral.Visible and PanelTolerances.visible btnProjSetNext.Enabled and btnProjSetPrev.Enabled That oppositeness might be coincidental rather than fundamental. If so the first code is actually better than the second. -Richard

                                  Hit any user to continue.

                                  L Offline
                                  L Offline
                                  Luc Pattyn
                                  wrote on last edited by
                                  #16

                                  Richard A. Dalton wrote:

                                  You beat me to it

                                  only just. :-D

                                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                                  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