Monthly Archives: February 2015

Quick, Cheap, Quality: choose two

Quick Cheap Fast

You can select two

It is old and common wisdom. Even printed on billboard of the mechanics shop where my car is usually repaired. And as with many well known facts: they are ignored many times.

Although his is a wider issue, and many statements I am going to make in this article is valid for other industries, I will focus on IT and more specifically on software development. I do that, because this is where I have experience and my interest. The software industry is new as compared to building constructions or car repair and the customers many times have unreal expectations. To make the situation worse bad developers and companies harvest the obliviousness of customers cheating them. This leads to misery and many times customers learn that software vendors are unreliable and they just tend not to believe what we say even when they face an honest vendor.

There will be no liberation of the world in this article. There is no such article that could do that, not even such a minor aspect of our lives as customer vendor relationship in software development.

As a vendor choose two you can control

After you realized that there is no free lunch and selected two of the above, it is still not the end of the story. You can say, for example, that you want quality software and fast, no matter what it costs. If you can not control the time or the quality you may get only one or none of the three above.

Controlling money

Controlling the money is the easiest these days so long as long there is a healthy society where contracts are obeyed and executed. You have a contract with the software vendor and you pay no more than the contract price. I have seen software projects when the software was not ready by time and the vendor demanded more money to finish. The customer had two choices. Pay the extra and get the software with some delay or start the whole project with another vendor from scratch. Both of them meant extra cost. Extra payment: obvious. Project start over: investment into vendor relationship on technical level and time to market money lost.

Looking at the story you can say the vendor simply blackmailed the customer. Real life is not that simple many times and a story can not be told in a paragraph as complex as the life is. I was lucky not to be involved in the whole story since I could see that there was foul play on both sides. It is kind of culture how we play these games.

Controlling time

Perhaps time is the second in this list. At least it can easily be measured since the invention of the chronograph. Controlling is, however, more than just measure. As you could see in the example above facing the fact that the software is not ready at the end of the project is a disaster.

To control the time you should use mile stones and project deliverables that show the progress of the project. It may be so important that in some project I have experienced delivery of artifacts that were not needed in the long run and from the position of the developers it seemed to be waste of money. We were asked, and paid of course, to develop a version of a software with an extremely simple UI that was not appropriate for use in the production version. Not a single line of code of this UI was used in the final version. Even though this was capable to demonstrate that the back-end of the software was partially developed it was possible for the customer to check some of the features and there was no room for slide ware lies. (We actually did not intend to lie, but even if we wanted there was no room: the demo was working on a partially developed back-end.)

On the other hand the strong control of time may lead to something that hardly can be named “control” in the noble sense of management. Tracking the progress, requiring constant administration and deliverables only for the time tracking may lead to unjustifiable overhead cost. Since the developers are usually not knowledgable about management they do not usually understand the importance of the measurement of their work and this may lead to frustration adversely affect motivation and thus work.

As is always: there has to be a good balance. There is no easy way to find the balance though. As one of my junior coworker once said when there was too much control and checkpoint in the project: “It is controlling without con.” (for those who have brain challenges: trolling)

Controlling quality

This is the hardest. It is not even trivial to measure the quality. There are great practices in software development that can help the measurement of the quality of a developing software product but they are not measuring quality itself in purest form. They measure something that may, if we are lucky, correlate with the quality of the software. We can measure the number of bugs discovered during a test phase. We can use sonar, PMD, findbugs, checkstyle on the code and follow strict coding conventions. These alone however does not assert that you will have good quality.

It is also a misconcept to aim for bug free software. There can be bugs in the code. The aim is to have a software that fits the business needs. If a software is targeted toward prospect customers, general internet audience who get distracted from a menu structure not intuitive enough then the UI has to be designed accordingly tested with trial audience and fine tuned. It incurs cost. If the cost is less than the business gain: go for it.

If you work for a company and the intranet application is used by internal users, who spend 8 hours a day using the application they learn to use a menu system even that is not too intuitive. I have experienced a software development project where we wanted to make the menu structure more intuitive and the users refused the change wanting the old, bad style structure back: they have already learnt where to click, what key combos to press.

Conclusion

All of these subjects deserve more discussion than a short article or just a section in an article. Here they are more as a discussion ice breaker than something to learn from like a tutorial. Just some ideas and fragments that you can add to in comments if you like.

Advertisement

Using junit for something else

junit != unit test

