Archive for the 'C#' Category

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.

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.

Regex for U.S and Canadian Zip Codes

for my future references, below is a regular expression for validating U.S and Canadian zip codes.

^\d{5}(?:-\d{4})?$|^[a-zA-Z]\d[a-zA-Z]\s?\d[a-zA-Z]\d$

more details on the format of Canadian zip codes

C# Numeric Literals Suffixes

The suffixes in the table below are useful because they tell the compiler what type a numeric literal is and how it should be treated. The default type of a numeric literal is integer (int) of course, but if you want to specify decimal or long, how would you do that? (please don’t cast it, it’s ugly)

Consider having an overloaded method, one overload takes a parameter of type long, another of type int, now if you want to pass in the number ‘15′ for example, these suffixes will help you choose which overload to actually invoke by appending a letter (or two) to the numeric literal.

Type   Suffix   Example
  uint   U or u   100U
  long   L or l   100L
  ulong   UL or ul   100UL
  float   F or f   123.45F
  decimal   M or m   123.45M

Note that the suffixes are case-insensitive.