Oh, no. "Switch" doesn't do anything so clever as all that. There are about five similar blocks of code in this particular class and some of them check Switch:
if(Switch==true){
//Switch is set on the previous line [poultine: no, it isn't]
if(i==0){
i=1;Switch=false;
}else{
i=0;Switch=false;
}
}
control.Text = i.ToString();
At least that one makes more sense, since i actually changes its value each time through. That duplicated "Switch = false;" makes me want to gnaw my own arm off, though.
And i never gets anything other than 1 or 0. It just really wants to be a bool.
Poultine
@Poultine
Best posts made by Poultine
Latest posts made by Poultine
-
RE: Refactor This!
-
RE: Refactor This!
@dhromed said:
The most expedient way of inverting a [1-0] combination is 1- i, though the speed difference might be negligible unless you do it about 10,000 times.
Huh? What about i = i^1?
More importantly, are you counting the integer to string conversion in there as well? I would expect the tertiary (i==0) ? "1" : "0" to run much faster than anything else, though it will soak up a bit more memory for the code and constants.
And yes, as far as keeping a low refactoring-profile on this, I'm not guaranteeing that if i is not in (0, 1) then it will be set to 1, but... I'm really OK with that.
-
RE: Refactor This!
No, that's hardly all the code. It's (naturally) a far more significant beast. The "loop" here is really the ItemDataBound callback for an ASP repeater. The exciting part of this is that "Switch" is an object field, as is "i" [shudders].
All things considered, I went with
control.Text = (i==0)?"1":"0";
Switch=true;
Incidentally-- the comment for this block of code is "set the alternating color". It's WTF-tastic.
-
Refactor This!
[int a loop]
if(i==0){i=1;switch=false;}else{i=0;switch=false;}
if(i == 0) {
control.Text = i.ToString();
i++;
} else {
control.Text = i.ToString();
i=0;
}
switch=true;
A part of me weeps whenever I see this sort of thing.
Every time you put a duplicate line of code at the top (or bottom) of each clause in an if statement, an angel gets set on fire. -
RE: Check if email address is already in use
WTF is up with people who BSD formatting, but then remove ALL spaces possible from each individual line of code? It drives me completely nuts. Love your whitespace or hate it, but please, at least be consistent.
-
RE: In the world of bits...
- for loop should go to MAX_INT
- "value >>= 1" should be "value = (value & 0x1e) / 2"
- tag should be a pointer to a float
- for loop should include additional, unused loop var.
- "double res" should be "register double res"
- for loop should go to MAX_INT
-
To loop or not to loop?
Missing the purpose of the loop:
bool firstTime = true;
foreach (obj x in collection)
{
if (firstTime)
{
[code not referencing "x" in any way at all]
firstTime = false;
}[code referencing "x"]
}
Wouldn't want to "check collection.Count" before the loop. No. Silly talk, that.