Getting Around Dynamic Casting To Achieve Better Design And Performance

Using dynamic casts to determine an execution path in your code is not the best to write your logic.

I realized this fact while looking at Google’s style guide for C++

Do not use RTTI, except in unit-tests. If you find yourself in need of writing code that behaves differently based on the class of an object, consider one of the alternatives to querying the type.

Virtual methods are the preferred way of executing different code paths depending on a specific subclass type. This puts the work within the object itself.

If the work belongs outside the object and instead in some processing code, consider a double-dispatch solution, such as the Visitor design pattern. This allows a facility outside the object itself to determine the type of class using the built-in type system.

If you think you truly cannot use those ideas, you may use RTTI. But think twice about it. :-) Then think twice again. Do not hand-implement an RTTI-like workaround. The arguments against RTTI apply just as much to workarounds like class hierarchies with type tags.

That was an eye-opener to me. I didn’t know what double-dispatch is all about. Turns out that it’s the heart of the Visitor’s Pattern (although the example at dofactory.com is not ideal because they do not provide overloads for the visit() function and they cast all types to the base).

 

Double-Dispatch is the ability to dynamically select a method according to the run-time type of the caller (single dispatch) and the run-time type of the argument as well.

 

To close out, remember before you use dynamic casting in your code to think if there is a better way to achieve the same results using function overloads, virtual functions or double dispatch.

Parsing SQL Statements With Regex

I was trying to parse a SQL script the other day with regular expressions to extract some values of some columns. The script looked something like this:

NSERT INTO wp_posts (ID, post_author, post_date, post_content, post_title) VALUES (5, 1, ‘2008-02-03 20:06:31′, ‘marco’’s long text’, ‘hello world’);

so I had a regular expression that would capture anything between two apostrophes

‘(?<text>[^’]+)’

The problem is escaped apostrophes in the text itself. Like in the example above marco’’s long text it would stop at the first apostrophe.

So, in short,  to get around that, I did a simple ORing like this:

‘(?<text>([^’]|[’]{2})+)’

Seems very simple, but it took me a while to see the light.

My Favorite Geek Quotes

I think any of those would be cool to have on a T-shirt.

  • I would love to change the world, but they won’t give me the source code
  • There are 10 types of people, those who understand binary, and those who don’t
  • There is no place like 127.0.0.1
  • Oh, please, Give me a <br/>
  • I’m not married, I’m only loosely coupled
  • To understand recursion, you must first understand recursion
  • Girls are like Internet domain names, the ones I like are already taken
  • How many people can read hex if only you and dead people can read hex?

jscalendar shows at the top of the screen in IE7

jscalendar v1.0 has a bug that makes the calendar displays at the top of the screen in IE7.

Here is a patch that fixes it.

reference: http://drupal.org/node/118926

Platform-Independent Bitwise Rotation function

A simple macro to rotate the bits of an unsigned 32-bit value. using shift-left and shift-right we can achieve rotate-left and rotate-right.

#define rotlFixed(x,n) (((x) << (n)) | ((x) >> (32 - (n))))

#define rotrFixed(x,n) (((x) >> (n)) | ((x) << (32 - (n))))

The macro above can be trivially converted to a function and used in C# for example.

JavaScript font detector

I found this little JavaScript utility that tests for the existence of a specific font on the client machine.
I thought it would be very handy to use for my church’s website (coming soon) which will probably have some content written in the Coptic language.

Most probably we will be using Athanasuis font.

Anyways, I want to save the script here in case the original site goes down or something.

The script is released under Apache License, Version 2.0

You Suck At Photoshop

I stumbled on this series of youtube episodes at digg.com and I think they are amazing.

if you have a few minutes check them out. each episode is like 5 minutes long.

Here is episode #2 but definitely check out the other episodes as well in the related videos section.

Shuffling Analysis

This post is in regard to Jeff Atwood’s post on Shuffling and The Danger of Naïveté.

In his second post he used an example of shuffling three-card deck 600,000 times to explain how naive solutions can be dangerous. The post is extremely well written. I enjoyed every bit of it. However I had two questions that I couldn’t find an answer for in Jeff’s post. Why exactly 3 permutations (out of the 6 possible ones) are biased? and why those 3 specifically are biased and not any other permutations?

