Logics is really hard to learn
-
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.
-
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.
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
-
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.
VUnreal wrote:
why can't people learn elementary logic rules?
Maybe they are pengiuns![^] :-O
It's an OO world.
-
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
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:
-
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:
-
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.
-
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.
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]
-
Eddy Vluggen wrote:
Because that repeats the evaluation
good point :doh:
I may or may not be responsible for my own actions
-
musefan wrote:
good point
Meh, too much VB, didn't notice the single equation-character :(
I are Troll :suss:
-
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.
"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.
-
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)
-
"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.
-
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.
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.
-
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.
VUnreal wrote:
This is twice as shorter!
What the heck does that mean? Twice as shorter than what? :confused:
-
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.
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.
-
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.
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.