Bug Vanquisher

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”.

Create a free website or blog at WordPress.com.