Posts tagged refactoring


Maybe you know, maybe no – it’s about five years as I’m involved in the Apache Struts project. About two years ago I became a Struts 2 Lead – whatever it means in non-profit organisation working on an opensource project where everyone is equal 🙂 Nevertheless I like what I’m doing – code and refactor 🙂 And as there is a plenty of code in the project and most of them need to be improved – I have what to do for the next few years 😀

Recently I’ve started working on a new plugin – the MVEL plugin – which will allow to use MVEL instead of OGNL to handle expressions. In theory the task wasn’t too difficult, Struts 2 already provides many extension points where you can hook in with your code. Replacing expression engine require to implement two interfaces – very simple and straightforward.  After I did that, I have started testing many possible expression syntaxes which are already present in unit tests.

And then problems show off :/ The main problem is conversion, to give you a hint check the below expression which simple means array in both – MVEL and OGNL


but the problem is how both engines handle such statement (pseudo code):

  • MVEL:
    new Object[]{Integer, Integer, Integer}
  • OGNL:
    new ArrayList(){Integer, Integer, Integer}

As you can see the difference is slight but important – the wrapping object is totally different. And then goes conversion logic, as I wanted to get string on output, built-in conversion mechanism do this:

  • MVEL: [1]
  • OGNL: [1,2,3]

Not so good 😉 The problem is that the conversion logic is too narrowed to OGNL. After some internal brainstorming I’ve came up with a plan – to allow other expression engines to work with Struts 2, first I must decouple conversion logic and OGNL 🙂 So the plan is:

  • extract conversion mechanism to allow replacement of it (another extension point)
  • simplify current logic related to converters created by users
  • implement more common logic to allow handle wide list of results
  • implement MVEL specific conversion
  • implement MVEL plugin 🙂

As you see the plan is simple, but as it is with plans – it is good to prepare them, but to follow them is a different story 😉

Finally, I found something interesting in the latest newsletter from TED – something about attention 🙂

avoid reflection

The Java Reflection API is a very nice future of the Java language. With the API you can do the Magic with Java classes, create an object or call a method on fly base just only on a name. The real Magic!Установка откосов – монтаж пластиковых откосов

So then, we all should use it all the time, to write a better code, yes? NO!

Avoid using it, try to write a code without the Reflection. The worst you can do is if you introduce some generic mechanism with it – a custom validation framework or a flow engine where class and method names are stored as a String (in a text file or a XML file).

You can ask: why?

The answer is simple: your IDE doesn’t support it! When you refactor your class or method, eg. just rename it, IDE will find all the references and change them appropriately for you. But IDE won’t know about your custom solution, is just a text, nothing more!

When you are using the Reflection, your IDE is blind!

Right now I’m working on our custom validation framework, to move definitions of validation from a XML file directly to a Java code. The case is, to not to change the validators, just used them as is. And not to use the Reflection as well 😉

I’ve done some draft implementation base on anonymous classes and the word “final” (thanks Rafal for the tip!). And introduce the ValidationAware interface to be able at the same time use the old way and my new solution! Next week on Monday I will ask other team mates for opinion!

are you affraid

The last two weeks I’ve been simplifying an application flow engine base on Apache Struts 1 – I know it’s a bit old technology but I don’t care, I have a job to do 😉

Anyway I discovered lot of strange use cases where some custom mechanism was build instead to use what was already provided with the framework. It was like inventing a wheel once again! Frankly, I was truly surprised how someone could do this that way. And I start thinking what was the reason to do that…

The only thought came to the mind that someone was afraid to use what was already provided to him with Apache Struts 1 – he didn’t know the framework well, he was too lazy to check sources, and so on. I was real devastated! If someone doing application in such way, no framework will help him or do the job for him!

Recently I’ve been solving issues in Apache Struts 2 and one of them was to add wildcard support to a file upload mechanism. Provided patch had solved that problem with some home made RegExp engine. I just added a test case to proof it was working properly and committed all the changes to a source code repository. After that a more experienced Committer pointed me out that I should use existing PatterMatcher class instead, what I did and the code looks much better right now. You can see an example here Apache Struts 2 useful beans.

beautiful code

Yesterday I had to help a younger team mate to solve a simple problem with persisting data with HTTP session. Case was quite simple but proposed solution wasn’t the best. We discussed other possibilities and found that what we had done is the best one for given circumstances. He did the work and has committed changes to source repository and then informed project architect.

As I said, solution wasn’t prefect but was the simplest one and easy to understand in the future when someone else who will take a care for the project. I took a look on the code and it was etoro beautiful! Separated meaningful method name, named constants for String literals, null safe – beautiful! And all that was achieved without any comment from my side regarding how a code should looks like!


The day was so bored, I’ve been fixing bugs regarding the new print template I had made last week. Many small and stupid mistakes, few of them were significant but most have never wouldn’t happen when I got more focus at first time when I did the template. Nonetheless I closed around 30 bugs, that improved my statistics 😉