Junit is the Java unit testing framework. We use it for unit testing usually, but many times we use it to execute integration tests as well. The major difference is that unit tests test individual units, while integration tests test how the different classes work together. This way integration tests cover longer execution chain. This means that they may discover more errors than unit tests, but at the same time they usually run longer times and it is harder to locate the bug if a test fails. If you, as a developer are aware of these differences there is nothing wrong to use junit to execute non-unit tests.

I have seen examples in production code when the junit framework was used to execute system tests, where the execution chain of the test included external service call over the network. Junit is just a tool, so still, if you are aware of the drawbacks there is nothing inherently wrong with it. However in the actual case the execution of the junit tests were executed in the normal maven test phase and once the external service went down the code failed to build. That is bad, clearly showing the developer creating the code was not aware of the big picture that includes the external services and the build process.

After having all that said, let me tell you a different story and join the two threads later.

We speak languages… many

Our programs have user interface, most of the time. The interface contains texts, usually in different languages. Usually in English and local language where the code is targeted. The text literals are usually externalized stored in “properties” files. Having multiple languages we have separate properties file for each language, each defining a literal text for an id.

For example we have the files

messages-de.properties
messages-fr.properties
messages-en.properties
messages-pl.properties
messages.properties

and in the Java code we were accessing these via the Spring MessageSource calling

String label = messageSource.getMessage("my.label.name",null,"label",locale);

We, programmers are kind of lazy

The problems came when we did not have some of the translations of the texts. The job of specifying the actual text of the labels in different languages does not belong to the programmers. Programmers are good speaking Java, C and other programming languages but are not really shining when it comes to natural languages. Most of us just do not speak all the languages needed. There are people who have the job to translate the text. Different people usually for different languages. Some of them work faster, others slower and the coding just could not wait for the translations to be ready. For the time till the final translation is available we use temporary strings.

All temporary solutions become final.

The temporary strings, which were just the English version got into the release.

Process and discipline: failed

To avoid that we implemented a process. We opened a Jira issue for each translation. When the translation was ready it got attached to the issue. When it got edited into the properties file and committed to git the issue was closed. It was such a burden and overhead that programmers were slowed down by it and less disciplined programmers just did not follow the process. Generally it was a bad idea.

We concluded that not having a translation into the properties files is not the real big issue. The issue is not knowing that it was missing and creating a release. So we needed a process to check the correctness of the properties files before release.

Light-way process and control

Checking would have been cumbersome manually. We created junit tests that compared the different language files and checked that there is no key missing from one present in an other and that the values are not the same as the default English version. The junit test was to be executed each time when the project was to be released. Then we realized that some of the values are really the same as the English version so we started to use the letter ‘X’ at the first position in the language files to signal a label waiting for real translated value replacement. At this point somebody suggested that the junit test could be replaced by a simple ‘grep’. It was almost true, except we still wanted to discover missing keys and test running automatically during the release process.

Summary, and take-away

The Junit framework was designed to execute unit tests, but frameworks can and will be used not only for the purpose they were designed for. (Side note: this is actually true for any tool be it simple as a hammer or complex as default methods in Java interfaces.)

You can use junit to execute tasks that can be executed during the testing phase of build and/or release.

  • The tasks should execute fast, since the execution time adds to the build/release cycle.
  • Should not depend on external sources, especially those that are reachable over the network,
    because these going down may also render the build process fail.
  • When something is not acceptable for the build use the junit api to signal failure. Do not just write warnings. Nobody reads warnings.

Default methods and multiple inheritance

Recently Lukas JOOQ Eder posted and article about nested classes and their use. This is an interesting topic and his article is, as always, interesting and worth reading. There was only one slight statement I could not agree with and we had a brief reply chain leading to default method and why there can not be something like

class Outer {
    <non-static> interface Inner {
        default void x() {
            System.out.println(Outer.this.toString());
        }
    }
 
    Inner2 y() {
        return new Inner2();
    }
}
 
class Inner2 implements Inner {
}
 
// This would now print Outer.toString()
// to the console
new Outer().y().x();

in Java. In the above code the default method of an inner interface would refer to the instance that is enclosing the interface, so to say. I believed that a “reply” was not the best communication form for this, as the original topic was different and here I go.

What are default methods

You probably know. If not google it, or read my articles Java 8 default methods: what can and can not do? and How not to use Java 8 default methods.

If you googled you can see that default methods in Java 8 bring the Canaan, multiple inheritance is available.

There is a very good discussion about it on stackoverflow with real professionals, who do know Java:

