Posts Tagged ‘software quality’

How to write untestable code

Say that we write code for a machine that it so powerful that rumors say it could generate a black hole. We need to be serious about security: we cannot let those evil international terrorists find out our black-hole-making secrets and blow half the Milky Way Galaxy.
If they managed to access our source code, they [...]

Don’t look for things. Ask for them!

Before our previous article, our code to model a mechanic used to look like this:
class Mechanic {
private final Engine engine = ServiceLocator.getCar().getEngine();
public void fixEngine() { /* … */ }
}

Luckily, we applied some dependency injection, resulting in nicer code: testable, and with an explicit dependency on the ServiceLocator:
class [...]

Can’t test that Singleton? Try Dependency Injection!

So you want to test this method:

public class Client {

public int process(Params params) {
final Server server = Server.getInstance();
final Data data = server.retrieveDate(params);
// do stuff
[...]