Bug Vanquisher

11 January 2011

Facebook Disillusioned

Filed under: Personal — Tanveer Badar @ 11:14 PM

Permanently Delete Account

Your account has been deactivated from the site and will be permanently deleted within 14 days. If you log into your account within the next 14 days, you will have the option to cancel your request.

That is all.

29 October 2009

The World Of Bizarre and Eccentric Failures

Filed under: Tips — Tanveer Badar @ 12:21 PM

public IEnumerable<int> GetIntegers( )
{
    if( <some condition> )
        yield return null;
    yield return <some other value;
}

and the other beauty

public IList<int> Integers { get; set; }

Now befuddled when foreach fails with a NRE. Or when someone extra smart says Integers = null; But don’t blame the other guy who is using your code.

When returning a collection, it is expected that collection itself will never be null (short of OutOfMemoryException)) even when it is empty. Properties which return a collection interface should never have public setters.

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.

23 October 2009

Story of a Parser

Filed under: Dev inside!, Tips — Tanveer Badar @ 8:31 AM

A quick and dirty, precedence parser I wrote in about 2 hours. Minimal error handling, (may be) able to parse expressions of the form

expr := expr combiner expr | expr op ( expr | list ) | term
combiner := AND | OR
op := ‘=’ | in
list := ( term ) | ( term , list )
term := ‘identifer or literal’

op will include usual comparison operators soon. List can only be specified if op is ‘in’. Code follows, you should be able to add the missing pieces yourself as not much more can be revealed.:

public static partial class Parser
{
    static Expression BuildTree( string expr )
    {
        int startIndex = 0;
        Stack<Expression> stack = new Stack<Expression>( );
        BuildTreeRecursive( expr , ref startIndex , stack );
        return stack.Pop( );
    }

    private static void BuildTreeRecursive( string expr , ref int startIndex , Stack<Expression> stack )
    {
        string combiner = null;
        while( startIndex < expr.Length )
        {
            Expression expr1 = null;
            if( string.IsNullOrEmpty( combiner ) )
            {
                expr1 = RecognizeExpression( expr , ref startIndex );
                EatWhiteSpace( expr , ref startIndex );
                combiner = RecognizeOperator( expr , ref startIndex );
            }
            Expression expr2 = null;
            switch( combiner.ToUpper( ) )
            {
                case "AND":
                    expr2 = RecognizeExpression( expr , ref startIndex );
                    stack.Push( new BinaryExpression
                    {
                        LeftSide = expr1 ,
                        RightSide = expr2 ,
                        Type = Expression.Operator.And
                    } );
                    EatWhiteSpace( expr , ref startIndex );
                    combiner = RecognizeOperator( expr , ref startIndex );
                    EatWhiteSpace( expr , ref startIndex );
                    break;
                case "OR":
                    BuildTreeRecursive( expr , ref startIndex , stack );
                    if( stack.Count > 0 )
                        expr2 = stack.Pop( );
                    if( stack.Count > 0 )
                        expr1 = stack.Pop( );
                    stack.Push( new BinaryExpression
                    {
                        LeftSide = expr2 ,
                        RightSide = expr1 ,
                        Type = Expression.Operator.Or
                    } );
                    EatWhiteSpace( expr , ref startIndex );
                    combiner = RecognizeOperator( expr , ref startIndex );
                    EatWhiteSpace( expr , ref startIndex );
                    break;
            }
        }
    }

    private static Expression RecognizeExpression( string expr , ref int startIndex )
    {
        EatWhiteSpace( expr , ref startIndex );
        string attrib = RecognizeAttribute( expr , ref startIndex );
        EatWhiteSpace( expr , ref startIndex );
        string op = RecognizeOperator( expr , ref startIndex );
        EatWhiteSpace( expr , ref startIndex );
        switch( op.ToUpper( ) )
        {
            case "=":
                string value = RecognizeWord( expr , ref startIndex );
                return new BinaryExpression
                {
                    LeftSide = new ConstantExpression( attrib ) ,
                    RightSide = new ConstantExpression( value ) ,
                    Type = Expression.Operator.Equal
                };
            case "IN":
                List<string> values = RecognizeList( expr , ref startIndex );
                return new BinaryExpression
                {
                    LeftSide = new ConstantExpression( attrib ) ,
                    Type = Expression.Operator.In
                };
        }
        return new Expression( );
    }

