Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
C

ClementsDan

@ClementsDan
About
Posts
31
Topics
10
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Who needs childNodes when you have innerHTML?
    C ClementsDan

    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'".

    The Weird and The Wonderful html css algorithms question

  • \r\n
    C ClementsDan

    The Chr() = Convert.ToChar() thing makes some sense, but why int.Parse("13") instead of just 13?

    The Weird and The Wonderful ruby

  • Anagram finder
    C ClementsDan

    The 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.

    The Weird and The Wonderful regex csharp database question

  • Comparing apples to oranges, or strings to bools
    C ClementsDan

    I 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 is NULL, and our string class has an operator==(const char*) — which does not properly check for NULL.

    Clever Code question

  • How to handle dates _properly_. [modified]
    C ClementsDan

    So, was this a replacement for an older program that used "19 +"?

    The Weird and The Wonderful tutorial json question

  • How to group by hour properly. [modified]
    C ClementsDan

    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;

    The Weird and The Wonderful database tutorial

  • Quantity, not quality
    C ClementsDan

    I 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.

    The Weird and The Wonderful debugging

  • Quantity, not quality
    C ClementsDan

    I 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;
               }
    
    The Weird and The Wonderful debugging

  • 16 layers of ifs, Who dare challenge this?
    C ClementsDan

    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)
    {
    // ...
    }

    ?

    The Weird and The Wonderful question

  • True/False Dichotomy
    C ClementsDan

    Many C compilers (and ancient C++ compilers) don't support the bool type. The expression (0==0) has the advantage of having type bool when possible and int otherwise.

    The Weird and The Wonderful

  • Zero-choice alternative
    C ClementsDan

    Some code I encountered yesterday included:

    if (string.IsNullOrEmpty(client) || string.IsNullOrEmpty(client))

    I think it's just a typo.

    The Weird and The Wonderful csharp

  • Zero-choice alternative
    C ClementsDan

    I've seriously used code like that to check for NaN.

    The Weird and The Wonderful csharp

  • Percent-decoding a URL
    C ClementsDan

    Right. It's a CString.

    The Weird and The Wonderful question

  • Percent-decoding a URL
    C ClementsDan

    "%253F" → "%3F" → "?". I don't know of any standard that encodes a question mark that way.

    The Weird and The Wonderful question

  • Percent-decoding a URL
    C ClementsDan

    Shortly 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", "~");

    The Weird and The Wonderful question

  • OMG a "Goto"
    C ClementsDan

    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 with GOTO. SELECT CASE didn't exist, so you had to use ON...GOTO. Multi-line IF statements didn't exist, so you either had to cram everything on one line or use GOTO. And exceptions didn't exist, so you used ON ERROR GOTO. So you ended up with code like this, filled with GOTO. And furthermore, line numbers were mandatory on every line, so it was very difficult to tell which lines were GOTO targets and which weren't. And this is what caused all the animosity towards the GOTO statement.

    The Weird and The Wonderful asp-net com help question

  • A big if
    C ClementsDan

    Recently, 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");
    }

    The Weird and The Wonderful ruby help

  • One guidelines for C/C++ programmer
    C ClementsDan

    It's easier for me to remember the == than it is to remember to write the condition backwards.

    The Weird and The Wonderful help c++

  • Looking at this math is making me num...
    C ClementsDan

    "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."

    The Weird and The Wonderful question lounge

  • Printer-friendly, not programmer-friendly [modified]
    C ClementsDan

    Good guess. I showed him how to do (B) today, and he was impressed at how much simpler it was.

    The Weird and The Wonderful javascript html database data-structures question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups