Things I’d love to learn more about

Below are some questions in various subjects I encounter either through day to day work or reading tech articles and blogs.

I will put the subjects in bullets and when i find the answers i will hyper-link them to another post where I write the answer I found.

  • How exceptions are implemented by compilers? How does the assembly generated by the compiler look like?
  • In public key cryptography, what is the algorithm used to raise very large numbers to very large exponents? I know it uses assembly to do bit manipulation but what is the exact algorithm.

If anyone can point me to some reference to fulfill my lust to learn about any of the subjects above, I’d appreciate it.

"Remember Me" Doesn’t work in ASP.NET

The "remember me" option in asp.net 2.0 will remember the user for as long as the Timeout period set in the forms authentication in the web.config

It’s really weird how it works as it is now. Below is an excerpt from a post found on the DotNukeForum:

How forms authentication cookies worked under asp.net 1.1

session cookies expiration = current datetime +the forms timeout value e.g. 60
persistent cookies expiration = current datetime + 50 years

 

How forms authentication cookies work under asp.net 2.0

session cookies expiration=current datetime +the forms timeout value e.g. 60
persistent cookies expiration= current datetime +the forms timeout value e.g. 60

Regex Inverse Matching

Came across a situation today were I need to inverse match on a string. In other words I want to match anything that is not xyz.

Here is how to do it using negative look-ahead:

^((?!xyz).+)$

Alternatively you can use negative look-behind:

^(.+(?<!xyz))$

On Defensive Programming

Notes from Code Complete

  • Use error-handling code for conditions you expect to occur; use assertions for conditions that should never occur
  • Use assertions to document and verify preconditions and postconditions
  • Throw exceptions at the right level of abstraction; include all information that led to the exception
  • Always have a mechanism to log application errors and a way to read that log.

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

Next Page »