    private static List<string> RecognizeList( string expr , ref int startIndex )
    {
        List<string> values = new List<string>( );
        if( expr.Length <= startIndex || expr [ startIndex ] != ‘(‘ )
            return values;

        ++startIndex;

        do
        {
            EatWhiteSpace( expr , ref startIndex );
            values.Add( RecognizeWord( expr , ref startIndex ) );
            if( expr.Length <= startIndex || expr [ startIndex ] != ‘,’ )
                break;
        } while( startIndex < expr.Length );

        EatWhiteSpace( expr , ref startIndex );

        if( expr.Length <= startIndex || expr [ startIndex ] != ‘)’ )
            return values;

        ++startIndex;

        return values;
    }

    private static string RecognizeAttribute( string expr , ref int startIndex )
    {
        return RecognizeWord( expr , ref startIndex );
    }

    private static string RecognizeOperator( string expr , ref int startIndex )
    {
        int start = startIndex;
        while( startIndex < expr.Length && !char.IsWhiteSpace( expr [ startIndex ] ) )
            ++startIndex;
        if( start != startIndex )
            return expr.Substring( start , startIndex – start );
        return string.Empty;
    }

    private static void EatWhiteSpace( string expr , ref int startIndex )
    {
        while( startIndex < expr.Length && char.IsWhiteSpace( expr [ startIndex ] ) )
            ++startIndex;
    }

    private static string RecognizeWord( string expr , ref int startIndex )
    {
        int first = expr.IndexOf( "’" , startIndex ) , second = first != -1 ? expr.IndexOf( "’" , first + 1 ) : -1;
        if( first != -1 )
            if( second != -1 )
            {
                startIndex = second + 1;
                return expr.Substring( first , second – first );
            }
            else
            {
                startIndex = expr.Length;
                return expr.Substring( first );
            }
        return string.Empty;
    }
}

This is what you should pass.

string expr = "’attribute1′ = ‘value1′ and ‘attribute2′ = ‘value2′ or ‘attribute3′ in (‘value3′) or ‘attribute4′ = ‘value4′";

Expression expression = Parser.BuildTree( expr );

21 October 2009

Extension Method Abuse

Filed under: Dev inside! — Tanveer Badar @ 4:03 PM

public static <Return Type> GetColumns( this int column , IList< Conditions > conditions ) { /* blah blah blah */ }

The Story of System.IO.FileNotFoundException

Filed under: Tips — Tanveer Badar @ 1:37 PM

‘Impaled by my own sword’, ‘shot myself in the foot’ would be some fine explanations for this exception. Ages ago, I checked ‘break on first chance exception’ for Debug > Exceptions > System.IO.FileNotFoundException. Then, I spent 30 minutes wondering and arguing with others why there was this exception when my code was doing exactly what it should have been doing and nothing changed.

It was long before I remembered to check this setting. So, next time you are bitten by some unexplained exception better see if it is a first chance catch and whether you are catching it as such. Setting is global in VS and persists across projects.

10 October 2009

The Composition Which Isn’t

Filed under: Dev inside!, Tips — Tanveer Badar @ 5:11 PM

Imagine someone trying to write a class which composes two streams together. You better make it a stream itself, meaning something like this:

class CompositeStream : IStream
{
public:
    CompositeStream( IStream* first , IStream* second );
    // blah blah blah
};

Or people will find it amusing that your class isn’t a stream itself.

Similarly, for a mathematical library if you provide a composing function it better be a math-op itself or someone is going to end up in a sticky situation.

class SuperDuperComposingFunction : MathOp
{
public:
    SuperDuperComposingFunction( const std::vector< MathOp* >& operations );
    // blah blah blah
};

Or a cache manager for that matter. It is easy to provide default implementations for simple cache algorithms like LFU, LRU, FIFO etc. But things get interesting if you provide an extensibility point for others to compose them together. Who knows whether I want to archive emails which are older than 3 weeks and I haven’t read them more than once. Things are even more interesting if people need to sub-class your cache manage in order to use composite cache policies.

In the end, it helps to read these things instead of arguing when someone says “yeah sure, great idea. Just one thing, make it a ICachePolicy itself.”.

This post was brought to you by “Working with goons”.

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?

Older Posts »

The Silver is the New Black Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.