Lazy loading with asychronous calls – A design pattern proposal

When using regular OOP, it is often a good idea to retrieve the data as last as possible, only if and when it is needed. This what is called lazy loading.

class MyClass
{
    private ABigObject _myVar;
    public ABigObject MyVar
    {
        get
        {
            if(this._myVar == null)
            {
                // Do the needed heavy stuff to retrieve the data here
            }
            return this._myVar;
        }
    }
}

One good thing with Ajax is that it can retrieve data on demand. The not-so-good thing is that the http connection comes at a cost.
So, being lazy in loading data is quite a good idea.

The problem is that, because the http connection can take some time, we do not want to make synchronous calls.

So the following code won’t work:

var myObj = {
    myVar:null,
    getMyVar:function(){
    	var that = this;
 
        if(this._myVar == null){
            $.post(
                '/get_my_var.php',
                { },
                function(data){
                    that.myVar = data;
                },
                'json'
            );
        }
        return this.myVar;
    }
 
}

The JQuery $.post method is asynchronous. It does not wait to be completed to pass the hand to the next statement.
So when the “return” is executed, there is a high chance that the ajax call is not completed and that this.myVar contains null.

The first very naïve option would be to try to force the ajax call to behave as if it was synchronous. Putting a long-enough pause before the return should be enough to be get greater chances the variable is set before returning it.
This is, of course a very bad idea.

  • You cannot be sure the return will always be called once the data is retrieved. Maybe the user is using a 56K line shared with 100 other people
  • To get bigger chances to get the data, you need to overestimate the pause delay, meaning loosing a lot of time
  • Your users do not want to wait!

To sum up, there is a good reason why the Ajax calls are made asychronously.

Conclusion: We just cannot copy/paste our design pattern from the above C# example.

It is very probably not the ultimate solution, but what I came up with is something like this:

var myObj = {
	myVar:null,
	doWithMyVar:function(callback){
		callback = callback || function(){	};
 
		$.post(
			'/get_my_var.php',
			{	},
			function(data){
				that.myVar = data;
				callback(that.myVar);
			},
			'json'
		);
	}
};
 
// To echo my var:
myObj.doWithMyVar(function(var){ alert(var);	});

Basically, we are turning things upside down. Instead of calling a function to retrieve the data and then process it, we send to the function that retrieves the data the code to be executed with it.

More elegant solutions are more than welcome!

Where an interpreted language can be useful – Apply a function to backward references with preg_replace

Eveything goes with PHP sauce
We all know the preg_replace function. I have been using it for years and never noticed its behaviour when it is associated with the “e” modifier.

Just take a look at this piece of code:

echo preg_replace(
  '/{(.*?)}/e',
  'strtoupper("$1")',
  '{did you know} you {could} apply {a} function to {a replacement} using preg_replace?'
);

The output is “DID YOU KNOW you COULD apply A function to A REPLACEMENT using preg_replace?”.
We chose strtoupper in the example but any expression would fit.

Note, and this is important, that the function is included in the replacement string.

The trick here is the use of the “e” modifier. The behaviour of preg_replace is then modified to not use the replacement string given to preg_replace but the result of its treatment by eval().

So, beware, the second parameter still must be a valid string and you will need to properly escape it.

jQuery tags plugin

from http://dragonartz.wordpress.com/tag/flowers/
I was looking for a little plugin to handle the tags input in a nice way, like for instance tumblr does and could not find any. It might very well be because I did search hard enough but decided to code a little to handle it.

Here you can find a demo of the result.

The usage is pretty straight-forward:

$('.tags').tags(options);

With options :

{
    separator:',',
    add:function( added_tag_txt, tags ){ },
    remove: function( removed_tag_txt, tags ){ }
}

All the parameters are optional.

The sources can be found on github.

Overriding static members: Nonsense?

Am I really the only one to be tempted to sometimes write absurd thigs like this?!

