Let your code know… your code

Has this ever happened to you ?

… you work on a legacy project, and there is this method called `getAccount` which sounds simple.
But this method is a compound one.
1°) It actually calls a database A to fetch a user Id.
2°) Then completes the data with a B Web service based on LDAP
3°) Then completes it with some extra data based on the account preferences from a service C
4°) It updates the database with the found data (yes, « getAccount » writes something… in a legacy project, you will certainly see that)
5°) It returns all the data.

What if you call getAccount from another point of view ? If you are the caller of the method, you could even do this :

if (getAccount (user) != null) {
String userName = getAccount (user).getName ();

Ugh… Did you realize that these 4 lines will call different external services 3 * 2 = 6 times… It has awful performance, and can be harmful (= with side effects) if the method getAccount is not idempotent (which is likely to happen in a legacy project)

What you need is not (really) refactoring. The technical debt is too tough to be attacked right now. What you need IS knowledge about the code. How can you know what is happening in the code ? Think painless, think about code to understand code …

I have created what I call a DiveDeepAnalyzer. The aim of this is to understand which sensitive methods are called directly or indirectly in each method.


Write down the list of sensitive calls (webservices, LDAP, databases, cache, drivers, hardware), and the algorithm will annotate each and every method that is likely to use these calls.

How to ?
1°) Copy this class in your project,
2°) change the params in the main method (you will need to exclude the DiveDeepAnalyzer class itself from the scan)
3°) run the class.

This analyzer uses spoon, an OSS made by a research lab in France (inria). You can find more about spoon at this address : http://spoon.gforge.inria.fr/

Query your app data like you would query a database

The interesting job in creating an app is to send or receive data to another actor :
– whether it is human or not,
– whether it is UI, JSON/Xml/Yaml or console text,
– whether it is a webApp, a program or a script

Now, what happens between the data exchange and the added value of your software ? Boring data manipulation stuff.

Furthermore, let’s imagine that you need to filter every bank account transaction on your account server with a synthetic data (for example, the impact of an economic crisis on the account itself) ?
public float estimatedBalance (int nbDays, float economicHealth)
-> Most of the time you would not compute that with a simple database, because it can depend on some calculation thanks to an algorithm.

Then, build a data cache and try to query your data inside your application instead…
I have 2 solutions to suggest (with a JVM) :
– Switch to Java 8, and to the easy lambda function syntax :
List result = accounts.filter(account -> account.estimatedBalance(90, -1) < max);
– Use my lib : http://github.com/libetl/prime. You are now able to query some java objects by using a simplified query syntax. You can even pass a String object and therefore construct a query on your own java objects. On the homepage of this project, you will get a preview of what can be done using this syntax.

It can be used as a backend if you don’t need to persist the data really. I am actually thinking if it could be useful to actually save the data in a file…