dimanche 30 août 2009

Backup with rsync and cygwin on Windows

I recently had to setup an automatic backup scheme for a desktop Windows PC used for office tasks. I usually try to avoid complicated backup software that tend to store the backups in a proprietary format. I want to be able to browse the files in the backup, and copy one file to recover the content.

On UNIX/Linux I use rsync and thanks to Cygwin I was able to do the same thing on Windows. The goal for the script was :
  • Must backup two directories into one directory located on an USB key drive
  • Must not delete any file in the backup
  • Must always keep the old versions of any updated file
Here is a bash script that backups two directories foo1 and foo2

  • links actually is a symlink to the directory that contains foo1 and foo2. I created this symlink with the ln -s command as the path to the parent directory of foo1 and foo2 had spaces and I didn't want to deal with path escaping issues.
  • --modify-window=2 is necessary on windows, else the whole set of file is saved all the time.
  • --delete can be added to the rsync command if we want the deleted files to be removed from the backup
Then a .bat file is created in order to run the script with a double click :

mardi 18 août 2009

Test Driven Wall Tiling

Last week I was tiling a kitchen backsplash and countertop and as I was using the usual tile alignment tricks, I was struck by the parallel with the Test-driven development techniques for application development.

This picture shows the wooden cleat (the dark brown area on the picture) that was temporarily screwed to the wall to obtain a perfect alignment for the second row of tiles above the countertop.

