## When to use log.isDebugEnabled?

One of the first coding-related question my partner asked me when I joined my current project: do you use log.isDebugEnabled()? This is a recurring question, and my answer is generally the same: “No.” Still, there is some nuance in this no. I will try to get to the point today and forget my usual digressions.

## Better comparison of Java implementations

I already posted a class to enable the comparison of Java method implementations. However, this was a draft method rather than a true solution.

Today, I come back with a(n almost) valid solution. You can get it on my Github repository: labs-java, implementation-comparer branch.

Basically, it will be able to compare several implementations of any class within its context, rather than the previous solution which forced you to rewrite the methods within the comparer.

Let us imagine you have a Foo class with a bar(String arg), but you suspect this method can be written in a better and more efficient way, but you want to ensure both methods return the same results and make sure which methods is the most efficient.

You will have to write the variants for your method, and suffix them with an incremental index (bar1(String arg), bar2(String arg), …). Then, you will call the implementation comparer with some arguments you wish to test:

This will output something like this:

The time checks are performed only if the info log level is above INFO, and the result comparison displays the details only on debug level, but the compare method returns true if all methods return the same result, and false otherwise.

This comparer can be really useful when you are refactoring or even included in unit testing if you find a use for it.

For more details, you can have a look at the class itself. It is really not that complicated and I made it an obligation to have documented code nonetheless.

As a reminder, you can also have a look at my my previous draft version, which came with more explanations. The main principle remains the same, only with more freedom.

And if you need help, you can always ask below, as I will not go further into details right now.

For the logging of tables, I used Alcibiade’s asciiart library.

All this just in preparation of my next post… And yet, I am quite satisfied with myself and feels this will come in handy next time I refactor…

## Comparing multiple directories (Java)

Several tools enable the recursive comparison of directories, but most of them allow for the comparison of only two directories (like my favorite, WinMerge). KDiff3 goes as far as comparing three files or directories.

As for me, I needed to compare up to five directories. As a consequence, I implemented my own algorithm. Read for more information.
Continue reading Comparing multiple directories (Java)

## Localization in Java

Localization, or internationalization, is important in most languages. What good is an application if it is restricted to your own country because you speak a far from widespread language? And as most languages, Java offers a mechanism to implement it quite easily. I will review here the basics, using Daily Tasks as an example.

## Simplify boolean expressions using WolframAlpha

When browsing through legacy code, or even when writing my own, I sometimes come across tests which seem uselessly elaborated, and in those occasion, I cannot resist the temptation: I grab a paper and draft some boolean algebra. The most time-consuming part is usually drawing my truth table to make sure I made no mistake while simplifying my tests…

I just thought I would write a program to perform those simplifications. Coding basic simplification rules from boolean algebra seemed quite simple if I used Tom, for instance. Never found the time though. Luckily, if you wish to skip the paper part and get quicker results, you can use WolframAlpha
Continue reading Simplify boolean expressions using WolframAlpha

Have you ever had tasks you had to perform each morning on getting at your desk? I do. Since I reboot every day, I first used a text file I added to the All programs > Startup menu of my Windows, but it was not satisfactory, as the text file gets lost behind all my other windows anyway (IDEs, e-mails, … You get the idea.).

So, I finally took the time to program the task manager I wanted: something compact, which will not close until I have not checked all tasks, and which remains on top of other windows. Here is what I did.

## My new favorite log4j conversion pattern

log4j is a powerful logging utility for the Java programmers. Until now, I let other developers configure it. However, I was not satisfied with the configuration in my current project and took a deeper look.

# The log4j pattern

Our logs were cumbersome, always displaying the full package, which is not always interesting. Dates varied from a log to another. So I looked for a standard conversion pattern I could use whenever I have to log something with log4j, and after some diving in the documentation, here is what I came up with:

# Some explanation

• %d{ISO8601} – displays the date and time in the ISO format (see earlier post about this norm).
• %-5p – the log level (such as DEBUG, INFO, WARN, …), formatted to occupy five columns (I like when things keep in line).
• %t – the name of the thread logging this line.
• %c{1} – the name of the class calling the logger (the {1} argument is there to restrict the number of packages displayed; {2} would display one package level before the class name, {3} would display two package levels, … and %c without the brackets would display the full package and class names).
• %M – the method.
• %L – the line number.
• %m – the message to log.
• %n – begin a new line.

There you are.

## Compare implementations of a method in Java

Often at work, I look at the code from the legacy of my project and wonder: was this coded this way for a purpose? Is it optimization? And doubt is upon me. My way seems better, but what if it is not? Well, let us just have a quick comparison!

In this article, I will give you the comparison class I created for myself. Quite simple, focused on time (not the best indicator, maybe, but a clue nonetheless). You just have to supply two methods, configure how you want to run it, and run it. Interested?
Continue reading Compare implementations of a method in Java

## Writing numeric dates…

Numeric dates can be a pain. Look at it: in France, today is 27 février 2013, which turns to 27/02/2013. In the United States, it is February 27th, 2013, which gives 02/27/2013. Sometimes, you do not know which format you are reading: is 02/03/2013 February 3rd or March 2nd?

Well, I discovered only today there is a global ISO standard for writing numeric dates:

This is the format I thought was the most logical when I looked for one at the beginning of my programming learning. As for a number, you go for the most rough value, and you gradually become more precise. You first write the largest unit (the year, or the hour in the case of time), and you go step by step to smaller units (month/minutes, then days/seconds, then time/fractions of seconds).

This is so logical. I mean, we already do it for time everywhere, or even decimal numbers, why is it not applied to dates?

Plus, have you ever tried to sort dates saved as strings? With this format, you can just sort them alphabetically. It is quite convenient, is it not?

For more information about the rules for writing date and time according to the ISO 8601, I suggest you take a look at the corresponding wiki page.

## How to implode iterables in Java

The PHP implode function can be quite useful, but no equivalent exist in Java that I know of. Luckily, you can quite easily implement your own.

# Implode arrays

This is actually pretty straightforward. Here is what you need to do:

Yeah, I declared this array so that it can contain just any kind of objects. No problem when passing StringBuilder.append(Object), since it will use String.valueOf(Object), returning either the null string if object is null, or the .toString() of the instance.

But arrays in PHP behave more like lists, so why not extend this method to Java Lists?

# Implode lists

Using an Iterator is great. It avoids using the .size() to count the elements of the list, and does not require an explicit loop with the .get(int).

Plus, since Iterator applies to all collections, you can extend the method…

# Implode collections

Alright, now it works for lists and sets. Just remember the order of elements is not garanteed if you use this method with a set.

[2013/01/16: made methods null safe]