December 2009 Archives

Singularly Lazy

| | Comments (0) | TrackBacks (0)

Ah, the Singleton pattern. It is used to create a single, globally accessible instance of a class, preventing any further instantiations (so only one object of the class can ever exist). When I first learnt about Singletons soon after leaving university, I thought they were wonderful and used them often. Now I think that they are often a very bad idea. Not because of security concerns, as one person once suggested to me, but because I don't want static methods/classes hanging round (increasing global state) or I later realise more than one is needed!

Once when creating a consumer website for a bank I needed access to various pieces of bank information at numerous points in the code. "There is only one bank", I reasoned and made the class containing this information a singleton. Then the bank acquired another two banks so my design was broken and refactoring had to take place right before launch. However, despite learning repeatedly from experience, I still find myself creating more. I think this is due to laziness on my part. If I'm short of time or feel that it is not particularly important, I fall back on habit. Later, while refactoring I realise a better result could have been achieved with some thought. For instance, if the global access of singletons is a problem, perhaps using an IoC container (like Spring) to wire everything up could help.

I hereby resolve not to use Singletons without thinking very hard first about whether there is a better way.

Available on iTunesU or at its website.

This is a recording of a University of California at Berkeley history course on the ancient Mediterranean given by Professor Isabelle Pafford during Fall 2007. There are 25 podcasts (each around one hour, and 15 MB) starting with a couple on Ancient Egypt and then passing through Ancient Greece, the Hellenistic Period, the Roman Republic before ending with the Roman Empire's collapse in the west around the 5th century AD. Most attention is paid to Greek and Roman history. A following course on Byzantine history is mentioned, but I couldn't find it online.

As should be expected for a series covering so much, the lectures often quickly pass over topics. Some parts of lectures were lists of important events. Despite this, there were more than enough digressions onto engaging side-topics to maintain interest (for instance, there was a passage on the nude in Greek sculpture and another on the formal political structure/progression in the Roman Republic). Thus the podcasts serve as a good introduction to the breadth of ancient Mediterranean civilisations and highlights areas you may want to explore further (after this I was looking forward to learning more on the mid and late Roman republic). I can't find much fault with the content of the course considering its scope.

I can find fault with the production standards, which overall are frustratingly poor. The sound quality is often annoyingly bad. For one lecture I had the volume turned up to max and pushed the earplugs deeper into my ears and I still could barely hear the lecturer, who sounded like she was mumbling next to a static noise generator. Course administration comments are sometimes made during a lecture - including asking unintentionally rhetorical questions to what seem to be largely apathetic students. Considering these are lecture recordings I can understand this, but usually the first few minutes of each lecture are taken up with course administration too (these could have easily been cut). Also, Professor Pafford regularly refers to visual aides that are no longer available online (the bspace website mentioned in the podcasts has been shutdown). Also the first two lectures are missing.

A good introduction to classical history, made slightly less pleasurable by poor production.

Testing generated code

| | Comments (0) | TrackBacks (0)

What is the accepted way to test generated source code? A recent work project required me to generate some Java source code from small definition files as part of a larger framework. The plan was for other project teams to take the framework and use the generator to create source which would become part of their project. I wanted to write unit tests for the source code. The problem was how to write a test for code that didn't exist at the time the test was written because it hadn't been generated yet! I came up with a few options:

  • Do a text comparison between generated code and the previously created known text output of the generation process. If they are exactly the same, the test passes. This tests the generation process if correct but doesn't test the logic of the generated code. If the generated code has syntactic errors, compilers or IDEs will pick this up quite fast and the developers in the other project will complain quickly, but subtle logic could go unchecked for ages.
  • Write a test that generates a class file from known inputs then compiles it programmactically with the Java Compiler API then loads the class into the JVM and runs tests against it. This would be better than the previous idea, as it tests the functionality of the result. This was my first choice solution. However, getting the Java Compiler API to work can be a bit hard. There was a paucity of documentation on this and in the end I couldn't get it to work within time constraints.
  • Generate the test at the same time as the generated code and pass them both to the other project. The other project would then test the generated code as if it was their code. This felt wrong to me. No other generations systems do this - it is like passing the buck. If there is an error in the generated code then the generator needs to be fixed not the generated class!
  • A 2-step process - test the functionality and generation process separately. First, create a class that should be the same as a generated class given known inputs (probably by generating it!) and write tests that work against that class. Thus the problem of testing not yet existing code is solved by using some code generated earlier. This tests the generated code's functionality as long as the generating process doesn't change. So the second part is to test the generation process hasn't changed using the first test in this list. Add another test to generate the code and check that it is exactly the same text as the class the rest of the test is written against. If the generation output changes then you have to modify the pre-generated class, and thus change the functionality test if required. Easy.

Available on iTunesU with a course website.

This complete university course (from Fall 2007) on Ancient Greek History from Yale is the best history podcast I have found so far. It consists of 24 lectures of about an hour's length each, all given by Professor Donald Kagan. It starts with an introduction to Mycenaean Greece before moving onto the Greek Dark Ages and Homeric stories. The series ends with Philip of Macedon's victory at the Battle of Chaeronea and the Greek Polis' permanent loss of independence. Along the way it details all the major events including: the rise and strangeness of Sparta; the rise of Athens and democracy; Persian wars; and the slow decrease of power in the struggle for hegemony after the Peloponnesian War. Only the 27 years of the Peloponnesian War seem to be under represented in coverage (from my previous knowledge of history). Despite 4 lectures being dedicated to it in the syllabus, most of these are taken by finishing prior topics and the buildup to the war. For the war itself, Professor Kagan suggests looking to other sources as it it too much to cover properly, and instead talks in detail about two related topics - Pericles' strategy and Thucydides' history.

One thing I can't fault is the production. The podcasts are professional from start to finish. The only blemish is Professor Kagan's occasional coughing, but this is a minor issue. The podcasts are available in a number of formats on the course website: audio-only (the same mp3s that are on iTunes); video, in both high (around 700 MB) & medium (around 250 MB) bandwith forms; and, complete text transcripts. The website also includes the notes handed out with each lecture, although these unfortunately are without the original images due to copyright problems. The website itself is downloadable too (albeit with only links to the audio and video files). While videos of the lectures are available, it is not really necessary to download them as they mainly just show Kagan talking at his podium. Rarely does he use any visual aid during the lectures. Thus these are excellent podcasts to listen to audio-only - very little will be missed.

The lectures are often a recitation of events together with discussion of how and why things may have occurred that way (for instance a section on various military strategies and whether they were correct). The aim appears to be getting the listener into the mindset of the ancient Greek statesmen (and thus the citystates in general). A goal largely achieved to my mind. There is less discourse on daily life (but still some). Professor Kagan also talks a little about historical controversies and often compares ancient events to modern history (in particular around the use of military force). In both situations he tends to declare his own prejudices so the listener is not led too much.

This is a highly recommended lecture series - interesting content that goes into the subject with some depth and so well produced it is surprisingly easy listening.