Archive

Archive for August, 2011

Java Code Geeks

August 28th, 2011 No comments

I have followed Java Code Geeks regularly, reading almost every article. Content is always top notch and on point! So im really happy to announce that my blog just got accepted into The Java Code Geek (JCG) Program.

This will of course increase publicity, but more importantly, allow me contribute and reach a larger portion of the java community and give something back. Hopefully my posts will spark discussions. I already feel motivated to write more and better posts.

Thank you for the support Byron! Im proud to be a Java Code Geek :-D

Categories: Java, writing Tags:

Uncle Bob

August 24th, 2011 No comments

I just went to a javaforum to celebrate the release of Java 7 with some friends, happy to see that some of the swedish JRockit core team was there to give their view on Java from an Oracle perspective.

The event was immediately full-booked on the day of announcement. And the interest was so high that the promoters quickly organized one more opportunity to celebrate in the coming two weeks, which was also immediately full-booked! Considering the bad economy and outsourcing insanity going on in the industry it is really nice to see Java is really alive and kickin’ it in Sweden :-D

Another great surprise was also revealed at the event, Uncle Bob was present to give a talk on Clean Code. Seeing living legends in person does not happen very often in Sweden unfortunately. I was stoked!

Among others, Robert talked about the importance of short methods. Hell, i want my methods to be short. I usually think 10 lines is reasonable.. but the man insisted on 3!

This made me wonder… what does he think about the Builder pattern where multiple method calls are chained together in a one-liner to create a fluent interface? Would that count as one line even though multiple method calls would be nested within that line of code?

Just for the record: im not talking about a transitive coupling train-wreck here.

Unfortunately I could not manage to ask my question since the QnA was very short, with only two questions answered. But I guess he would approve it as one line, as long as the syntax would be understandable.

Anyways, it was a really good and energizing presentation from Uncle Bob *and* Oracle, not to forget the free beer ;-)

Categories: Java, principles Tags:

The Devil is in the details

August 24th, 2011 No comments

I want code to be simple-n-short, on-point and easy to read. Unnecessary complexity distract and obscure understanding of what is really going on and can be a real killer for productivity.

You know, tangled for-loops and indexes to track, if/else and switch cases, null/validation checks, converting/copying/deleting/sorting collections, exception handling … the list goes on along with ever-increasing line numbers and maintenance burden.

An excellent quote by Tony Hoare comes to mind.

There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies, and the other way is to make it so complicated that there are no obvious deficiencies.

In other words: The Devil is in the details.

Apache Commons have some of most wonderful libraries complementing the JDK APIs, but this post is not about Commons. It is about Google Guava which is similar to Commons in many regards. It provide a single library for commonly used day-to-day tasks, such as collection handling, string manipulation, concurrency, IO, primitives, exceptions etc.

There is so much nice stuff in Guava and I wont have time to go through the complete library, but here at least some examples of what it can do.

Objects
Objects makes it easy to implement hashcode/equals without cluttering your classes too much (Eclipse auto-generation tends to be a bit verbose for my taste).

