|
This bad on so many levels.
Let's look at:
0 & 0 & 0 & 0 & 0 & 0 & 0 & conv8
Remeber kids, conv8 is a Variant so it will be type checked even though it is /probably/ a String .
Every 0 is an Integer that will be cast to a string. Then EACH AND EVERY & creates a new string, which gets thrown away with the next & .
This is so bad, I love it!
Reminds me of a way back when fixed length message that we had in a system if days gone by, that was built up using a this & that & the & other type assignment. The message was regularly being buit up and broken down.
The breakdown would be
this = Left$(message,4)
message = Mid$(message. 5)
that = ...
Changed it to use a function that built a fixed length string that then got updated. Boom, super fast (by VB standards)
Similarly the break up was changed to only extract what was needed and used Mid$() exclusively.
It still holds true today. String manipulation normally requires memory allocation. Work out what you need FIRST and reduce the re-allocs every 3 steps of the friggin way.
Slightly harder to code, but when performance maters it is important.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
williamnw wrote: Every 0 is an Integer that will be cast to a string. Then EACH AND EVERY & creates a new string, which gets thrown away with the next &.
For all its faults, the VB6 compiler is a little better than that - it is capable of concatenating string literals at compile time.
I remember maintaining a report-writing program that abused VB's string handling similarly. It created an RTF report, but generally appended characters pretty much 1 to 5 at a time, and the eventual reports ran up to about 14MB as RTF, so you can imagine how many string copy's resulted there.
I ported the core of the engine to C++, resulting in a speedup well over one thousand fold!
|
|
|
|
|
Once, in the long-ago murky past, before learning the wonders of Int32.ToString("Dx"), and still being relatively new, I wrote something like this
public string Pad(int val, int places)
{
int realplaces=0;
if (val < 10) // notice that this includes all negatives, so FAIL
realplaces=1;
else if (val < 100)
realplaces=2;
else if (val < 1000)
realplaces=3;
...
if (realplaces >= places)
return val.ToString();
return new string(' ', places - realplaces) + val.ToString();
}
|
|
|
|
|
Well, you could also learn the wonders of Math.Log10 ...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
string s = number.ToString();
if (s.Length >= placesWanted)
return s;
return new string(' ', placesWanted - s.Length) + s;
Because I still dislike the failure of 1.5 + 1.5 == 3
|
|
|
|
|
What has the above to do with Math.Log10 ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I prefer to avoid doubles when possible, even if it's a stupid idea. And why wastefully calculate the log when you already have the actual length of the number right in your hands?
|
|
|
|
|
Stefano Basili wrote: thank god there was no need for a Conv64 !!!!
No kidding. It would be ugly, but doable with just loads of copying/pasting
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hey! Did you post this code with the author's consent? Gosh, he must've had a hard time on this. Haha
|
|
|
|
|
Sure ... and the original code was even worse than that!
Don't write code: generate it!
|
|
|
|
|
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("block_name").cloneNode(true)); } catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("block_type").cloneNode(true)); } catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("title").cloneNode(true)); } catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("list_id").cloneNode(true)); } catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("format_number").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("format_force").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("visible").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("titre").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("bloc_width").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("bloc_sens").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("bloc_data_type").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("locked").cloneNode(true));} catch(e) {};
try { oXMLRecord.appendChild(oXMLBlock.selectSingleNode("display_type").cloneNode(true));} catch(e) {};
Why have methods when you can just copy-paste the code you need onto each line? And why bother checking such things as if a node exists before trying to do anything with it when you can just catch any exceptions?
This is from a 4165 line javascript file of consistently awful code. The block above is a small sample but there were perhaps three times as many lines just like those where only the name of the child node changes. Another function that could have been somewhat improved had the programmer taken a minute to use an array and a loop rather than his copy-paste approach to everything:
function displayButtonsBlocExistForCell(sCell)
{
if(sCell=="A1")
{
disableAllButtons()
}
else
{
if(sCell.substring(0,1)=="A"||isRow1(sCell))
{
disableControlButton("btnBodyColumn");
disableControlButton("btnBodyLine");
disableControlButton("btnBodyText");
enableControlButton("btnBodyModify");
disableControlButton("tdAddNewDocLink1");
disableControlButton("tdAddNewDocLink2");
disableControlButton("tdAddComment");
disableControlButton("tdDelComment");
disableControlButton("btnEditDelete");
if(sCell.substring(0,1)=="A")
{
enableControlButton("btnEditInsertRows");
enableControlButton("btnEditDeleteRows");
disableControlButton("btnEditInsertCols");
disableControlButton("btnEditDeleteCols");
}
else
{
disableControlButton("btnEditInsertRows");
disableControlButton("btnEditDeleteRows");
enableControlButton("btnEditInsertCols");
enableControlButton("btnEditDeleteCols");
};
};
};
};
Also notice the additional semi-colons..! Sure, it only adds an empty statement and you can write ;;;; anywhere in between statements or declarations in JS without changing anything, but much like the if (myVar == true) bunch it doesn't exactly make you look like you know what you're doing or care much about doing it properly.
(In case anyone don't get the thing with the == true, adding == true to any boolean expression results in another boolean expression that is necessarily equivalent to the original boolean expression. Another, even stupider variant is the ternary myVar? true : false ... Sigh.
|
|
|
|
|
Nice, very nice... My apologies if this is the code that you are currently trying to deal with (and it does sound like that is the case).
As for me, I'm feeling kind of happy right now because I am in the midst of starting up a new project that will be all new development so I won't have to be dealing with this kind of nonsense. If I'm lucky, development will be controlled enough that none of this will crop in to the code base either (I'm probably not that lucky).
|
|
|
|
|
I think these came from using external consultants and no peer review to be honest. The only people evaluating anything the consultants did were non-techies so only new buttons in the UI, more pages, more lines of code, and ridiculously simplistic metrics like that were actually being observed. Hence we had effectively set up a system that was basically a trap, for ourselves! I want to point out I was not a part of this team at that time though...
|
|
|
|
|
dojohansen wrote: I want to point out I was not a part of this team at that time though...
That was also my excuse for the code I'd been involved in for the last two years.
It's still embarrassing when someone looks at the code for the first time, or notices the bugs in the code in runtime situations, and gives those not so nice glances in the direction of the current developers. It seems like the sins of those who originally wrote the stuff end up becoming the sins of those currently working in it - guilt by association.
|
|
|
|
|
Yes, the current developers always get the heat for things that may have been done years before they even started working at the company. Such is life
|
|
|
|
|
CIDev wrote: Yes, the current developers always get the heat for things that may have been done years before they even started working at the company. Such is life
Can anyone say "Refactoring"? I've spent a good deal of my career refactoring bad code, even when I had to do it on the sly to keep "if it ain't broke, don't fix it" bosses from burying me in manure. If it's bad code, it IS broken and needs to be fixed.
If your life sucks, you're working by the wrong set of rules.
|
|
|
|
|
I normally "tweak" or just plain replace old code that may work, but is really bad code. I don't always have enough time to replace as much bad code as I would like, but I do it whenever possible.
Bill W
|
|
|
|
|
CIDev wrote: I normally "tweak" or just plain replace old code that may work, but is really bad code. I don't always have enough time to replace as much bad code as I would like, but I do it whenever possible.
I know what you mean I'm in the process of finally, COMPLETELY refactoring an application created by an entry-level programmer in .NET 1.0. It's not terrible, considering her skill level and the state of Visual Studio.NET at that time, but it wasn't good. I'm guessing that the size of the code in both .aspx and .aspx.vb files has decreased by half. It only took me four years to get enough knowledge of .NET and ASP.NET, then to get permission to go for it
|
|
|
|
|
More lines of code is a bad thing in my mind. The fewer, the better if it's not sacrificing readability, performance, or functionality...which it rarely does.
|
|
|
|
|
That is just hideous
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Yes.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
dojohansen wrote: the additional semi-colons
I used to do that with Pascal, so I could count them to know how many statements were in the program.
|
|
|
|
|
But here the semi-colon adds an empty statement!
This is a javascript function block with two statements:
function f()
{
a = 5;
++i;
}
The function itself is a declaration, not a statement, and adding a semicolon merely makes it a block with two statements followed by an empty statement.
Admittedly the difference between a declaration and a statement is a bit fuzzy in this case, since in js a function declaration is equivalent to an assignment statement, like this:
var f = function()
{
a = 5;
++i;
};
In which case it's correct to include the semicolon (because it ends a statement in this case).
Anyway, my real problem is the insane amount of reduncancy in the code. I declare war on copy-paste "programmers" the world over!
|
|
|
|
|
dojohansen wrote: I declare war on copy-paste "programmers" the world over!
To the death!!! ...alright, To the death or reform!! (I've been guilty of this occasionally as well, especially when I was in a screaming hurry...but it's VERY rare that I've left copy-and-paste stuff in my code longer than the first moment I could go back and revise it).
|
|
|
|
|
Whats wrong with the semi-colons; I put semi-colons everywhere; It is a C/C++ thing;
|
|
|
|