We have a webpage that contains a table with collapsible sections. Straightforwardly, the code behind the show/hide links took the approach of (1) searching for the tr elements that need to be toggled, and (2) changing the .style.display property to "" or "none" as appropriate. Less straightforwardly, step #1 was implemented not with DOM methods, but by searching the innerHTML for a string starting with "<TR ID=DISPFILE_". In uppercase, so it wouldn't work on Firefox which normalizes the tag names to lowercase. At least the original programmer was consistent: Step #2 was implement by a similar text search for "STYLE='display: none'".
ClementsDan
Posts
-
Who needs childNodes when you have innerHTML? -
\r\nThe Chr() = Convert.ToChar() thing makes some sense, but why int.Parse("13") instead of just 13?
-
Anagram finderThe impressiveness of (for example) a 87,178,291,200-element search space for 14-letter anagrams should have gotten the original author to use less permissiveness in choosing an algorithm. If he could miss such an obvious rectification for the problem, he doesn't deserve to earn any kind of certification in software development.
-
Comparing apples to oranges, or strings to boolsI spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line
if (strProtocol == FALSE)
which should have been
if (strProtocol.IsEmpty() == FALSE)
So why did the original compile in the first place? Because
FALSE
is #defined as 0, and coincidentally so isNULL
, and our string class has anoperator==(const char*)
— which does not properly check forNULL
. -
How to handle dates _properly_. [modified]So, was this a replacement for an older program that used "19 +"?
-
How to group by hour properly. [modified]You don't need to check for nulls, because SUM will automatically exclude them. Don't know why they wouldn't use a TIME column, unless they were using SQLite, but even there you can just do:
SELECT strftime('%H', TimeText) AS Hour, SUM(Cost) FROM MyTable GROUP BY Hour;
-
Quantity, not qualityI did eventually decide to move the body of each loop into its own function. Still uglier than I'd like it to be, but yes, I was reluctant to rewrite it, especially when the problem I was trying to fix turned out to be somewhere else.
-
Quantity, not qualityI found this function in the service I'm having to debug. I've omitted a few hundred lines of the code so that you may more clearly see the control flow.
void ProcessMessage()
{
if ( )
{}
if ( )
{}
if ( )
{}
if ( )
{}
while ( )
{
if ( )
{
continue;
}if ( ) { continue; } if ( ) { continue; } if ( && ) { if ( || ) { if ( ) { } else { } continue; } } if ( ) { continue; } for ( ; ; ) { if ( ) { } if ( ) { if ( ) { } continue; } if ( ) { if ( ) { } continue; } if ( ) { if ( ) { } else { } if ( || || ( && )) { if ( ) { } } else { if ( ) { } } continue; } if ( ) { for ( ; ; ) { if ( ) { continue; } if ( ) { if ( ) { } else { } if ( ) { } break; } else if ( ) { if ( || ) { if ( ) { if ( ) { } } } if ( ) { } else { } continue; } else { if ( ) { } break; }
-
16 layers of ifs, Who dare challenge this?How about
try
{
hr = Operation1();
if (!SUCCESS(hr)) throw hr;hr = Operation2(); if (!SUCCESS(hr)) throw hr; hr = Operator3(); if (!SUCCESS(hr)) throw hr; // ...
}
catch (HRESULT hErr)
{
// ...
}?
-
True/False DichotomyMany C compilers (and ancient C++ compilers) don't support the
bool
type. The expression (0==0) has the advantage of having typebool
when possible andint
otherwise. -
Zero-choice alternativeSome code I encountered yesterday included:
if (string.IsNullOrEmpty(client) || string.IsNullOrEmpty(client))
I think it's just a typo.
-
Zero-choice alternativeI've seriously used code like that to check for NaN.
-
Percent-decoding a URLRight. It's a CString.
-
Percent-decoding a URL"%253F" → "%3F" → "?". I don't know of any standard that encodes a question mark that way.
-
Percent-decoding a URLShortly before my co-worker left for vacation today, he wrote something like this:
strNew.Replace("%20", " ");
strNew.Replace("%21", "!");
strNew.Replace("%22", """);
strNew.Replace("%23", "#");
strNew.Replace("%24", "$");
strNew.Replace("%25", "%");
strNew.Replace("%26", "&");
strNew.Replace("%27", "'");
strNew.Replace("%28", "(");
strNew.Replace("%29", ")");
strNew.Replace("%2A", "*");
strNew.Replace("%2B", "+");
strNew.Replace("%2C", ",");
strNew.Replace("%2D", "-");
strNew.Replace("%2E", ".");
strNew.Replace("%2F", "/");
strNew.Replace("%2a", "*");
strNew.Replace("%2b", "+");
strNew.Replace("%2c", ",");
strNew.Replace("%2d", "-");
strNew.Replace("%2e", ".");
strNew.Replace("%2f", "/");
strNew.Replace("%3A", ":");
strNew.Replace("%3B", ";");
strNew.Replace("%3C", "<");
strNew.Replace("%3D", "=");
strNew.Replace("%3E", ">");
strNew.Replace("%3F", "?");
strNew.Replace("%3a", ":");
strNew.Replace("%3b", ";");
strNew.Replace("%3c", "<");
strNew.Replace("%3d", "=");
strNew.Replace("%3e", ">");
strNew.Replace("%3f", "?");
strNew.Replace("%40", "@");
strNew.Replace("%5B", "[");
strNew.Replace("%5C", "\\");
strNew.Replace("%5D", "]");
strNew.Replace("%5E", "^");
strNew.Replace("%5F", "_");
strNew.Replace("%5b", "[");
strNew.Replace("%5c", "\\");
strNew.Replace("%5d", "]");
strNew.Replace("%5e", "^");
strNew.Replace("%5f", "_");
strNew.Replace("%60", "`");
strNew.Replace("%7B", "{");
strNew.Replace("%7C", "|");
strNew.Replace("%7D", "}");
strNew.Replace("%7E", "~");
strNew.Replace("%7b", "{");
strNew.Replace("%7c", "|");
strNew.Replace("%7d", "}");
strNew.Replace("%7e", "~"); -
OMG a "Goto"It's an understatement to say that "goto came in handy" in the old style BASIC. You had to use
GOTO
because the language was severely lacking in control flow.DO
...LOOP
didn't exist, so you had to make those loops withGOTO
.SELECT CASE
didn't exist, so you had to useON
...GOTO
. Multi-lineIF
statements didn't exist, so you either had to cram everything on one line or useGOTO
. And exceptions didn't exist, so you usedON ERROR GOTO
. So you ended up with code like this, filled withGOTO
. And furthermore, line numbers were mandatory on every line, so it was very difficult to tell which lines wereGOTO
targets and which weren't. And this is what caused all the animosity towards theGOTO
statement. -
A big ifRecently, I stumbled across this little gem. I don't have the exact code handy, but the gist of it is:
nErrorCode = cFtpConn.SetHost(HOST);
if (nErrorCode == 0)
{
nErrorCode = cFtpConn.SetUser(USERNAME);if (nErrorCode == 0)
{
nErrorCode = cFtpConn.SetPassword(PASSWORD);if (nErrorCode == 0) { nErrorCode = cFtpConn.SetPath(PATH); if (nErrorCode == 0) { nErrorCode = cFtpConn.SetFilename(FILENAME); if (nErrorCode == 0) { // Retrieve files, adding a few _more_ levels of `if` } else { Log("Error setting filename"); } } else { Log("Error setting path"); } } else { Log("Error setting password"); }
}
else
{
Log("Error setting username");
}
}
else
{
Log("Error setting host");
} -
One guidelines for C/C++ programmerIt's easier for me to remember the == than it is to remember to write the condition backwards.
-
Looking at this math is making me num..."all of the variables used were num1, num2, num3 etc. down the line. This is in several pages of code and it goes up to num26," They probably started off calling them a, b, c, ..., z, until learning that "you shouldn't use 1-letter variable names."
-
Printer-friendly, not programmer-friendly [modified]Good guess. I showed him how to do (B) today, and he was impressed at how much simpler it was.