Bug Vanquisher

28 October 2009

How Not To Blog

Filed under: S&G — Tanveer Badar @ 11:07 PM

Beginning your post with something like

Whenever I pick up a new technology I must admit that I feel kind of stupid, like a dummy or a complete idiot.

sends the wrong message to your readers in my books. You are implying that they are inherently stupid, imbeciles and morons. And your unearthly piece of work is too complex for mere mortals to understand.

Here’s a tip, get a life and get off your high horse people. No one wants to be like you. You may be an idiot, we are not. We like our own wits, thank you very much.

Advertisements

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.

4 March 2009

Yawn

Filed under: S&G — Tanveer Badar @ 10:51 PM

.

19 January 2009

General Public Notice

Filed under: S&G — Tanveer Badar @ 12:55 AM

I do not entertain business calls after office hours.

Blog at WordPress.com.