abstract class MyAbstractClass
{
	public static abstract string MyString	{ get; }
}

Of course it is absurd. At least in .Net/Mono, Java, and I guess most if not all compiled OO languages.

For those who would consider the code above legal: A static member is attached to the class where it is defined. Overriding it does not make any sense.

Actually, you need in any case to explicitly tell which class the member belongs to in order to be able to call it.

class Class1
{
	public static void MyMethod()
	{	Console.WriteLine("I am Class1.MyMethod");	}
}
 
class Class2:Class1{ }
 
class Class3:Class2
{
	public static new void MyMethod()
	{	Console.WriteLine("I am Class3.MyMethod");	}
}
 
class Main
{
	public static void Main(string[] args)
	{
		Class1.MyMethod();	// prints "I am Class1.MyMethod"
		Class2.MyMethod();	// prints "I am Class1.MyMethod"
		Class3.MyMethod();	// prints "I am Class3.MyMethod"
 
	}
}

The term “static” was not made randomly. It was chosen to tell there cannot be any confusion for the compiler in choosing the proper definition to use for the member.
Contrarily, the definition to use for dynamic members depends on the context and usually can be evaluated during runtime only.

For instance:

class Class1
{
	public void MyMethod()
	{	Console.WriteLine("I am Class1.MyMethod");	}
}
 
class Class2:Class1{ }
 
class Class3:Class2
{
	public new void MyMethod()
	{	Console.WriteLine("I am Class3.MyMethod");	}
}
 
class Main
{
	public static void Main(string[] args)
	{
		this.Test(new Class1());
		this.Test(new Class2());
		this.Test(new Class3());
	}
 
	public void Test(Class1 obj)
	{	obj.MyMethod();	}
 
}

Le method obj.MyMethod(); will depend on the type of obj.
The compiler cannot know which method will be called and the link will be made during runtime.

So, if the code at the beginning of this post is so absurd, why try to write it?

It seems to me that it is mostly because static can handle two definitions, between which there can be some kind of friction.
- On one hand, the concept of being attached to the class by opposition to the instance.
- On the other hand, the disambiguation of types for the compiler.

The first one is mostly conceptual:
_ Insects have 6 legs
_ This ant is moving a leaf

As all insects have 6 legs, it can look logical to define this value at class level, using a static member.

class Insect
{
	public static int NbLegs
	{
		get:{ return 6; }
	}
}

After all, defining a value for the instance would conceptually imply that the value could change from one instance to another, no?

Now, let’s imagine we want to create an interface to define all animals for which we know the number of legs per family.

interface AnimalsWeKnowTheNumberOfLegs{...}

The fact that the number of legs will be known for all instances that will implement this interface must be stated.

interface AnimalsWeKnowTheNumberOfLegs
{..
	int NbLegs{	get;	}
..}

But, if we find more logical that the value is attached to the class rather than to its instances?

interface AnimalsWeKnowTheNumberOfLegs
{..
	static int NbLegs{	get;	}
..}

