Yet another code snippet of the day

by Jon Sagara July 22, 2009 at 1:53 PM

Man, this is getting bad. See anything wrong with this?

INSERT INTO [dbo].[MyTable]
(
    <snip />
    [CreatedUtcDate],
    [ModifiedUtcDate]
)
VALUES
(
    <snip />
    GETDATE(),
    GETDATE()
)

Tags: , , ,

Blog

Another code snippet of the day

by Jon Sagara July 22, 2009 at 12:45 PM

Today's a good day. Check this out:

private static Dictionary<string, string> _dict = new Dictionary<string, string>();

static MyPage()
{
    //_dict.Add("item1", "val1");
    //_dict.Add("item2", "val2");
    //_dict.Add("item3", "val3");
    //_dict.Add("item4", "val4");
}

private void InitDictionary()
{
    try
    {
        _dict.Add("item1", "val1");
        _dict.Add("item2", "val2");
        _dict.Add("item3", "val3");
        _dict.Add("item4", "val4");
    }//END TRY
    catch
    {
        _dict.Clear();
        _dict.Add("item1", "val1");
        _dict.Add("item2", "val2");
        _dict.Add("item3", "val3");
        _dict.Add("item4", "val4");
    }//END CATCH
}

So what happened here? The guy decided that initializing the static dictionary variable in the static constructor wasn't good enough, and that he needed to do it on every page request, so he created an instance method to do the initialization. For some reason, though, he kept getting an exception when initializing the dictionary after the first page view. Hmm... bettter just swallow the exception, clear the dictionary, and try again.

*slaps forehead*

Tags: , , ,

Blog

Code snippet of the day

by Jon Sagara July 22, 2009 at 9:54 AM

Just ran across this awesomeness today:

string campaignId = null;

/* ------------------------------------------------------------------------
 * see if the campaign id is in the request query params, so that a default
 * based on the url can be set.
*/
foreach (object key in Request.Params)
{
    string objCampaign = Convert.ToString(key);
    if (string.Compare(objCampaign, "campaignid", true) == 0)
    {
        campaignId = Request[Convert.ToString(objCampaign)];
        break;
    }//END IF
}//END FOREACH
/* ---------------------------------------------------------------------- */

Huh. Well, that's one way to do a Request.QueryString["campaignid"] lookup.

Tags: , , ,

Blog

A sign you've been working too many hours lately

by Jon Sagara August 31, 2008 at 8:29 PM

You get sloppy and write something like this:

if (string.IsNullOrEmpty(campaignId))

{

    throw new Exception(string.Format("Missing campaign ID in the query string: {0}", campaignId));

}

D'oh.

Tags:

Blog

try/swallow

by Jon Sagara August 21, 2008 at 10:33 AM

Dear fellow coders,

Please don't ever do this:

private static void SomeFunc()

{

    try

    {

        DoSomething();

        DoSomethingElse();

    }

    catch

    {

        // eat the exception  <-- NO!  BAD CODER!

    }

}

Love,

Jon

Tags: ,

Blog

Nice job, Unnamed Financial Institution

by Jon Sagara March 11, 2008 at 12:45 PM

To their credit, I was actually able to sign back in and complete my registration without a hitch, but, still, I expect much more from a large financial institution that handles my investments.

Tags:

Blog