mercredi 30 juillet 2008

Four harmful Java idioms, and how to fix them.

Nice discussion about OO design, and arguable conventions


In conclusion

I have argued in this article that four common Java idioms should be modified. The ultimate justification for such changes is that they will make code demonstrably easier to read, understand, and use -- and, in the process, they will exhibit more compassion for the mental experience of the reader. In the case of immutability and packaging style, they will also nudge you in the direction of improved design.

In summary, I suggest that the following idioms should be favored as the preferred style:

  • Use a naming convention to distinguish three kinds of data, not two: local variables, fields, and method arguments.
  • Prefer the package-by-feature style over package-by-layer.
  • Prefer immutable objects over JavaBeans.
  • Order items in a class in terms of decreasing scope, with private items appearing last.

mardi 29 juillet 2008

Unit testing within a container.

Problem: how can we test our code which relies on services provided by a JEE container ?

  • Write stubs which provide a fake version of these services
Drawback: writing these extra classes - as well as pluging them in - will require extra work. Moreover, the tested behaviour will not be the same as customer production code.

  • A better solution ?
Embed your unit tests in Tomcat:


vendredi 25 juillet 2008

Python URL Handling (HTTP)

Now this is another example of the expressiveness you get from a scripting language:

Get some HTTP resource using Python urllib:
 import urllib 
my_url='http://diveintomark.org/xml/atom.xml'
data = urllib.urlopen(my_url).read()
print data

Same thing in Java:

 public class HttpClient
{
public static void main(String[] args) throws IOException {
URL url = new URL("http://diveintomark.org/xml/atom.xml");
URLConnection con = url.openConnection();
con.connect();
HttpURLConnection httpConnex = (HttpURLConnection) con;
InputStream inputStream = httpConnex.getInputStream();
BufferedReader b = new BufferedReader(new InputStreamReader(inputStream));
String l="";
while ((l=b.readLine())!=null)
System.out.println(l);
}
}

Scratch - An educational programming language.

Scratch is a new programming language that makes it easy to create your own interactive stories, animations, games, music, and art -- and share your creations on the web.

http://scratch.mit.edu/

Consider it like a YouTube, but replace videos with game sequences.