Bug Vanquisher

30 September 2009

Infinite Circular Looping

Filed under: Tips — Tanveer Badar @ 10:19 PM

Want to turn a linear sequence into a circular one so you have go round and round over it as long as needed? Here is how (with the added bonus of resetting and stopping the iteration)

   IEnumerable<T> GetNextInterval<T>( IEnumerable<T> input )
        {
            bool flag = true;
            while( flag )
            {
                input.Reset( );
                foreach( T t in input )
                {
                    if( end )
                    {
                        flag = false;
                        end = false;
                        break;
                    }
                    if( reset )
                    {
                        reset = false;
                        break;
                    }
                    yield return t;
                }
            }
        }

Setting reset to true short circuits the loop and starts from beginning again. Setting end to true terminates it entirely.

28 September 2009

Solution to XmlTextReader’s Fiasco

Filed under: Fun, Tips — Tanveer Badar @ 10:22 PM

He’s not that knowledgeable. Mr. pretentious. And he didn’t try this either:

”, hexadecimal value 0x1B, is an invalid character. Line 46, position 128.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Xml.XmlException: ”, hexadecimal value 0x1B, is an invalid character. Line 46, position 128.

The simple solution is to set CheckCharacters = false; And since you would like to know what the heck am I talking about? XmlTextReader is in the bad habit of failing over Unicode characters.

23 September 2009

Amazing Trace

Filed under: Funnier — Tanveer Badar @ 11:33 PM

3    39 ms    34 ms    42 ms  203.99.170.110
4    42 ms    42 ms    42 ms  221.120.251.9
5    41 ms    42 ms    42 ms  202.125.128.131
6    43 ms    42 ms    41 ms  221.120.250.254
7   174 ms   179 ms   178 ms  166.49.170.17
8   184 ms   178 ms   179 ms  166.49.135.139
9   175 ms   179 ms   178 ms  166.49.208.125
10   269 ms   272 ms   264 ms  166.49.164.65
11   414 ms   417 ms   418 ms  206.223.115.17
12   415 ms   408 ms   409 ms  209.240.199.162
13   422 ms   426 ms   434 ms  207.46.43.3
14   419 ms   417 ms   418 ms  207.46.43.39
15   432 ms   434 ms   443 ms  207.46.43.117
16   441 ms   443 ms   443 ms  207.46.43.184
17   439 ms   435 ms   434 ms  207.46.43.172
18   438 ms   435 ms   434 ms  10.22.8.50

Have you ever seen 10.x.y.z appearing on tracert output before?

16 September 2009

The Off-by-1 Game

Filed under: Bugz, S&G, Tips — Tanveer Badar @ 7:54 PM

Consider this:

for( int i = list.Count – 1 ; i >= list.Count – 50 ; –i , )
.     array [ i ] = list [ i ];

Intention was the get the last 50 terms from list. Ignoring the facts that it will throw a null reference exception if the list is null and an index-out-of-range exception if it contains no elements it also fails to copy all 50 elements. The condition is to be blamed.

Correct way is not to invent your own logic and use what the framework offers.

if( list != null && list.Count > 50 )
    list.RemoveRange( 50 , list.Count – 50 );

For added bonus, consider this paging code.

for( int i = 0 , index = 0 ; i < some_huge_number ; ++i )
{
    page.Results.Add( results [ i ] );
    if( index == 10 )
    {
        pages.Add( page );
        page = new Page( );
        index = 0;
    }
    ++index;
}

This code strives to divide results into pages each with 10 results. Can you imagine what this will do if there were only 9 results?

It will proceed to add those results to page, then, do nothing. Because of the incorrect if condition. Again, the correct way is divide-and-remainder approach as demonstrated here.

When Oracle Met A Computer Architect

Filed under: Bugz, Dev inside!, Tips — Tanveer Badar @ 7:49 PM

Oracle can’t handle more than 1000 expressions in a single in clause. (Really stupid restriction, I know and here’s the tip: you can do (, ,,,,,) or abc in (,,,,,,) ad infinitum).

