Is TDD Only for … Junior Developers??

Just before the Easter holidays, I had a discussion with two senior developers from my project about TDD. These guys are experienced software engineers that have been around for some time: they have 11 and 20 years of experience in software development under their belts.

I don’t claim to be an advocate for TDD. Currently, I do not practice it (yet), but I do recognize and appreciate its importance. TDD really “forces” you to have clear understanding about the business requirements to be implemented. You cant implement what you do not understand, right? Because you have to write the test first, your code becomes more solid, less bug prune and you have better test coverage.

Sure, writing the test first its an interesting concept: You start writing a test, and you discoverer that you need a class, and possibly a function. You create the class, function and keep writing the test. Basically, you write few lines of test, and few lines of code, few more lines of test and few more lines of code etc. etc.

Ok, I think I diverted a bit, back to the topic :) The discussion took an interesting turn, and I still keep thinking about it. My question to them was – what do you think about TDD? The responses I received totally surprised me.

One of the claims supported by them, was that TDD does not serve much purpose, and a developer will end up re-factoring the unit tests eventually anyway as a result of re-factoring the concrete classes as the development goes on. So if latter is the case, my colleagues argued that there is no point writing unit tests first. Its better to finish writing concrete class, before attempting to write any unit tests.

Also, one of the developers claimed that many books and articles written on TDD discuss examples that are quite simple to implement, and in reality it is quite difficult to use TDD for complex business scenarios.

Another claim was that TDD should be used to teach junior developers the importance of unit testing, the experienced developers don’t really need to use it. The developers should follow KISS principles instead.

I respected their opinions about it, but it seemed fundamentally wrong to me that such experienced developers claim that TDD is basically overrated. The feeling that I got from them was that a lot of developers and software engineers in IT industry really got it wrong.

It got me wondering how, after more than ten years of experience in the industry, one does not appreciate one of the best software engineering practices around …

Offcourse, having said all that, I must point out that TDD is not suitable for every case. TDD can be effective only when it is clear what need to be implemented and there is a deep understanding of requirements.

Jquery UI: Add a Shadow Line Around a Dialog Box

I really like the style of Facebook (or LinkedIn) Javascript popup dialogs – a thin semi-transparent shadow line around the dialog. It looks nice and from the usability point of view it has a purpose, as it is attracts user’s attention to the important component on the screen.

There are many examples of custom Javascript dialogs on the Internet that explain how to achieve this. While many solutions are really good, I wanted to make use of the dialog provided by jQuery UI library by default.

I have came up with a solution that works, but I don’t think it is the most elegant way to do it (CSS is not my strong side). The solution is quite simple, and pretty straight forward (I think :) ). The following are the final results as tested in FF3, IE8, Opera 10 and Chrome 10, please note the shadow line around the dialog:

Firefox - jQuery dialog box with semi-transparent shadow around it

Firefox 3.6.16

Internet Explorer - jQuery dialog box with semi-transparent shadow around it

Internet Explorer 8.0.76

opera-10.63

opera-10.63

chrome-10.0.648

First I defined a CSS class that’s going to have properties for the background shadow (the bg_dialog_modeless_mask.png is an 8×8 gray square image)

.dialog-mask {
 -moz-border-radius:5px 5px 5px 5px;
 -webkit-border-radius: 5px;
 border-radius: 5px;
 background:url("../images/bg_dialog_modeless_mask.png") repeat scroll left top transparent;
}

Somewhere on my HTML page, I defined a DIV that’s going to be the dialog holder:

<div id="popup-holder"></div>

I added jQuery dialog init statement in my global JS file:

$(document).ready(function(){

 $( '#popup-holder' ).dialog({
       autoOpen: false,
       height: 350,
       width: 530,
       modal: true,
       resizable: true,
       draggable: false,
       buttons: {
         Cancel: function() {
            $( this ).dialog( "close" );
            $( '#dialog-wrapper' ).remove();
         }
      },
      close: function() {
            $( '#dialog-wrapper' ).remove();
      },
      open: function() {
            wrapJqueryDialog($( this ));
      },
      resize: function() {
            wrapJqueryDialog($( this ));
      }
   });
});

The #dialog-wrapper is the ID of the DIV that holds the semi-transparent shadow which wraps the dialog. The wrapJqueryDialog function creates the shadow when dialog is opened, and handles the shadow resize when dialog is resized (it is also possible to add an event to drag the shadow when dialog has draggable option set to true).

Now the important part, the wrapJqueryDialog function:

function wrapJqueryDialog(dialogWidgetObj)  {
	//The width and height of the shadow line wrapping the dialog
        var shadowWidth = 20;

        //Lets remove the shadow holder to start clean
	$( '#dialog-wrapper' ).remove();

        //Gets the current dialog widget
	var widget = dialogWidgetObj.dialog( "widget" );

        //Create the shadow holder DIV that has the background shadow CSS class
	var dialogwrapper = $('<div id="dialog-wrapper" class="dialog-mask" />');

	//For IE, the holder must be appended to the body first before any manipulation.
	$('body').append(dialogwrapper);

        //Lets copy default inline styles from the dialog widget to the shadow holder
	dialogwrapper.attr("style", widget.attr("style"));

        //Lets position the shadow holder before and above the dialog widget
	dialogwrapper.css("top", parseInt(dialogwrapper.css("top")) - shadowWidth);
	dialogwrapper.css("left", parseInt(dialogwrapper.css("left")) - shadowWidth);

        dialogwrapper.css("position", "absolute");

	//Make sure that the dialog widget is infront of surrounding shadow box and overlay (if there is one set)
	widget.css("z-index", (parseInt(widget.css("z-index")) + 1));

	//Add to the shadow width default padding value from the class '.ui-dialog' and
	//default border width from the class '.ui-widget-content' (1px times two)
	var finalShadowWidth = shadowWidth + parseInt(widget.css("padding-top")) + 2;

	var widgetHeight = parseInt(widget.css("height"));
        //Lets extend the shadow holder height
	dialogwrapper.css("height", widgetHeight + (finalShadowWidth * 2));

	var widgetWidth = parseInt(widget.css("width"));
	//Lets extend the shadow holder width
        dialogwrapper.css("width", widgetWidth + (finalShadowWidth * 2));
}

That’s all when it comes to code (don’t forget to add to your HTML/JS code a dialog trigger, it can be a link or a button). I am really interested to hear your comments about it and suggestions. If you can suggest a better way of implementation – please do so, I am really keen to know :)

Java, Good Ways – A Book of Unwritten Rules, Tips and Gotchas

A colleague of mine, who is currently my tech lead, wrote a book.

The book Java, Good Ways is a collection of unwritten rules, tips and gotchas collected over a long period of time. The book comes to minimize the gap between university studies and what Java developers actually need to know.

Have a look, read through, let me know what you think or simply leave a comment on Java, Good Ways :)