|
You aren't working with my previous employer, are you?
|
|
|
|
|
The advantage of a switch is that it bails as soon as a test passes, thus reducing conditional branching.
Here you could use an if else.
|
|
|
|
|
Or take it out altogether as it's setting 2 Booleans.
|
|
|
|
|
In that case it has the same advantage as the "if" would have; but if that is an important 'optimization', then you already have a bigger problem
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
In my experience Microsoft is pretty much immune to optimisation. You can reduce branching by one or two orders of magnitude and the crappy performance just doesn't budge.
|
|
|
|
|
Probably because it is a micro-optimization; you won't notice much difference if it merely saves you the toggeling of a boolean.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Ha! It was actually an image processing filter mask which used pointer arithmetic. I invented something I called a Summation Threshold Filter which should have been ten times quicker than a Median filter.
Let's just say that this was not apparent.
|
|
|
|
|
Umm...that's not true. A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.) On the other hand, if/else statements do bail as soon as the first passing conditional is found and the associated code block is executed.
Scott E. Corbett
|
|
|
|
|
Scott Corbett wrote: A switch will continue to fall through until you get to a break statement or the end of the switch (i.e. the default case.)
Not in C# - every case is required to have a terminating statement (break , goto , return or throw ).
Unlike C++, C# does not allow execution to continue from one switch section to the next.
...
C# requires the end of switch sections, including the final one, to be unreachable. That is, unlike some other languages, your code may not fall through into the next switch section.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You're right about the switch case and fall through. Been spending too much time playing with C++ lately. My apologies.
Scott E. Corbett
|
|
|
|
|
I believe a switch is just a calculated jump statement. It doesn't work it's way through all the previous possibilities. Yes. Once calculated, the program goes to the break statement then jumps out appropriately. Switch statements are quite fast. In this case, I don't see advantage either way as an if statement is very simple too.
|
|
|
|
|
Well I kind of assumed you knew how to write a switch.
|
|
|
|
|
The only reason I could think for writing or keeping the code this way would be if enabled became an enumerated type and had more than 2 states.
|
|
|
|
|
In that case - create it as an enumerated type, with two values. You may then expand the enumeration without rewriting the existing code.
If you have an important point to make, don't try to be subtle or clever. Use a pile driver. Hit the point once. Then come back and hit it again. Then hit it a third time - a tremendous whack.
--Winston Churchill
|
|
|
|
|
FirstControl.Enabled = !FirstControl.Enabled;
SecondControl.Enabled = !SecondControl.Enabled;
|
|
|
|
|
Couldn't resist:
void EnableFromValue(bool enabled)
{
FirstControl.Enabled = enabled;
SecondControl.Enabled = enabled;
...
}
|
|
|
|
|
it is simple :
void EnableFromValue(bool ?enabled)
{
FirstControl.Enabled=enabled.HasValue?enabled.Value:false;
SecondControl.Enabled=enabled.HasValue?enabled.Value:false;
}
|
|
|
|
|
This works for me
FirstControl.Enabled = SecondControl.Enabled = (enabled) ? false : true;
|
|
|
|
|
LOL! I recently went through my company's entire code base to purge out constructs such as "? true : false".
Even more fun were comparisons such as "if (some_int_var == TRUE)". That's great unless, say "TRUE" is defined as 1 and your variable is set to -1.
|
|
|
|
|
Look. this construct is a perfect alternating on/off switch.
I've used it exclusively over past 10 years.
There is no valid reason not to use it, especially if you favor
Clean, concise, easy to understand code.
Different strokes ...
Tony d
|
|
|
|
|
No, "? false : true" is still superfluous. Just use ! (the not operator). It's certainly more concise, and I would argue, even cleaner and easier to understand.
FirstControl.Enabled = SecondControl.Enabled = !enabled;
|
|
|
|
|
Hey Dan love the use of Boolean operators.
Used them extensively in the old mainframe days when storage was measured in megabytes.
When working with 16mb system storage every byte counted. Here's an old trick to save bytes
a *-6, ctr.
Used this in 360/370 mainframes since adding the opcode (numeric value of 1) would save 1 byte.
nice chatting with a knowledgeable tech
|
|
|
|
|
If you are sure that the function argument named enabled is bool forever? If yes than there is no reason for the switch. If chacne to change the argument type is exist so - I would add into the switch
default:
FirstControl.Enabled = enabled;
SecondControl.Enabled = enabled;
|
|
|
|
|
Al Chak wrote: If chacne to change the argument type is exist so - I would add into the switch
There's no reason to add a switch statement now just in case the argument type changes in six months.
Add the switch when you need it - ie: when the argument type changes.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
If it is not break-en, don't switch it?
[[Thank you, I'm here all week; try the veal...]]
Chaos.
|
|
|
|