Java has always had multiple inheritance of types. Default methods add multiple inheritance of behavior, but not of state. (Multiple inheritance of state in languages like C++ is where most of the trouble comes from.) – Brian Goetz Jun 21 ’14 at 2:05

In this article I will examine a little how to interpret and understand that statement.

Types of inheritance

The quote from Brian Goetz mentions:

  • inheritance of types
  • inheritance of behavior, and
  • inheritance of state.

Inheritance of types is very easy and well known for Java programmers. You define abstract methods in the interface, but you do not specify how they work, only the return value and the signature of the methods. With default methods Java 8 introduced inheritance of behavior without inheritance of state. But can you really have inheritance of behavior without inheritance of state? Not really. At least in Java 8 you can have inheritance of state though this is not recommended, not well performing (I mean: it may be slow) and also cumbersome and error prone to program. But you can, and I will show here how. (In addition to the thread local nonsense I published in the article I referred above.)

I believe that Java 8 inventors wanted the default method to keep backward compatibility while implementing the functional interfaces (e.g.: streams) in the standard run time. I recently watched the series Fargo and I feel the language designers just obliviously answered “yes” to the question “Is that what you really want?”

State inheritance with default methods

Default methods can not access fields (except static fields, that are final anyway in interfaces, so let’s forget them for the while). Just like you can not access private fields of class A from a class B extending A. Or the other way around: you can not access the private fields of B from A. You can however have getters and setters in B and if you declare them as abstract methods in A you gain the access. Open sesame. Getters and setters are the solution.

When you declare abstract methods in an interface for all the state fields you want to access from default methods you can access them. This way you get the very same result as if there was real state inheritance. The difference is the syntax: you use getter and setter methods instead of the field name, and you have to declare these in the interface. That way compile phase checks that the getters and setters are really there.

You can see that things with Java 8 get really complicated. Mix that up with generics and you may not find a living soul who understands it all. Having a construct, like

Outer.this.toString()

from the sample code above would make it even more complex with no real leverage, probably.

I believe I have some knowledge about what default methods are in Java 8 and what you can do with them. Having 10 years of Java and more than 30 years of programming experience, however, is not enough for me to tell how you should use default methods. I feel envy for the developers that still work with Java 1.6 or earlier in production code: they need not worry about default methods. (It was meant to be a joke.)

Even though I try to give some advices.

Recommendation

Never mimic state inheritance in default methods. Hard to tell what it is in practice though. Calling a getter or setter is clearly is. Calling some abstract methods that are implemented in the implementing class may or may not be. If doubt: better do not.

Never ever use the threadlocal trick I wrote in the other article.

Use default methods for what Java language inventors used it: keep backward compatibility in your library interfaces. If you ever released a library and it contains an interface (how could otherwise it be, btw) do not change it… Think about client code using your library that implements the interface. From Java 8 you have the possibility to finish the sentence: do not change it incompatible. If there is a new method: create a default implementation so the code that already implemented the previous version remains compatible and there is no need to extend these classes.

Do not unit test bugs

Before getting to the topic of the title let’s have a simple programming sample. On the programming task I will demonstrate some bad coding style and based on that it will be easier for me to explain why the same style is bad in unit tests. Well, now that I wrote this sentence this seems to be a obvious statement. Why something would be good in unit testing when this is not good in programming. One thing is that it is not always the way like that, and the other is that the same mistake may not be so obvious when we create unit tests.

Demo task

The demo task is very simple. Let’s write a class to decide if an integer number > 1 is prime. The algorithm is simple. Check all the numbers starting with 2 until the square root of the number. If the number is not prime we will find a number that divides the number integer times, if we do not find a divisor then the number is prime.

public class PrimeDecider {
	final int number;

	public PrimeDecider(int number) {
		this.number = number;
	}

	public boolean isPrime() {
		for (int n = 2; n * n &lt; number; n++) {
			if (number % n == 0) {
				return false;
			}
		}
		return true;
	}
}

The unit test is

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class PrimDeciderTest {

	@Test
	public void sample_2_IsPrime() {
		PrimeDecider decider = new PrimeDecider(2);
		boolean itIsPrime = decider.isPrime();
		assertTrue(itIsPrime);
	}

	@Test
	public void sample_17_IsPrime() {
		PrimeDecider decider = new PrimeDecider(17);
		boolean itIsPrime = decider.isPrime();
		assertTrue(itIsPrime);
	}

	@Test
	public void sample_10_IsNotPrime() {
		PrimeDecider decider = new PrimeDecider(10);
		boolean itIsPrime = decider.isPrime();
		assertFalse(itIsPrime);
	}
}