When compilers software pipeline a loop, they calculate two things at first. The remainder and the dividend.

(This post got too keyword heavy. Matrix fans will curse me that its the other Oracle. That Oracle’s users will not find all ORA-xyz error descriptions here. Architects may not have any interest in writing computer code. Computer architects will not see any processors discussed. One post to rule them all. ]:) )

The problem at hand was hand generating a query with in clause in its where section. Number of expressions was unbounded. And the genius who came up with the loop did this:

for( int i = 0 , n = 0 ; i < count ; ++i )
{
    n++;
    if( n == 1000 )
    {
        quwry += " ) or in ( ";
        n = 0;
    }
    query += list [ i ] + ‘,’;
}
query = query.Remove( query.Length – 1 , 1 );

Remember this is the work of someone who has never heard about software pipeline and may never know what gcc or cl are. Thinking one level above will help us arrive at the correct solution (notice, there is nothing wrong functionally with the above code either. It does what it says it does.):

int n = numbers.Count / stride , k = numbers.Count % stride;
StringBuilder builder = new StringBuilder( );
if( n > 0 )
{
    builder.AppendFormat( "'{0}’" , numbers [ 0 ] );
    for( int j = 1 ; j < stride ; j++ )
        builder.AppendFormat( " , ‘{0}’ " , numbers [ j ] );
    for( int i = 1 ; i < n ; ++i )
    {
        builder.Append( separator );
        builder.AppendFormat( "'{0}’" , numbers [ i * stride ] );
        for( int j = i * stride + 1 ; j < ( i + 1 ) * stride ; j++ )
            builder.AppendFormat( " , ‘{0}’ " , numbers [ j ] );
    }
}
if( k > 0 )
{
    if( n > 0 )
        builder.Append( separator );
    builder.AppendFormat( "'{0}’" , numbers [ n * stride ] );
    for( int j = n * stride + 1 ; j < numbers.Count ; ++j )
        builder.AppendFormat( " , ‘{0}’ " , numbers [ j ] );
}

No repetitive checking of one variable which also displays off-by-1 error. And which is also mission the other correction in first line of this post in bold.

13 September 2009

is This Beautiful?

Filed under: Dev inside!, Rant vs Vent — Tanveer Badar @ 10:01 PM

CodeTypeDeclaration tablerow = new CodeTypeDeclaration
{
    IsClass = true ,
    Name = ( row [ "TABLE_NAME" ] + "Row" ) ,
    IsPartial = true ,
    BaseTypes = { datarowref } ,
    Members =
    {
        new CodeMemberField( ( row [ "TABLE_NAME" ].ToString( ) + "DataTable" ) , "table" + row [ "TABLE_NAME" ] ),
        new CodeConstructor
        {
            Attributes = MemberAttributes.Public ,
            Parameters = { parameter } ,
            BaseConstructorArgs = { buildervar } ,
            Statements =
            {
                new CodeAssignStatement( new CodeFieldReferenceExpression( thisref , "table" + row [ "TABLE_NAME" ] ) , new CodeCastExpression( row [ "TABLE_NAME" ].ToString( ) + "DataTable" , tablefield ) )
            }
        }
    }
};

To some, it may be but for me it is nauseating. What happened to all those .Add and .Insert calls?

CodeDom Fun

Filed under: Dev inside! — Tanveer Badar @ 5:52 PM

Writing CodeDom statements by hand is no fun. Visual studio team members who wrote the code to generate data sets must be the finest. For example, guess what would be the output of this statement:

constructor.Statements.Add( new CodeAssignStatement( new CodeFieldReferenceExpression( new CodeThisReferenceExpression( ) , "table" + row [ "TABLE_NAME" ] ) , new CodeCastExpression( row [ "TABLE_NAME" ].ToString( ) + "DataTable" , new CodeFieldReferenceExpression( new CodeThisReferenceExpression( ) , "Table" ) ) ) );

Just something like

this.tableDT = ( DTDataTable ) (this.Table );

Side note:

I am back!

Create a free website or blog at WordPress.com.