Bug Vanquisher

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.

Advertisements

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.

28 March 2009

Work Efficiency

Filed under: Bugz — Tanveer Badar @ 7:01 PM

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current Dload  Upload   Total   Spent    Left  Speed
109   219  109   219    0     0    438      0 –:–:– –:–:– –:–:—   438

I like this downloader, given how it built redundancy by downloading 109% of a 219 byte long file.

Work Efficiency

Filed under: Bugz — Tanveer Badar @ 6:22 PM

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current Dload  Upload   Total   Spent    Left  Speed
109   219  109   219    0     0    438      0 –:–:– –:–:– –:–:—   438

I like this downloader, given how it built redundancy by downloading 109% of a 219 byte long file.

14 February 2009

The Impossible Task

Filed under: Bugz — Tanveer Badar @ 7:45 PM

image
+
image
= Impossibility of minimizing multiple windows

Welcome to Windows 7.

[Edit 08/03/09: It is possible to right click on a thumbnail, usual window context menu appears there. But I still think it is very undiscoverable.]

image_thumb1

20 January 2009

Firefox Updated, Close First Window

Filed under: Bugz — Tanveer Badar @ 5:28 PM

Save Your 5 Minutes Installing AVG on Windows 7

Filed under: Bugz — Tanveer Badar @ 5:25 PM

By reading here….

Free edition of AVG has a dependency on MFC 8.0. However, they fail to ship the msp files with their program and Windows 7 doesn’t come with MFC 8.0 pre-installed either.

AVG will install successfully but will fail to run any of the UI components I had to install VS anyway, hence problem solved for me.

Choose your poison.

29 November 2008

It Bit You In The Butt

Filed under: Bugz — Tanveer Badar @ 3:20 AM

new Guid( )

Fact 1: Guid is a value type.
Fact 2: Values types are initialized to all zero upon construction.
Fact 3: Value types cannot have default constructors.
Conclusion 1: new Guid( ) should earn you lots of spanking.
Conclusion 2: Guid.NewGuid( ) is the right thing to do.

24 May 2008

Moving the Ground from Underneath Your Feet

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

Run check disk while doing something important. :) The drive should disappear from everyone’s view as check disk opens it under exclusive RW access.

This brings us to some things, some tests I would like to run on many applications. Typical happens as I write this.

1- Visual Studio setup just hangs. *

2- Firefox crashes upon close when running from that drive. *

3- Tortoise SVN background process will crash. **

4- Opening any MS Office program will invoke windows installer instead (expected). If office was installed on that drive.

5- OneNote’s background keystroke monitor will crash. **

There are lots of things programs access file system for.

1- Custom logging: Not essential enough to actually crash the application. I would love to see some example program doing exactly that.

2- User data: Nothing should ever crash for this. Instead prompt for an alternate location.

3- On demand functionality: User invoked something. You need to load code/resources/configuration from a disk not available. What will you do? Crashing is very gross.

4- Configuration data: This can happen anytime. Don’t tell me you don’t keep a default copy of your required, must-have, 24/7 available, configuration ready at hand in resource form. Embedded right into your executable images.

3 May 2008

Woes and Worries

Filed under: Bugz, Rant vs Vent, Windows Vista — Tanveer Badar @ 5:52 PM

I wonder why people cry of the sucky video drivers pushed out by NVidia. The monsters Intel rolls out for their D946 GZ chipsets must be on par. I have had more GPU resets from them than power failures!

Even new yahoo messenger causes the graphics stack to crash weirdly.

[Update-After five minutes]

Here’s the most charming, updated EULA of the driver I downloaded.

This file should be replaced by the current license file when built.

 

I mean what they are playing at?

Older Posts »

Blog at WordPress.com.