At the client site we had a need to write many widgets that render HTML output. The way I approached the problem was not as efficient as I could have approached it and it led to me being stressed about what was able to be achieved in the allotted time frame.
I hope that this tip will save you that same stress.
I wrote a widget and then I wrote a builder to help build the fragments of HTML the widget would put together and output. The solution was neat in my eyes but the builder was a complete waste of time, since a simpler solution was ok for now. The builder will be used later but since I didn’t need it right now, as there was a simpler way, it was a waste of my time to even think it through, let alone implement part of it.
When assigned a task, do the simplest thing that could work, then write a story to refactor it. When the story is functionally complete you can then go back and do the refactoring before moving onto the next story or if time doesn’t permit you can come back to the story later.
So what is the simplest thing that could work?
In my case we had a widget that looked like this when using a html builder:
class ThingWidget {
void renderOn(Writer writer) {
HtmlBuilder html = new HtmlBuilder();
html.divClass(”Widget”)
.with(html.iframe(”someurl”))
.renderOn(writer);
}
This looks simple enough but the fact is, the smarts in the builder took some time, a lot more time than what would have worked for now. This is how the widget was made to be the simplest thing that could work:
class ThingWidget {
void renderOn(Writer writer) {
writer.write(”<div class=\”widget\”><iframe src=\”someurl\”/></div>”);
}
The test in both cases was the same, and this is an important fact:
class ThingWidgetTest {
void testCanRenderWidgetOnWriter() {
String expected = “<div class=”widget”><iframe src=”someurl”/></div>”;
Widget widget = new ThingWidget();
StringWriter writer = new StringWriter();
widget.renderOn(writer);
assertEquals(”Widget rendering invalid.”, expected, writer.toString());
The code in the Widget isn’t how I would want to leave it for some other developer to change, but it is at a point where it does what it is supposed to, it’s backed by a test and it can be checked in. Only now after this simple solution is tested and checked in is it the right time to consider the refactoring or to delay the refactoring by making a story for it.
I’m not a big fan of having lots of refactoring debt so I’d probably try and work on the refactoring rather than card it for later but this depends on the project.
Doing the simplest thing does not mean leaving the code as the simplest thing forever. The first code check-in should be the simplest thing, since others could use it now and if time doesn’t permit the refactoring you still have something that is complete.
I stopped using the HtmlBuilder for now since the complete range of features required of it would take more time than the simpler approach. Leaving it until later also has the added advantage that I will know more about the various scenarios required of the HtmlBuilder when I start to do it. This is a much better situation.
So take small simple steps and avoid the stress of a deadline. I’m putting a print out of the title on my monitor to remind me to do it, always.