I spent sometime thinking about it and using a pencil and a paper I was able to get some answers.
First here is the naive method: (I reversed the loop to be as similar as possible to the correct method)

for (int i = cards.Length - 1; i >= 0; i--)
{
    int n = random.Next(cards.Length);
    Swap(ref cards[i], ref cards[n]);
}

And here is the correct one (Knuth Shuffle)

for (int i = cards.Length - 1; i > 0; i--)
{
    int n = rand.Next(i + 1);
    Swap(ref cards[i], ref cards[n]);
}

Now let’s agree on some facts real quick. For a three-card deck:

  1. There are exactly 6 unique possible permutations for that deck (3!)
  2. Using the Naive shuffling method, there are 27 possible sequences of random-numbers on any given run (33)

The answer to the first question above, is that 27 mod 6 is equal to 3. What that means is there are 3 extra sequences of random numbers that have to map to 1 (or 2 or 3) of the 6 unique permutations of the deck.

To answer the second question we have to note something first about using swapping to perform a shuffle, take a look at the state of the deck at each iteration given the following random numbers:

Step Random Number Deck
0 N/A 123
1 2 132
2 3 123
3 3 321
Step Random Number Deck
0 N/A 123
1 1 321
2 1 231
3 2 321

Notice how the two different sequences of random-numbers resulted in the same permutation of the deck. Now remember there are 27 possible sequences and only 6 unique permutations. if we divide 27/6 we get 4 (with remainder 3) which means there are at least 4 paths (or sequences) for every unique permutation of the deck, the 3 extra possible sequence lead to 3 of the unique permutations giving them an advantage over the other 3.

so basically we have 6 permutations of the deck, 3 of them have 4 random-number sequences that lead to them, and the other 3 have 5 sequences that lead to them.

In this particular example if you follow the algorithm above and try the following random-number sequences you will get the same deck permutation for all 5.

  • 1,1,2
  • 3,1,3
  • 2,2,2
  • 1,3,1
  • 2,3,3

Conclusion

The problem with the Naive shuffling method is swapping! The fact that swapping elements in 2 (or more) different sequences can lead to the same deck permutation is a subtle problem.

The Knuth shuffle algorithm avoids that problem by ensuring N! possible random sequences where N is the number of elements in a deck (or any container).

Function Pointers

Declaring function pointers in C\C++ has a somewhat strange syntax, specially when you want to specify a function pointer as the return type of another function.
so here is a brief explanation:

To declare a pointer to an int called pNumber:

int *pNumber;

To declare a pointer to a function called pFunc (that has a string parameter and returns an int):

int (* pFunc)(string);

in red is the variable name, in purple is the type.

To declare a function that returns a pointer to a function:

int (* GetFuncPointer(void) )(string);

in red is a normal function declaration, in purple is the return type of that function.
S o basically int(*)(string) is the type of the function pointer.

If we want to declare a function that takes a function pointer as a parameter then we would write something like this:

void RegisterCallBack( int(*)(string) ); /* unnamed parameter */

or

void RegisterCallBack( int(*myCallback)(string) ); /* named parameter */

A prettier way to use function pointers is to use typedef to declare an alias. The typedef syntax is exactly the same as declaring a regular variable (or a function pointer).

typedef int(*FunctionPointer)(string);

now we can use FunctionPointer like this:

FunctionPointer GetFuncPointer(void);

Click For More Information…

Mr. Compiler, would you please inline this function?

I’ve always read that marking a function inline doesn’t guarantee that the compiler will actually inline it, but it’s merely a hint to the compiler to “try its best” to do it. Which have always raised the question in my head, how do I know if Mr. Compiler agreed to inline my function? Do I have to look at the generated assembly code? There has to be an easier way!

So I invested 15 minutes researching that issue and found out that Mr. Compiler is friendly enough to let you know if it was not possible to fulfill your request to inline a certain function by issuing a warning.

For Microsoft’s VC8 compiler warning C4710 is turned off by default, you can turn it on by doing any of the following (depends on your choice):

  1. #pragma warning (default : 4710)
  2. #pragma warning (custom_warning_level : 4710)

For GCC the -Winline switch can be used to achieve the same thing.

Next Page »