Classes that implement toString are really pleasant to use when doing debugging and logging, but can be a real pain implement. Objects.toStringHelper makes this really easy and also help maintaining a consistent format for printing objects.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
public class Item {
private String id;
private String name;
public Item(String id, String name) {
this.id = id;
this.id = name;
}
public String getId() { return id;}
public String getName() { return name; }
 
@Override
public int hashCode() {
return Objects.hashCode(getId(), getName());
}
 
@Override
public String toString() {
return Objects.toStringHelper(this).add("id", getId()).add("name", getName()).toString();
}
 
@Override
public boolean equals(Object o) {
if (!(o instanceof Item)) {
return false;
}
Item other = (Item) o;
return Objects.equal(getId(), other.getId();
Objects.equal(getName(), other.getName());
}
}

Printing this class outputs something like this.

1
Item{id=1, name=Book}
view raw gistfile1.txt This Gist brought to you by GitHub.

Throwables
Wrapping the original exception object is not always appropriate, because it can cause ClassNotFoundException in the client code if communication occur between unrelated class loaders or if they are serialized on the wire. Throwables can decouple this dependency, still allowing remote clients to see the stack trace by converting it to a string.

1 2 3 4 5
try {
// throws implementation specific exception
} catch (InternalException e) {
throw new ApiException("reason", Throwables.getStackTraceAsString(e));
}

Iterables
Concatenating two separate collections and performing operations on the result can cause a quite a lot of clutter. Iterables to the rescue. Take a minute and think how code might look without Iterables.concat.

1 2 3
for (Item item : Iterables.concat(books, electronics)) {
// do something useful
}

Multimaps
Multimap is like a Map, but allow multiple values to be stored for every key. The following example is a a variant of a typesafe hetereogeneous container using multimap to realize a product catalogue of items.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
public class ProductCatalogue {
private Multimap<Class, ? extends Item> catalogue = ArrayListMultimap.create();
 
public void add(Item item) {
catalogue.put(item.getClass(), item);
}
 
public <T extends Item> Collection<Item> list(Class<T> clazz) {
return catalogue.get(clazz);
}
}
 
ProductCatalogue catalogue = new ProductCatalogue();
catalogue.add(new Book("1", "Book1"));
catalogue.add(new Movie("2", "Movie1"));
// only get books
System.out.println("Books " + catalogue.list(Book.class));
// only get movies
System.out.println("Movies " + catalogue.list(Movie.class));

BiMap
BiMap implement a one-to-one bidirectional relationship between key and value of the Map. Here is an example using language code to get the language and vice versa.

1 2 3 4 5 6
BiMap<String, String> languageCodes = HashBiMap.create();
languageCodes.put("en", "English");
languageCodes.put("fr", "French");
languageCodes.put("zh", "Chinese");
assert "English" == languageCodes.get("en");
assert "en&amp" == languageCodes.inverse().get("English");

Preconditions
Most classes have restrictions on values given them in constructor and methods. Invalid values should be escalated as soon as possible by doing explicit validity checks before execution. It is a lot better to fail-fast than to fail later with an unexpected exception or worse, silently compute the wrong result.

1 2 3 4 5
public Item(String id, String name) {
this.id = Preconditions.checkNotNull(id, "id must not be null");
this.name = Preconditions.checkNotNull(name, "name must not be null");
Preconditions.checkArgument(name.length() < 6, "name must be longer than 6 chars");
}

Constraints
Constraints are similar to preconditions in a way that they can restrict what values are added to a collection. This makes collections much easier to use and code a lot cleaner, since constraints are separated from business code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
public class Voyage {
private Country targetcountry;
private int capacity;
private List<Cargo> items = Constraints.constrainedList(new ArrayList<Cargo>(), new Constraint<Cargo>() {
@Override
public Cargo checkElement(Cargo cargo) {
Preconditions.checkNotNull(cargo);
Preconditions.checkArgument(targetcountry.allows(cargo));
Preconditions.checkArgument(cargo.getUnits() > 0);
return cargo;
}
});
 
public void load(List<Cargo> cargos) {
items.addAll(cargos);
}
}

Predicates and Functions
Predicates evaluate if something is true or false but can also be combined into more complex evaluations using “and”, “or”, “not” and “in”.

What normally would require a for-loop and bunch of if statements can now be reduced to a one-liner. How sweet is that?

1 2 3 4 5 6 7 8 9 10
Predicate<Item> heavyItemPolicy = new Predicate<Item>() {
@Override
public boolean apply(Item item) {
if(item.getWeight() > 1000){
return true;
}
return false;
}
};
Collection<Item> heavyItems = Collections2.filter(order, heavyItemPolicy);

You can also use Maps.filterKeys or Iterables.filter in a similar way. But keep in mind that removal from modification is bidirectional. e.g. removal from the origin affect result and vice versa.

Functions on the other hand, is a way of transforming one object to another. For example, convert concurrency on a order of items.

1 2 3 4 5 6 7 8
Function currencyConverter = new Function<Double, Item>() {
 
@Override
public Double apply(Item item) {
return item.getPrice() * ANOTHER_CURRENCY;
}
}
Collection<Double> prices = Collections2.transform(order, currencyConverter);

You can also use Maps.transformValues or Iterables.transform in a similar way.

A Query API

I have been think about how to create simple but powerful Fake Objects for some time now. But I dont want fakes themselves to turn into a maintenance burden, so they must be easy to implement. My intuition tells me i need a general purpose state management framework for this to work. And so using predicates, I created a small fluent query interface interacting with an in memory storage.

1 2 3 4 5
InMemoryStorage storage = new InMemoryStorage();
// add a few Item.class objects to storage
Criteria middleprice = field("price").is(largerThan(100)).and(lessThan(200));
Criteria expired = field("expires").is(after(currentDate));
Collection<Item> result = storage.select(middleprice.and(not(expired))).from(Item.class);

I feel quite satisfied with the result actually – short, compact, understandable and typesafe.

Im not going to go into the details here, but please do inspect the implementation of Criteria and InMemoryStorage, as well as the tests.

I hope these examples will trigger you to explore Guava further and use it to make your code more readable, robust and maintainable.

And lastly, I really do hope many of these facilities reach standard Java some day soon.

Categories: coding, Java, principles, testing Tags:

Precarious case of API stability

August 22nd, 2011 2 comments

The NetBeans team just got themselves in a really tricky situation when switching to Java 7.

It turns out the that the Class.getMethods() in Java 7 does not return the methods in a deterministic, but random, order. This causes JUnit tests to be shuffled around for every execution. This may be fine, but the NetBeans team have unknowingly introduced tests that are dependent on each other, and the order in which they are executed, causing their test suite of 8000 tests to fall like a domino.

This is an excellent example of the type of maintenance nightmare that I have warned about earlier. I certainly don’t blame them, they hardly could have anticipated this to happen. I have personal suffering from this myself and know how hard it can be to write independent tests. But this incident convince me even more about the fact that tests should be focused, robust and self-contained. I actually already wrote this down earlier in Open Config Developer Guidelines.

Jaroslav Tulach (who btw also wrote this excellent book) explain the problem in more detail here and you can follow to discussion between him and the JUnit crew on their mailing list.

Categories: coding, Java, principles, testing Tags:

The Truly Educated Never Graduate

August 9th, 2011 No comments

Science can be very complex in theory and people failing to think in these abstract terms probably get cold feet prematurely in their choice of education. Other people have a hard time to apply theory in practice even though they are all very smart people! Indeed, having a university degree is far from an insurance of being intelligent.

Alan Kay, the father of object-oriented programming, shares some very interesting ideas around teaching and understanding. James Bach also touches on the subject in Secrets of a Buccaneer-Scholar. Noam Chomsky and Sir Ken Robinson further acknowledge the failure of modern education in a very deep and interesting analysis.

I think this is very unfortunate, because it means that we (the people) are somewhat mislead in a system that does not encourage individuals to pursue our their inner passion.

But I have a somewhat optimistic view that it is never too late to pursue your goals, even though you have been mislead in the past, for whatever reasons…

Some time ago I read a very encouraging article explaining that intelligence is never constant, it is work in progress.

A mountain of evidence suggests that intelligence really is a skill. That is, the harder you work, the more you learn. So, when you encounter something difficult, it is better to treat that as a challenge than as a sign that you have reached your mental limits. It is also better to believe that studying hard will lead to good learning than to believe that studying hard leads to poor learning. By putting in extra effort on difficult concepts, you come away with more knowledge.

Ultimately, this aspect of learning feeds on itself. The more you learn at any given time, the easier it is to learn new things in the future. The effort you put in to learn is rewarded by making it easier for you to learn more things in the future.

And it works the other way as well, use your brain or loose it.

I sometimes get the question why I do programming and read about it on my spare time. The explanation is that if you have passion and love for what you do, motivation comes naturally because you are acting along the lines of your most authentic self. It is not about “enduring” your job in the traditional sense, it is who you are. This stimulates enthusiasm and creativity in a very powerful way. Focus on your strengths, as Richard Branson points out.

I cannot stress enough on the importance of having a responsible attitude towards self-education – learning without someone telling you. Taking the opportunity to learn something new and continuously sharpen your skills, every year.

And so I have a few tips for software developers to get into this state of mind. You may of course feel that these advices feel boring and annoying to apply in practice. But if that’s how you feel.. I would advice you to think twice about if software development is for you (if you havent read the links i posted earlier, do so and you’ll see what i mean). So here goes..

Practice continuous self-assessment of your previous work, maybe by looking at old code and try to think out-of-the-box on how it can be improved.

Fork and read code from popular Open Source projects and try to change them. Or even better, join a Open Source project that interest you. This can also be a way to extend your social network with bright, caring and driven individuals that also probably will inspire you in positive ways.

Improve your ability to perceive many different perspectives by learning something entirely different. Try to take two different ideas and combine them in different contexts to create something new, the basic principle of innovation. I have elevated my way of thinking of design a lot from reading philosophy for example, in particular Ontology (study of reality) and Epistemology (study of knowledge). Or why not learn business strategy and planing, management techniques, or economics? Aristotle’s philosophy covered physics, metaphysics, poetry, theater, music, logic, rhetoric, linguistics, politics, government, ethics, biology etc.. that’s an impressively broad set of subjects!

Read books, at least one every year, and try to find timeless books written by people who are famous for their brilliance. If you find out what these people read – add those books to your reading list. Isaac Newton said, “if I have seen further it is by standing on the shoulders of giants”. Eventually you will learn to discover the same fundamental red threads buried underneath different domains, perspectives, shapes and forms.

I just started a blog and i regret not doing it earlier. It makes you really reflect and organize your thoughts and pushes you to find and think about new or interesting things to learn.

Use your own judgement and try to be humble and a good listener – which can be hard in the face of arrogant and ignorant people – the greatest enemy of learning is knowing! Never, ever ever ever, be afraid to ask questions! Socrates used questions to stimulate critical thinking and illustrate ideas, he said, “and in knowing, that you know nothing, makes you the smartest of all”. Richard Feynman, Nobel Prize in Physics 1965, summarize this mindset in a beautiful way.

The more I learn, the more convinced I get that software development truly is about knowledge acquisition and ignorance reduction.

I wish you Good Luck on the lifelong road of learning!

For every step you take toward mastery, your destination moves two steps further away. Embrace mastery as a lifelong endeavor. Learn to love the journey.

-George Leonard

Categories: learning Tags:

Is quality dead?

August 4th, 2011 No comments

Offshore outsourcing is a complex coorporate undertaking that brings a lot of problems along with it and somehow I have a hard time understanding the logic behind it.

Sometimes it seems as if hourly rate is all that matters, thinking that “code-like-hell” mentalities lowers costs, including the belief that more hands are more productive than few. Tell me, are we still not pass this old-fashioned mindset?

This kind of reasoning is an cheapening insult to the craft of software developers.

I realize that developers carry some responsibility for delivering results. I have had my share of daily WTF projects with people who not truly love or take their profession seriously. But software projects does not usually fail because of this, they fail mostly because of overly optimistic schedules and expectations.

Software development is largely an heuristic process of iteratively massaging and discussing goals and constraints of a problem domain, that gradually spirals towards deeper understanding and ultimately solved from a certain perspective. These continuously gained insights should be shared to all people involved in the project. The most efficient way to exchange such information is face-to-face communication.

So how can Open Source be successful being such a geographically distributed way of doing development? For one, it does not encourage “code-like-hell” mentalities and developers on these projects are usually driven by higher ideals and motives that magnetize quality. This is not my point.

Please do excuse a caring software developer’s honest and frustrated reflection, merely scratching the surface, there a lot of aspects to the equation and I do not claim to have a solution. But outsourcing core business without accompanying a comprehensive and technical argumentation for why this decision is taken seeds feelings of disappointment.

Take a step back and ask yourself, is it possible for all of us to focus on maximizing value instead of being obsessed with minimizing cost, or have top management already given up on quality?

Henry Thoreau said, “we become tools of our tools”, which basically means that the more complex our tools are, the more complex our lives become.

Less is more, remember?

Categories: business, principles Tags:

Reuse fanatics

August 3rd, 2011 4 comments

I really enjoy working with JUnit, it has served me well in the past and really appreciate the efforts made into this fantastic framework.

However, I need to write test methods that can be executed for unforeseen circumstances without changing tests themselves. Being able to write tests that can be easily reused for a *combination* of irrelated set of fixture inputs (or pre-conditions if you will).

For example, I want to write JUnit tests that automatically assert success for all possible outcomes using a combination of application servers (weblogic, jboss, glassfish), JPA providers (eclipse-link, open-jpa, hibernate), databases (mysql, oracle, postgresql) and a unspecified set of boundary/randomized input values (possibly also including data provided by a QA database)… without writing redundant tests for these combinations.

Having such a mechanism would make it real easy to introduce additional test fixtures without changing tests, removing duplication and significant test maintenance.

It would be reasonably easy to parallelize tests (and possibly distribute them across machines), if verifying all combinations turns out to take too long time.

I have tried org.junit.runners.Parameterized but its mechanics feels a bit too constraining and verbose for my needs. So I created my own JUnitRunner. I will not go into details of its implementation (you can check it for yourself).

This is a trivial example where each fixture argument is of the same type, but there is no limitation using different fixture types and the number of arguments can vary as needed.

1 2 3 4 5 6 7
@RunWith(JUnitRunner.class)
public class JUnitRunnerDemoTest {
@Test
public void demoFixtureArguments(FixtureExample one, FixtureExample two, FixtureExample three) {
System.out.println("demonstrateFixtureArguments(" + one + ", " + two + ", " + three + ")");
}
}

Assumming each fixture produce two separate sets of input data, the output would look something like this (where each line is a separate test execution).
1 2 3 4 5 6 7 8
demonstrateFixtureArguments(0, 0, 0)
demonstrateFixtureArguments(1, 0, 0)
demonstrateFixtureArguments(0, 1, 0)
demonstrateFixtureArguments(1, 1, 0)
demonstrateFixtureArguments(0, 0, 1)
demonstrateFixtureArguments(1, 0, 1)
demonstrateFixtureArguments(0, 1, 1)
demonstrateFixtureArguments(1, 1, 1)
view raw gistfile1.txt This Gist brought to you by GitHub.

I also plan to implement data generation annotations for standard java.lang types, enabling me to do the following.

1 2 3
@Test
public void demoFixtureArguments(@Boundry @Random(10) Integer one, @Random Date two, FixtureExample three) {
}

And taking this even futher, enabling data generation annotations in Fixture classes.

Ok, Mission accomplished. Moving on to next topic of this post, JUnit Rules.

JUnit Rules enables you to manipulate the test running process itself in various ways. Every rule needs to be activated by having a variable annotated with the @Rule annotation in the test.

1 2 3 4 5 6 7 8
public class DummyTest {
@Rule
public IgnoreLeadingFailure ilf = new IgnoreLeadingFailure();
@Test
public void testTest() {
assertTrue(false);
}
}

This feel a bit too intrusive for my taste, I would like to annotate anything without the burden of @Rule activation.

So I incoorporated this into my JUnitRunner aswell, allowing me to write this instead (no need for activating @Transactional).

1 2 3 4 5 6 7 8
@RunWith(JUnitRunner.class)
public class DummyTest {
@Test
@Transactional
public void testTest() {
assertTrue(false);
}
}

The magic lies in the RuleHandler annotation, which is used as a meta-annotation for annotating @Transactional to indicate what org.junit.rules.MethodRule to run.

I can see the need for creating annotation extension hierarchies aswell, using the same technique to create a chain of MethodRule invocations using a single base annotation.

1 2 3 4 5 6 7 8 9
@Reliable
@RuleHandler(ExampleRule.class)
public @interface ExampleAnnotation {
}
 
@Traceable
@RuleHandler(ReliableRule.class)
public @interface Reliable {
}

These techniques also integrates seamlessly. Sweet!

1 2 3 4 5
@Test
@Reliable
public void demoFixtureArguments(FixtureExample one, FixtureExample two, FixtureExample three) {
System.out.println("demonstrateFixtureArguments(" + one + ", " + two + ", " + three + ")");
}



One final remark…

Im not sure I understand why JUnit limit each test class to be run with *one* JUnit Runner, verbosely indicated @RunWith for every test. I have multiple frameworks that I need to activate simultaneously, such as arquillian, unitils and soon my own stuff.. but cannot because of this constraint. This bothers me.

Im thinking, wouldnt it be possible to discover JUnit Runners at runtime, activating a chain of combined runners, removing the need for @RunWith? Hmmm..

Im not trying to criticise JUnit in any way. I would be more than happy to help contribute with improvements, and I also do realize that I may make hasty and incorrect assumptions of JUnit design principles.

Thoughts?

Categories: coding, Java, testing Tags:

The crystal ball illusion

August 1st, 2011 No comments

I have noticed that people sometimes avoid doing thorough testing. This may sound as bogus to some, but hear me out… I do understand why this happen.

Tests can create a feeling of being trapped, a burden that gets heavier to carry with every new test introduced. Building a stable, non-intrusive and quality ensuring suite of tests is a difficult task… but *why* does these problems surface?

Most people can agree that testing, performed in one way or another, for any type of product, is a great way for revealing quality and potentially later increase it. But the testing process can get problematic when you specify a static suite of tests and then continuously re-execute it through the lifecycle of the product, i.e. regression testing.

Stop and think for a second… what value does interfaces and abstractions provide to clients? They provide a way to enjoy a *particularly* valuable service without having to worry about the complex internal details of *how* it is provided to us.

Interfaces are not unique to software development, they are everywhere in our everyday community. Consider leaving your Toyota in for repair. Employer Mike is excellent fixing Toyota cars using custom-made tools. But do you, as a client, really care *how* and by *whom* the car was repaired, if you can observe that malfunctioning parts have been replaced and the car works better than before?

Mike will do his job better without being bothered by people who cannot keep up with his bleeding-edge techniques. Mike may quit or learn even better methods, throwing some of his old custom-made tools away. It is better to assure that the company service, as a whole, can deliver high quality for a wide array of diverse problems and clients.

This is a simplification, but you get the point.

I have observed that testing efforts (far too often) tries to verify unspecified implementation details, without really thinking of the consequences. Having too many poorly specified tests can easily snowball into a chaotic maintenance nightmare, making future product development difficult and unproductive. When these symptoms come creeping developers stop making continuous improvements, to avoid problematic refactoring and infectious conflicts with QA, hurting product quality more than helping it.

Internal domain experts can help you understand fundamental business strategies and what *really* matters for the company to be successful. Think long and hard when specifying significant interfaces that are visible for *both* internal and external business processes. Distill abstractions that capture high-level goals and concepts, not complex implementation details. Focus your design and testing efforts on these interfaces and you will earn stability.

Remember that business is fiercely competitive and vibrant, changing rapidly to outrun competitors. Allow flexibility for business to grow and evolve when defining interfaces and tests in your architecture. Enable interfaces to be combined into unique service compositions to support unforeseen business capabilities, maximizing value with minimal efforts.

The future will forever remain untold so do not make too many assumptions on what tomorrow brings.

There is no such thing as a crystal ball.

Categories: business, principles, testing Tags: