Wednesday, August 23, 2006

I Get Spring

When you google up Java Spring, one of the best results is a post from crazybob called "I Don't Get Spring". For a long time, I shared a similar opinion. But now that I have used it, I get it. I only use it for defining replaceable services, so when I talk about Spring, I mean spring-core and spring-beans.

These two packages are not big, and have only very few dependencies. So it is quite easy to use Spring in any project, be it small or not. It is actually easier than using NanoContainer because Spring has less external dependencies.

I did not talk about Dependency Injection yet, because that was not my primary goal. Many times I had to write by hand configurable factories. By using Spring, I avoid writing all that boilerplate code. I was surprised that with 1 line of code I actually had my factory done. Granted, the config file is xml, an I am not necessarily an xml fan. But 1) it's very simple, and 2) if you don't want your custom factory to depend on all implementations, you will either use strings in your code or a config file, which in the end is no better than Spring config file.

Dependency Injection is the big plus that Springs gives on top of it. It's very useful to define your dependencies explicitly in the config file, since all beans instantiated from the config file are instantiated dynamically. And by defining the dependencies, you also get for free the injection.

Spring is not big and strange and unknown. It solves a fairly simple problem. But again I don't use it for everything, every class. I don't think it brings much versus straight Java code when you don't want replaceable services. And putting it in places where it is not needed would be dangerous, because of the unneeded xml configuration.

I Get Spring

When you google up Java Spring, one of the best results is a post from crazybob called "I Don't Get Spring". For a long time, I shared a similar opinion. But now that I have used it, I get it. I only use it for defining replaceable services, so when I talk about Spring, I mean spring-core and spring-beans.

These two packages are not big, and have only very few dependencies. So it is quite easy to use Spring in any project, be it small or not. It is actually easier than using NanoContainer because Spring has less external dependencies.

I did not talk about Dependency Injection yet, because that was not my primary goal. Many times I had to write by hand configurable factories. By using Spring, I avoid writing all that boilerplate code. I was surprised that with 1 line of code I actually had my factory done. Granted, the config file is xml, an I am not necessarily an xml fan. But 1) it's very simple, and 2) if you don't want your custom factory to depend on all implementations, you will either use strings in your code or a config file, which in the end is no better than Spring config file.

Dependency Injection is the big plus that Springs gives on top of it. It's very useful to define your dependencies explicitly in the config file, since all beans instantiated from the config file are instantiated dynamically. And by defining the dependencies, you also get for free the injection.

Spring is not big and strange and unknown. It solves a fairly simple problem. But again I don't use it for everything, every class. I don't think it brings much versus straight Java code when you don't want replaceable services. And putting it in places where it is not needed would be dangerous, because of the unneeded xml configuration.

Tuesday, August 22, 2006

Java Serialization vs .NET Serialization - Java Perverse?

Did you know what happens in Java when you serialize a subclass of a non serializable class? I was surprised by the answer: it works!

Unfortunately it is not a good thing, because it will serialize fields from your subclass and no fields from the parent class. So you'll end up with a half serialized instance.

In .NET, it breaks at runtime, throwing an exception, which is I think, much more logical, because then you don't end up with half data somewhere.

• Java Code:
 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Test {   public static class Toto    {     public String me;       }      public static class Toto2 extends Toto implements Serializable   {     public String you;     public String toString()     {       return me+" "+you;     }   }      public static void main(String[] args) throws Exception   {     Toto2 t = new Toto2();     t.me = "it's me";     t.you = "it's you";     System.out.println("t="+t);     ByteArrayOutputStream b = new ByteArrayOutputStream();     ObjectOutputStream oos = new ObjectOutputStream(b);     oos.writeObject(t);     ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b.toByteArray()));     System.out.println("u="+ois.readObject());   } }

will output:
t=it's me it's you
u=null it's you

• .NET Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication
{
public class Toto
{
public string me;
public override string ToString()
{

return me;
}
}

[Serializable]
public class Toto2 : Toto
{
public string you;
public override string ToString()
{

return you + " " + me;
}
}

class Program
{
static void Main(string[] args)
{

Toto2 t = new Toto2();
t.me =
"it's me";
t.you =
"it's you";
using (FileStream fs = File.Create(@"c:\test.bin"))
{

BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(fs, t);
}
Console.WriteLine("t=" + t.ToString());
Toto2u = null;
using (FileStream fs = File.Open(@"c:\test.bin", FileMode.Open))
{

BinaryFormatter bFormatter = new BinaryFormatter();
u = (Toto2)bFormatter.Deserialize(fs);
}
Console.WriteLine("u="+u.ToString());
}
}
}

will throw an exception.