It made me think of TDD because :
  • The screwed wooden cleat is a temporary built artifact that has nothing to do with what we want to construct (that's to say, a tiled backsplash), but we built it nonetheless, and with care, as it's going to direct the alignment of the tile. That's what we do with unit test, they are a programs designed to unit test other programs.
  • The wooden cleat is attached to the wall using the same tools and technique that are used to build the main "noble" artifacts (wood, screw, you name it). A unit test is build with the same skills than the tested program.
  • When the cleat is fixed we do not have to think anymore about the tile alignment. The alignment is forced by the wooden rule, we can forget about it and concentrate on the quality of the tiling. The unit test creates design or behavior constraints on the code that is written. You can then forget about these constraints (you'll get a red test anyway if you don't respect these constraints)
  • The wooden cleat is set at the beginning of the tiling for the row, you do not verify the alignment at the end. Test first
  • Nobody told you to screw a wooden cleat on the wall. Its an act of craftsmanship. We do it because we think that it will improve the quality of the tiling. You have to write unit tests, even when nobody tells you to do so.
  • Sometimes, it is not perfectly clear where the tiling must start, so you just set the tiles on the countertop without adhesive, and then you know where the wooden cleat must be fixed. Can be seen as exploratory testing.
  • I need the second row to be perfectly aligned since the first row above the contertop can not be aligned on the countertop itself (tiles thickness may vary). When parts of a system have good unit tests, the integration of these parts is easier.
So why do we still see large software systems that are written without any sort of unit tests whereas this seems to be a widespread technique for building physical artifacts (tiling, walls, etc...) ?

I think it is because programming is like tiling without adhesive. You may always be able to move the tiles. But even though we may try to align all the tiles at the end of the project, we would not completely succeed, and the quality of the alignment would suffer.

We should rather use all the available engineering techniques during the life of the project in order to be sure that the alignment is perfect, all the time. From start to end.

mercredi 13 mai 2009

OPML import for Rhythmbox, kind of

Update : it seems that Rhythmbox now has support for OPML import, you may type the path to the OPML import in the new feed dialog box. I didn't try it, though

I've just installed the brand new Ubuntu 9.04 to replace my good old 6.06, and now I must find a way to import all my podcast feeds exported in opml format from Juice, to Rhythmbox, the official Ubuntu music manager.

However, it seems that Rhythmbox does not manage opml imports. I don't feel like copying manually 70 rss feeds...

An opportunity to learn the XML library of Groovy !

Save this groovy script as opml2rhythmbox.groovy

Then if it is not already done, install Groovy

Execute the script on your exported opml file

Now, you must manually copy the content of the generated generated.xml file, into the ~/local/share/rhythmbox/rhythmdb.xml db file of Rhythmbox (make a backup of the rhythmdb.xml file before !)

The generated file content must be inserted after the rhythmdb opening tag at the begining of the rhythmdb.xml file

Then start Rhythmbox and run "Update all Feeds"

You're done !

samedi 24 janvier 2009

OOP vs Procedural : a definition

In my opinion one of the best definition :

Procedural code gets information then makes decisions. Object-oriented code tells objects to do things. Alec Sharp

That's an explanation for the Tell, don't ask advice for OOP.

mercredi 24 décembre 2008

Checked exception : why the debat is not over for everyone

I've just read Clean Code by Bob Martin.

In this book uncle Bob says :
"The debate is over [...] At the time, we thought that checked exceptions were a great idea; and yes, they can yield some benefit. However, it is clear now that they aren’t necessary for the production of robust software"

I do agree, but as I was reading these lines I was wondering why, for plenty of people, checked exception is still the way to go when writing a java app.

Here is my humble hypothesis.

First, I need two acronyms :

  • SLJA : "Significantly Large JAVA Application". let's say a 500+ java classes application with at least two layers.

  • BTDT : "Been There, Done That"


So, in the checked vs unchecked exception debate I think that we may find at least 5 categories of contestant :


  • Category 1 : programmers who have never worked on any SLJA and just didn't experience what the real difficulties are with checked exceptions, so these programmers think that checked exceptions must be systematically used. BTDT (remember my second acronym ?)

  • Category 2 : programmers who have already worked on one or two SLJA and who also think that checked exceptions must be systematically used. Still they wonder why they need to use so many try/catch blocks and so many throws directives when the number of classes increases and the layers stack. The bigger the SLJA the more painful it becomes. BTDT.

  • Category 3a : programmers who have aleady worked on some SLJA and who know that exception hierarchy in an application must be thoroughly designed in order to keep the verbosity of the try/catch/throws at a manageable level and to keep a coherence in the abstraction levels.They are sure that checked exception must be used, granted that a MyApplicationException is used as the top most exception in the exception hierarchy of their application. They use "throws MyApplicationException" in every methods, to please the compiler and allow most of their exceptions to bubble up. Most of their try/catch/finally became try/finally as they don't need to rethrow most exceptions anymore, thanks to the "throws MyApplicationException" directive in almost all methods signature. BTDT.

  • Category 3b : programmers who have aleady worked on some SLJA and who are fed up with the MyApplication exception trick. They understand that's only a symptom of the fact that in any SLJA most exceptions must bubble up and unchecked exceptions must be used instead of the "throws MyApplicationException" everywhere. Still, they think that checked exceptions could be useful sometimes for the recoverable errors. BTDT.

  • Category 3c : programmers who have aleady worked on some SLJA. They never use checked exceptions anymore. they have a few unchecked exceptions at the top most level of their exception hierarchy. They declare as many unchecked exceptions as necessary in the packages of the classes that might need to throw specific exceptions. As soon as they have to work with an API that throws checked exceptions, they wrap them with unchecked exceptions.


So, actually, the debate is only over for category 3c (I consider that I'm in 3c now).

It's interesting to understand how you move from 3b to 3c (You're in 3b because you think that checked exception might sometimes be used for recoverable errors).

Let's say you're in 3b and one day you have to design a factory method that parses a string for a credit card number and transform it into a CreditCardNumber object.

The first client of this factory method is the GUI layer, so you think : "hey, the user may have made a mistake when entering the credit card number, it is a recoverable error because the GUI must warn him and retry, so I'm going to use a checked exception". So your factory method throws a BadCreditCardNumberException checked exception that inherit from a more general BadFormatException checked exception.

Two weeks later you have to store the credit card number as a string into a database, and when you reread the record, you must transform the string back into a CreditCardNumber object. So you obviously reuse the factory method that was created. But now you have to catch the BadCreditCardNumber exception in your DAO, and you don't want to do that, because it is really not a recoverable error in this layer. It is a fatal error (data corruption ?) and you just want the application to bubble up and hit the fault barrier of your application as a BadFormatException.

That's the exact moment you move from 3b to 3c because you see that an error that's recoverable in a layer may not be recoverable in an other layer and you really don't want to be forced to artificially chain a checked exception that is not recoverable. You prefer to choose to catch an unchecked exception when you think that it is recoverable for you in your layer.

When you choose to use checked exception for errors that you think are recoverable, you try to decide for the client of you API, whereas only he may know what's recoverable and what isn't in his own context.

Update : Everething was already there

mardi 23 décembre 2008

How to negate a regexp with a negative lookahead

When I need it, I tend to forget how to create a regular expression that matches a large set of strings but does not match some specific strings. It's tricky because it involves the negative lookahead syntax.

For exemple the regexp

.+\.java

matches any java filename.


But the following regexp

(?!Foo\.java).+\.java

matches any java filename except Foo.java because the negative lookahead of Foo\.java will prevent .+\.java from matching.

The negative lookahead syntax is
(?!X) where X is the string that must not be matched.

dimanche 9 novembre 2008

Sepia GFA Basic code

Internet is such a strange place. I've found by chance some lines of GFA Basic code I wrote something like 20 years ago.

Actually in the late 80s the French magazine ST Mag used to publish short GFA Basic programs written by the readers. These were called GFA Punch. They had to have at most 20 lines of code, and had to do something funny or useful. Some GFA Punch I had written were published. Someone kept a floppy disk of a bunch of GFA Punch that were published and made it available.

Now, among all these programs I've found some of my old GFA Punch. For me its like finding an old sepia picture in a box. It's weird. I've decided to post two of these programs just for archeological reason. There isn't any technological interest as the quality of the code is poor.

This one draw colored frames :


This one draw a big ugly and slow scroll text :