Archive for the 'Code' Category

Post data lost on 301 Moved Permanently

Friday, May 15th, 2009

What the hell with 301 Moved Permanently HTTP header !?

I’ll take an exemple to explain my ugly problem, take, a [very ugly cause it's PHP] index.php, it could be retrieved by using :

http://example.com/directory/index.php

-> Got the page, 200 OK
or http://example.com/directory/
-> Got the page, 200 OK
or http://example.com/directory
-> Got a 301 Moved Permanently Location: http://example.com/directory/

Just like expected … but…

As you can see, on the captured HTTP headers below, when you POST data on a 301 target, you’ll be redirected, but unfortunately you’ll lost your POST data, even worse, your request can be reforged as a GET request !

(more…)

Integrating google bookmarks in google chrome

Thursday, April 16th, 2009

As every developer, you have 42 computers, 8 browsers, and spent a lot of time asking why google chrome does not integrate google bookmarks ?
(more…)

C# Using alias directives

Wednesday, March 25th, 2009

Just found in section 9.4.1 of the C# language specification :
The using keyword can be used to alias a namespace or a type name :

using-alias-directive:
using identifier = namespace-or-type-name ;

(more…)

Covariance and Contravariance in C#3

Monday, March 9th, 2009

A short introduction to Covariance and Contravariance in C# 3 preparing you to an article about that in C# 4.
So what is covariance ?
Covariance is basically using a method which returns something derived from the expected type.

An exemple ? It’s safe to have a method returning a cat when you expect it to return an animal.
(more…)

Silverlight Tutorial – Flashlight Tutorial

Monday, January 19th, 2009

How to make a flashlight effect with silverlight ?
 
 

 
 
(more…)

Silverlight Custom Control – II : Properties

Thursday, January 15th, 2009

This is the second part of my serie about creating Custom Controls, if you missed the first, you should read it : Part I

In this Part, we’ll speak about Custom Control’s properties : How to databind a template property to an exposed property, and how to create new exposed properties.

(more…)

Silverlight Custom Control – I

Wednesday, January 14th, 2009

This article is my first about creating Custom Controls in Silverlight 2.
So let’s build a simple one :
You need :

  • A .cs file, named with your new Custom Control name
  • A directory named Themes
  • A generic.xaml file in it (just delete generic.xaml.cs if it was created)

(more…)

Combinatory logic from scratch

Friday, November 28th, 2008

Cause it’s sooooo sexy, let’s speak about Combinatory Logic !
Rule 1 : You don’t talk about Combinatory Logic
Rule 2 : You don’t talk about Combinatory Logic
Rule 3 : Combinatory Logic is based on Lambda Calculus (see Wikipedia for both)
Rule 4 : A combinator is a Lambda expression taking One and only One combinator as parameter, and returning a Combinator.

(more…)

The ?? operator aka the Null Coalescing Operator

Sunday, November 23rd, 2008

If are familiar to the use of ternary operators, you must have encountered several situations like this one :

string pageTitle = getTitle() ? getTitle() : "Default Title";

Download this code: bad_situation.cs

You would want to call getTitle() only once, but then you wouldn’t have a simple one-lined initialization of you variable.

Actually there is a simple solution that most langages implements, the null coalescing operator; let’s replace the above C# code with this one :

string pageTitle = getTitle() ?? "Default Title";

Download this code: solution.cs

Now you have the same behaviour, but with only one call to your function. The syntax is simple :

possibly_null_value ?? value_if_null

the “??” operator is implemented in C# since C# 2.0.

You also have it in C and C++ as a GNU extension using the “?:” operator :

string pageTitle = getTitle() ?: "Default Title";

Download this code: solution.cpp

You can get the same behaviour in javascript using the “||” operator :

pageTitle = getTitle() || "Default Title";

Download this code: solution.js

You can read more about this operator here and here

Lazy Loading in php with spl_autload

Sunday, November 16th, 2008

Today, a very short post about the lazy loading in PHP : spl_autoload
I’ll not expose everything about it here, cause it’s already done here :
http://php.net/autoload

Basically, a Lazy Loading allow you to predifine some paths where PHP should seek for classes to include, this allow you to directly instantiate an object without having included its file.

public static function lazyLoad($strClassName)
{
        /* some work to find the file to include mixing strClassName */

        /* include your files here... */
}
spl_autoload_register("lazyLoad");

Download this code: lazy.php