File=AnimalsWeKnowTheNumberOfLegs.cs, Line=10, Column=24, Type=Error, Priority=Normal, Description=The modifier `static’ is not valid for this item(CS0106)

ARghle…

But… Wait a moment.
What are we trying to express there?

We would like to express that the number of legs of all instances of the classes that implement AnimalsWeKnowTheNumberOfLegs is a value that is defined for their class.
It is a bit tricky, I know…

By translating this proposal in using a static member, we are making a mistake:

A class property != A property shared amongst all instances of the class!!

The insect class does not have 6 legs! The insect class has no leg at all! It is a class, not an animal for god’ sake!
Only insects have 6 legs.

Yes, but then how do we express that the property is shared amongs all instances of the class?

- Wel, in .Net/Mono, we have the const keyword.

But we still do not have any way to force a child class to define a const.
In other words, it is not allowed to specify a const inside an interface, as an abstract member or even as virtual.

- In Java, you cannot. To my knowledge, final is the equivalent of readonly and not of const.

- In prototype-based languages, you just have to set the property on the prototype.
Ex en Javascript:

var o = {
};
 
var F = function(){};
F.prototype = o;
another_o = new F(); // o and another_o share the same prototype
 
o.prototype['my_shared_value'] = 25;
 
alert(o.my_share_value);
alert(another_o.my_shared_value);

- PHP also offers a const keyword. But the variables defined that way must be called like static members.

<?php
class MyClass
{
	const MyConstant = 'A constant';
}
 
echo MyClass::MyConstant;
$a = new MyClass();
echo $a->MyConstant;	// ERROR!
?>

Note that PHP also allows to write “static abstract function test()”.

No doubt my original temptation is coming from there.

- In python and other python-like languages, the situation is again a little confusing. The self keyword is used to call indifferently static or not members.
The following Boo code is perfectly valid:

class MyClass():
	_string = "A string"
 
	public def constructor():
		self.StaticMethod()
		Console.WriteLine(self._string)
 
	public static def StaticMethod():
		pass

As a conclusion, I do not see any real solution to enforce in an interface or an abstract class a property for which the value would be shared amongst all the instances of the class.
Of course, we can still code but the programmer is responsible for ensuring the immutability of a value for one class.

To my point of view, class-based OO languages do miss a level of abstraction to define this.
And this level of abstraction is exactly what the prototype concept in prototype-based language handles.

Is there a chance I ever can write things like this?:

class Insect(Animal):
	prototype:
		public NbLegs = 6;
 
	public def constructor():
		pass

How to emulate private vars in Javascript

As you know, the object members of an object are all public in Javascript.
There is no mechanism to declare a protected or private variable.

In some situations, using private variables would be needed though, like if for instance you are building a library that is meant to be used by other people.
In his excellent short book Javascript, the Good parts, Douglas Crockford, the father of JSON, explains how to do it:

var o = function(){
    var _my_private_var = "I am private";
    return {
        test: function(){
            alert("I am a plublic method, having access to a private var: " + _my_private_var);
        }
    }
}();
 
o.test(); // will display the message

The trick here is to use a function to define the object o.

In Javascript, all functions are closures, meaning that they embed a reference to the context where they were created.
The second point to know is that, contrary to C, where the scope is defined per block, the scope in javascript is defined per function. If you define a child function inside a parent function, the child will have access to the variables that were declared in the parent.

Finally, in order to be sure not to be able to get a way to access directly our private variable, we use an anonymous function, that we immediately call (note the “}();” at the end) to return the object.

So, what is happening here is that when we call the “test” method, it has still access to the context where it was defined (closure property). In this case, the context is our anonymous function.
As _my_private_var is defined inthere, it is visible from test().

On the other hand, we did not store a reference to the anonymous function. o only contains a reference to the returned object. So there is no way to access _my_private_var from the outside anymore.
Note that as functions are also objects in Javascript, we could access _my_private_var if we had kept a reference to the object generation function.

var func = function(){
    var _my_private_var = "I am not that private";
};
alert(func._my_private_var); // Will display "Im am not that private"

Block delimiters – a syntaxic evolution

Recently, I have been playing with Boo and the Mono framework. Even if it suffers a lot from its lack of auto-completion in Monodevelop 2, the python-like syntax is really nice to use. I am really looking forward the new Monodevelop plugin version to be ready and (why not), a VS 2008 plugin version for it.

I realised that what I like the most about the python-like languages is not their instrinsic capabilities. Let’s face it, you do not code the same playing with Boo or IronPython than you would do with python. Having a framework (.Net or Mono) as a basement does change a lot of things and in many case the whole way of approaching a problem.
Of course, the language possibilities do matter. And it matters a lot. I am just saying that this is not the main reason why I like python-like languages.

Anyway, if what the language does is not the big point, what is it? I just realized that what I really liked with it was that we were finally freed from block delimiters.
That, and the fact that the compiler/interpreter won’t play dumb and will finally be able to guess what type I am using.
Why the hell would I have to type

T var = new T();

Can’t the compiler know from the right side of the equal side that I want a T instance? I know, C# 3 does, but it took ages to come if you ask me.

But the nicest thing ever is definitely not to have to type any curly brace while coding.
If, like me, you are using an AZERTY keyboard, you need to press AltRight + 4 and AltrRight + “+” to make them.

For sure, the guy who designed the AZERTY keyboard was not a C coder…

Of course, by the time, you learn to twist your thumb and extend your ring finger to reach it without thinking about it at all, but this is a real additional pain for the programming newbies.

But this was even worse before. I remember a time where we had to type entire words(!) to delimit programming blocks!.

I came to computer programming in the late 80s. At that time, I was experimenting my first pains with GFA basic on Atari ST. The same mostly applies to the VB6 coders community.
In Basic, once it became mature enough to integrate decent flow control instructions and not only GOTO and GOSUB, some new words were introduced to delimit these blocks of instructions.
For each loop or conditional block, we would then get a starting delimiter and a closing one.

In order not to confuse the BASIC programmers, it was decided to define specific per block delimiter.

If IWantAnIf Then
	'... DoSomeStuff
EndIf
 
Do
	'... Whatever
Until done
 
Do
	'.. Loop
Loop
 
For i = 0 To 1
	'...
Next

Etc.

Note that this syntax is still in use in VB.Net and VBA for instance.

After BASIC, the first other language I learned was Pascal, at school.
Pascal introduced to me the concept of universal block delimiter syntax. Once the type of the block and its parameters were expressed, the code to run was always enlosed by a begin and and end:

program WHILE_DEMO (output);
	const   PI = 3.14;
	var     XL, Frequency, Inductance : real
	begin
	        Inductance := 1.0;
	        Frequency  := 100.00;
	        while  Frequency &lt; 1000.00 do
	        begin
	            XL := 2 * PI * Frequency * Inductance;
	            writeln('XL at ',Frequency:4:0,' hertz = ', XL:8:2 );
	            Frequency := Frequency + 100.00
	        end
	 end

That was already a little simpler but still, we had to type 8 chars just to say to the compiler where the block starts and where it ends.
We still encounter this kid of syntax from time to time, for instance in PL/SQL and T-SQL.

Then I was introduced to C…
At that time, switching from beginend to { } was a big relief to me. It then became the obious way to do it. Almost all that very popular languages I was seeing from there: C++, Java, Javascript, PHP, Perl, C# etc were using the same curly braces to delimit blocks.

Soon, I started believing the curly braces were an universal paradigm.

Till I saw some python and ruby code… First I could not accept it. My first impression was that it was “dirty” and probably error prone. I had been using curly braces with every language I was using for more than 10 years and there had to be a good reason for this. This was the way to do it. Period.

Except that I had to change my mind. Looking at some cool applications developped upon the Django framework, or seeing that Google was internally using python, I took a closer look. And it rocks.

The thing is that there is something true: At the end, coding is just typing. And the more you have to type, the less you can think to your code.
The simpler the syntax is, the more you can focus on what the code should do and not on how to write it. It also makes it more readable and then easier to debug.

Just compare the two following bits of code.
C#

class MyClass
{
	private string _name;
 
	public string Name
	{
		get	{	return this._name;	}
		set	{	this._name = value;	}
	}
}

Now, in BOO

class MyClass:
	[Property(Name)]
	_name as string

They both do the exact same thing.
Which one is quicker to write?
Which one is easier to read?

But even if there is quite a lot of fuzz around ruby and python for quite a long time now, I do not believe the languages based on this kind of syntax have reached their maturity state yet.
The first symptom is the poverty of good IDE integration.

For Python:

  • BOO lost its auto-completion in MonoDevelop with the 2.0 version.
  • PyDev for Eclipse mostly only supports native methods completion and still has a lot of progress to perform. It is very far from CDT, PDT or JDT integration.*
  • Cobra is only on the state of syntax highlighting, not of integrating an IDE.
  • Only IronPython is quite well integrated into Visual Studio but then, you need to stick on Windows.

I am far being an expert in Ruby but my understanding is that it is a bit the opposite (quite good integration in Eclipse, very early state of development for IronRuby).

Second, especially regarding python, there are still too many dialects and no one is totally beaten yet. Native python takes the lead due to its age but the community is complaining about performance issues. The lack of portability is also a problem in many cases.
IronPython is quite mature on .Net but is not officially supported in the Mono ecosystem. BOO is nice and offers additional features such as the duck typing but is still very young.

Interestingly, we have a bit the same evolution with data exchange format specifications for the web.

Since the early 90s, the king is XML. It lead to an almost infinite number of specifications, formats or standards that we use or should use every day (XHTML, SOAP & WSDL, XSL(T), XSD, RDF, OWL, XAML, XUL, SVG, OpenDocument and all other Oasis standards…)
XML was so strongly accepted that some could not even imagine using something else to exchange data.

I was watching a conference by Douglas Crockford, the father of JSON. The fact that some of customers we refusing to use JSON because “they had a policy to stick to XML” struck me.

If you look at some XML messaging, it is bloaty, giving more space to meta information than to information. All block delimiters are specific per block and use words.
In a way, XML is to data exchange formats what BASIC is to programming languages.

JSon uses curly braces. It is by far easier to read and often leads to much smaller messages, which can quickly be very important on a network.

And finally we have YAML, as the python|ruby for data.

And here again, the last two ones appear to be quite immature compared to XML. I have to say there are good reasons to this.

All decent programming platform do offer at least one, often more, XML parsing library. This is becoming true for JSON, not for YAML yet.
Only XML offers the ability to be validated against a schema or a DTD.
And of course, I believe the install base of XML-dependent application is by far larger than any other format.

And you, are you ready for indent-base syntax?

*[EDIT:] I just reinstalled Pydev and I must say it has made a lot of progress. It is now able to detect what import is needed or not, and to drill through your imports to give you some completion on your custom types.

A very lightweight PHP HTTP client

Si vous cherchez un client HTTP léger en PHP5 et que vous trouvez les solutions de Snoopy, Zend ou Pear un peu trop lourdes, voici peut-être ce que vous cherchez.

J’ai écrit une petite classe pour simuler un browser et retourner le source de la page visitée.

Il vous faudra cUrl installé et activé pour PHP.

La classe expose deux méthodes publiques: get et post. Les fonctionalités sont très limitées. Pas de moyen de traquer les headers, pas de gestion d’erreur non plus.

Exemple:

$c = new WebClient();
var_dump($c->get('http://www.google.com'));
var_dump($c->post('http://www.myform.com', 'field1=value1&field2=value2');
 
$c->close();

Si ca peut servir…

How to get last autoincremented id with SQLite in Mono

It is a bit strange to me that every database has its own way to get the last inserted (@@IDENTITY) value.
It is actually even stranger to me that the ADO.Net connector packages do not unify this but I guess there are good reasons for this.

Anyway, if you are wondering on how to to get the just inserted autoincrement value after your insert using the Mono.Data.SqliteClient extensions, here is the way to go.
The Mono.Data.SqliteConnection class includes the LastInsertRowId property, which contains the desired value.

Which will give something like (in BOO):

import Mono.Data.SqliteClient
 
cnx = SqliteConnection("URI=file:sqliteDb.db")
cmd = SqliteCommand("INSERT INTO my_table (field) VALUES ('value')", cnx)
 
cnx.Open()
cmd.ExecuteNonQuery()
inserted_id = cnx.LastInsertRowId
cnx.Close()