After work we had first meeting of Warszawa Design Patterns Study Group for this season. It was purely organizational meeting, we’ve been discussing about what we want to do for up coming weeks. What kind of expectation we have and how to achieve them. We decided to learn TDD on some real example, next we will add DDD, maybe some Scrum and XP methodology. We will see! As a example we decide to develop software to manage conferences. As a organizer of Javarsovia 2009 conference I had experienced  lots of problem that I can address in the requirements. Hopefully we will be able to build either something useful or at least some basis for a real project.

I must prepare a backlog and next time we will be able to start coding 😉

The quite interested technique was suggested during meeting. The meeting should start as a Code Kata session when we will get some functionality and learn coding techniques. The second part will be like a Randori Session when we will be able to refactor the code and learn (maybe) some Design Patterns with live examples!

I’m looking forward for the next meeting!


Today I still tried to improve code quality of application. I double-checked what I had done previously to be sure I didn’t introduce some typo. I’ve been working on that when I got message from testers that they experienced some strange behaviour with the application. It just showed them blank screen when they had tried enter given page. I made quit investigation and found a problem.

The worst thing was that it happened because of automatically merge – two additional lines were added where the same lines already were there but surrounded with if () to protect against NullPointerException. It was stupid mistake and any test didn’t check such piece of code against null value. I will have to add some tests and check more carefully in the future what was merged during project update. I’m using IntelliJ IDEA and there is fancy window with update status, see below:


After quick fix and deployment to test environment I was able to came back to refactoring. I’ve been digging around trying to find places where the code can be improved.

In mean time I’ve got request to correct some mistakes on the print template I had made few days ago. It took me about one hour and once again I was able follow with application refactoring.

half a way

I started, mentioned early code clean-up and refactoring. It was huge task but quite simply – I just had to keep focus on what I was doing. I implemented all the changes and confirmed that everything was ok with Unite tests. No problem so far!

Then I decided to committed the code to have clean starting point for next step. In first step I just introduced one common interface which base on Template Method pattern and reimplemented all needed concrete classes. After all I had one common interface to use in many places, it simplified many operations and I was able to cut off bunch of unneeded methods.

That was the half of way. The second part was to used custom Enums instead of String constants. I implemented the Enums and started replacing Strings. It was very bored and could introduce accidental bug. Hopefully it didn’t – lots of manual typing without mistake!

I reran the tests to confirm that I didn’t break anything. Done and committed! That was all for today, tomorrow I will still try to simplify the interface. To have less code needed to cover by tests 😉

too good

As I said last time, today I was assigned to a new task. The job was mostly done by a team mate, I just had to implement some additional functions – basically extend already existing logic. It was an old code I’d written some time ago, but was a bit overused and became a mess. So, I thought to clean-up it first, to did it in right way. I’ve started refactoring some large parts of application and after three hours I still was in middle of nowhere, with bunch of  changed classes. Neither new functionality wasn’t done, nor code clean-up.

The time was important, so I decided to revert back all my changes and start again with the simplest changes to met requirements. I also dropped idea to improve code quality in the same time, I had already registered some extra time with PM to do this later. Anyway, after another two-tree hours, I finished and the team mate was able to moved forward with his changes.

I committed my changes and also he did the same. Than we verified the application and double-checked all key figures. Everything was as expected, we were able to mark bug as solved.

At the end of day I started refactoring and cleaning the code, that work will follow during upcoming days!

I also begun introducing Zen-To-Done in to my life, it should allow me to better manage my time and life 😀

the bug

Today day wasn’t pleased, it started with phone from a tester that something was wrong with application. It wasn’t crushing or something – the figures calculated were wrong – the worth thing ever! I got some screen shots with little explanation what is wrong, what was expected. Try to guess!

Nevertheless I started digging in to the code. It was hard as we introduced some many changes because of Clean Code Days. I wasn’t able to find anything particular that could be the root of the problem. I was very annoying! Another phones from testers and managers didn’t allow to think productively! I started asking around, maybe someone would figure out what it could be. Stupid idea!

Fortunately, our application dumped to a file all request sent to the WebServices. One of the team fellow identified the wrong output from a WebService and found the corresponded input. He sent that data to author of that WebService from another team and he discovered the bug! The bug was intrudced by some small refactoring, during writting tests. Right now, all we need is to deploy the new version of that WebService to a test environment!

borring day

Yeah, it was very boring day, I finished solving the bug from previous day, added additional test and closed the issue, nothing special. I was also checking logs to find something interesting, I mean, some unusual bug. Nothing, just some common problems with timeouts and performance 🙁Watch Full Movie Streaming Online and Download

So, I ran coverage report and started preparing some issues that have to be solved quickly, especially additional tests and code clean-up. As we have new team members, I think it will be the best way to introduce them in to the project by assign some minor issues and tasks as write those additional tests to them. The best way to learned unknown project is prepare the tests for it.

I got free hand for that, so I’m planning to do this next week, when nobody is being pushing us to solve some important bugs.

Go to Top