This is a great test, readable, some copy paste and most of all it gives us 100% code coverage. Believe me:

Java_EE_-_JavaBeanTester_src_test_java_PrimeDecider_java_-_Eclipse_-__Users_verhasp_github_javax_blog

It is all green. There can go nothing wrong! We happy.

Bug appears

One day, however, somebody gets the strange idea to test if 9 is prime. Believe it or not, our program says that 9 is prime. So the tester (or, if you are not lucky a customer) opens a bug ticket:

BGTCKT17645329-KT The method Prime does not give the
correct answer for the numbers that are multiplications of three. For example it results true for an object that represents 9.

Then comes the tedious work of bug fixing. What a joy it is usually. First of all you overcome your feeling that whispers into your ear that “the customer is stupid”. Obviously the customer is stupid because he wanted to use the class to test the number 9 it was never meant to be… hahh!!! and because the bug description is simply wrong. There is no method Prime! And the code correctly detects for example the number 3 (which is a multiplication of 3 itself) is prime. And it also detect correctly that 6 and 12 are not prime number. So how does a customer dare to craft such a bug report. Thoughts in your brain like that may help you calm down but do not help business, which is the first priority for a professional like you.

After calming down you admit that the code does not really work for the number 9 and you start to debug and fix it. First there comes a unit test that fails. That is the way we have to do TDD:

	@Test
	public void demonstrationOf_BGTCKT17645329() {
		PrimeDecider decider = new PrimeDecider(9);
		boolean itIsPrime = decider.isPrime();
		assertFalse(itIsPrime);
	}

and you deliver the fix:

	public boolean isPrime() {
		if (number == 9)
			return false;
		for (int n = 2; n * n &lt; number; n++) {
			if (number % n == 0) {
				return false;
			}
		}
		return true;
	}

I am just kidding!!!… or not

Actually I have seen fixes like that in real production code. When you are under time pressure and since life is finite you are, you may come up with a fix like that even when you know what the proper solution would be. In this case it is as simple as inserting a = in front of the < sign in the loop condition to test that the number is actually not the square of a prime number. Essentially the code

for (int n = 2; n * n =&lt; number; n++) {

would be nice.

In real production cases this may be a real and huge refactoring and if these special cases appear rarely since the code is usually used for numbers less than 25 then this fix is (may be) commercially OK.

Realistic fix for the bug

Be more realistic and assume that you realize that problem is not limited to the number 9 but to all square numbers and you apply the fix:

public class PrimeDecider {
	final int number;

	public PrimeDecider(int number) {
		this.number = number;
	}

	public boolean isPrime() {
		if (isASquareNumber(number))
			return false;
		for (int n = 2; n * n &lt; number; n++) {
			if (number % n == 0) {
				return false;
			}
		}
		return true;
	}

	private boolean isASquareNumber(int number) {
		double d = Math.sqrt(number);
		return Math.floor(d) == d;
	}
}

This is ugly, but it works. Real word code with god classes containing a few thousand lines do not get any better than this even after refactoring.

Are we finished? Not really. Lets look at the unit tests again. It documents that the code

sample 2 is prime
sample 17 is prime
sample 10 is not prime
demonstration of BGTCKT17645329

Thats is not really meaningful, especially the last line. The bug was reported (in addition to some false statement) that the number 9 is not handled properly. But the actual bug was that the program did not handle properly the numbers that were squares of prime numbers. If you know ITIL the first one is the incident and the second one is the problem. We created a unit test for the incident and it was good we did that. It helped the debugging. But when we identified the problem, before applying the fix we did not create one to test the fix for the problem. This was not real TDD and because there was a unit test for the incident but we did not create it to test the fix.

The proper test would have a name something like

some sample square number is not prime

(with the appropriate camel casing in the method name) and it would have some square numbers, like 9, 25, 36 as test data.

Conclusion

When fixing bug be careful with TDD. You may apply it wrong. TDD says to write the unit test before you code. The unit test you write will define what you want to code. This is not the unit test that demonstrate the bug. You can use that as a tool to debug and find the root cause. But that is not the part of TDD. When you know what to write, no matter how eager you are to fix the code: do write the unit test that will test the functionality that you are going to write.

This is what I wanted to imply (in an attention catching way) in the title: write a unit test for the functionality or functionality change that fixes the bug instead of the bug.