Reflect on Your Mistakes

by in CodeSOD on

While Java didn't invent putting a toString method in the base class of every object, it certainly made it mainstream. The nice thing about the method is that it allows you to turn any object into a string, though it's up to the implementor to decide what a good string representation is. But what if you want to ensure that the object you're handed is really and truly a string, not just something you can convert to a string?

teknopaul's co-worker found their own solution:


Minimal Commentary

by in Coded Smorgasbord on

Comments explain a lot about our code. And sometimes, the comments explain more than the code itself.

Alastair found this lovely comment, which demonstrates an excellent, if confusing, understanding of a boolean "or":


Suspicious Contents

by in CodeSOD on

While poring through some VB .Net code, John noticed some odd things in their datamodel. For example, a different process would scan files, and log any "suspicious" files into a database. The program John supported would then report on that data.

One of their classes had a property which looked like this:


Spaced Out Replacement

by in CodeSOD on

You have some text, and need to replace every sequence of spaces with a single space. E.g., My text becomes My text. Now, if you're most of us, you ignore the famous quote and reach straight for regexes.

But Samuel's co-worker isn't most of us.


Testing 1,2,3,4,5

by in Error'd on

An anonymous friend sent us our frist test in prod for this week. And then there will be more, and more, and more. "This came up in Kaspersky's Blog's RSS. If you're lucky they might still have the error up in the original URL." I don't know if "chron" is just how "cron" translates to Russian and back, but the test appears to have succeeded.

timely post


Totally Valid

by in CodeSOD on

Greg's co-worker really wanted to make sure that a variable was correctly set to true or false. So they did this:

if (isValid == true)
{
	isValid = true;
}
else
{
	isValid = false;
}

Metaception

by in CodeSOD on

Meta-programming- programs which generate programs- is a delightful hobby, but usually shouldn't be used in production code. Usually. I mean, if you're working in LISP, 90% of your program is going to be macros.

But if you're using PHP and JavaScript, there's good odds that someone you work with has decided to combine these two tastes together to create something nobody wants to taste.


Finding the Right Size

by in CodeSOD on

Zeke sends us a C# snippet from an extract-transform-load process his company uses. It's… special.

private void ResizeColumn(string table, string column, int minSize)
{
    if(null == _connection) return;

    string sqlReadSize = "SELECT DATA_LENGTH,DATA_TYPE,DATA_PRECISION,DATA_SCALE FROM USER_TAB_COLS WHERE TABLE_NAME = '" + table.ToUpper() + "' AND COLUMN_NAME = '" + column.ToUpper() + "'";
    string data_length = "";
    string data_type = "";
    string data_precision = "";
    string data_scale = "";
    string sizeInfo = minSize.ToString();
    
    IDataReader r = null;

    try
    {
        r = _connection.DbAccessor.ExecuteSqlText.ExecuteReader(sqlReadSize);
        if(null != r && r.Read())
        {
            if(!r.IsDBNull(0)) data_length = Convert.ToString(r[0]);
            if(!r.IsDBNull(1)) data_type = Convert.ToString(r[1]);
            if(!r.IsDBNull(2)) data_precision = Convert.ToString(r[2]);
            if(!r.IsDBNull(3)) data_scale = Convert.ToString(r[3]);

            r.Close();
            r = null;

        }
    }
    catch(Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        return;
    }
    finally
    {
        if(null != r)
        {
            r.Close();
            r = null;
        }
    }
    if(data_type == "NUMBER")
    {
        return;
    }

    if(data_type == "DATE")
    {
        return;
    }

    if(data_type == "CLOB")
    {
        return;
    }

    if(data_type == "BLOB")
    {
        return;
    }
    
    if(minSize <= Convert.ToInt32(data_length))
    {
        return;
    }

    string sqlAlterSize = "ALTER TABLE " + table + " modify " 
        + column.ToUpper() + " " + data_type + "(" + sizeInfo + ")";


    try
    {
        _connection.DbAccessor.ExecuteSqlText.ExecuteScalar(sqlAlterSize);
    }
    catch(Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
        return;
    }
}

Archives