Java Serialization vs .NET Serialization - Java Perverse?

Did you know what happens in Java when you serialize a subclass of a non serializable class? I was surprised by the answer: it works!

Unfortunately it is not a good thing, because it will serialize fields from your subclass and no fields from the parent class. So you'll end up with a half serialized instance.

In .NET, it breaks at runtime, throwing an exception, which is I think, much more logical, because then you don't end up with half data somewhere.

• Java Code:
 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Test {   public static class Toto    {     public String me;       }      public static class Toto2 extends Toto implements Serializable   {     public String you;     public String toString()     {       return me+" "+you;     }   }      public static void main(String[] args) throws Exception   {     Toto2 t = new Toto2();     t.me = "it's me";     t.you = "it's you";     System.out.println("t="+t);     ByteArrayOutputStream b = new ByteArrayOutputStream();     ObjectOutputStream oos = new ObjectOutputStream(b);     oos.writeObject(t);     ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(b.toByteArray()));     System.out.println("u="+ois.readObject());   } }

will output:
t=it's me it's you
u=null it's you

• .NET Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApplication
{
public class Toto
{
public string me;
public override string ToString()
{

return me;
}
}

[Serializable]
public class Toto2 : Toto
{
public string you;
public override string ToString()
{

return you + " " + me;
}
}

class Program
{
static void Main(string[] args)
{

Toto2 t = new Toto2();
t.me =
"it's me";
t.you =
"it's you";
using (FileStream fs = File.Create(@"c:\test.bin"))
{

BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.Serialize(fs, t);
}
Console.WriteLine("t=" + t.ToString());
Toto2u = null;
using (FileStream fs = File.Open(@"c:\test.bin", FileMode.Open))
{

BinaryFormatter bFormatter = new BinaryFormatter();
u = (Toto2)bFormatter.Deserialize(fs);
}
Console.WriteLine("u="+u.ToString());
}
}
}

will throw an exception.

Thursday, August 17, 2006

Unfortunately for Google, under Linux at least, I don't find Google Spreadsheets usable for anything else than storing and sharing some information, not often updated. Although I admire the engineers that managed to write the Javascript behind Google Spreasheet, it is way too slow for using it in interesting ways. Editing is slow, copy/paste is slow, sorting is slow.

I then wondered if it could read my contacts CSV from GMail (you can export contacts to a CSV file in Gmail). It could be a better interface to edit contacts. But strangely, importing Gmail CSV in Google Spreadsheets does not work.

I don't think I'll use it another 5 more minutes.

Unfortunately for Google, under Linux at least, I don't find Google Spreadsheets usable for anything else than storing and sharing some information, not often updated. Although I admire the engineers that managed to write the Javascript behind Google Spreasheet, it is way too slow for using it in interesting ways. Editing is slow, copy/paste is slow, sorting is slow.

I then wondered if it could read my contacts CSV from GMail (you can export contacts to a CSV file in Gmail). It could be a better interface to edit contacts. But strangely, importing Gmail CSV in Google Spreadsheets does not work.

I don't think I'll use it another 5 more minutes.

Tuesday, August 08, 2006

Procedural Programming in an OO language

OO is an old buzzword, that is not required anymore to get an employment. Recruiter seems to prefer SOA, Web Services, and in France, "mutualisation" and "urbanisation". Sometimes I really wonder if OO made it.

I am sure many of you are confronted with programmers that love procedural programming in many of your projects. They might use an OO language but in the end will organize everything by "type", split invariably state and logic. Everything will be so much better stateless. And we will create lookup maps to glue everything back together. In a way I feel they remove the OO of the language.

It's not necessarily bad programming. Sometimes it is encouraged by standards, after all, that's the way most Javabeans are used. It can be preferable in some places, for example you don't want to mix your XML creating code in the object that has to be transformed to XML, this way of transforming objects to XML has been gone a long time ago in Java.

But the excess of it can be quite frustrating.

Procedural Programming in an OO language

OO is an old buzzword, that is not required anymore to get an employment. Recruiter seems to prefer SOA, Web Services, and in France, "mutualisation" and "urbanisation". Sometimes I really wonder if OO made it.

I am sure many of you are confronted with programmers that love procedural programming in many of your projects. They might use an OO language but in the end will organize everything by "type", split invariably state and logic. Everything will be so much better stateless. And we will create lookup maps to glue everything back together. In a way I feel they remove the OO of the language.

It's not necessarily bad programming. Sometimes it is encouraged by standards, after all, that's the way most Javabeans are used. It can be preferable in some places, for example you don't want to mix your XML creating code in the object that has to be transformed to XML, this way of transforming objects to XML has been gone a long time ago in Java.

But the excess of it can be quite frustrating.