The danger of a poorly written equals in Java

Recently, I was refactoring a system at work and ran across a rather hard-to-track down bug after writing a unit test.  The data structure was an ordered list of what were essential ordered 3-tuples.  At first, the goal of the refactoring was to move classes into new packages then verify that nothing was broken.  Along the way of doing the testing I was alerted to the fact that the default set in the ordered set had changed and that needed to be updated as well and this is where the problem reared it’s ugly head.

I made the changes to the default set to mimic those on the server and then when I compared the new set to the set on the server, I was coming up short on the number of tuples.  Doing a diff of the rules showed two cases:

  1. Incorrect tuples
  2. Duplicate tuples
  3. Missing tuples

I quickly compared all the tuples I created to the tuples on the main list and verified that they were correct.  Once these were verified, I set a break point to see why tuples were missing.  As I stepped through the code I noticed that there was a point where the list stopped growing and items were being replaced when they shouldn’t have.  Obviously, this means we’ve got a problem with the equals.

Looking at the equals, the error was subtle, they attempted to use an != command on two Integer classes expecting it to compare the integer value of the class and not the hashcode.  I made the appropriate change to the equals method and re-ran the test and it passed without problems.

Of course, here I was able to write up what was basically an entire day of work in a few paragraphs… you’d think it’d be easy to spot a poorly written equals or even figure out that is the problem when you begin to see issues in production code.  However, the nature of production code and the complexities of the systems often lead you down rat holes in the search for the true problem.

@Autowired considered harmful

I guess I’m on an opinionated roll, after declaring my dislike of the Singleton design pattern, I’m taking a swing at @Autowired.

Spring is a great framework for application development for Java and even though it’s now arguably more complex than Java itself, it’s advantages far outweigh the problems.  The best part of it to me is the IoC container however, it has introduced one of the largest poxes (next to Singleton :)) to Java coders everywhere: @Autowired.  I’ve seen more problems occur in code due to the use of @Autowired and package scan.  In fact, Colin Yates of Expert Spring MVC + Webflow fame, has this to say about autowiring:

If you insist on sticking with autowiring, there are three different types of autowiring … bad, dangerous and terrible.

And I don’t think I can say it much better than that.

Running command-line programs from inside Java

I’ve been writing a program for a class I’m taking at school to automate some of the work that had to be done.  I could have written it as a shell script, but I wanted to be more adventurous and write it in Java so it could be run in more than just a Unix environment.  So, as you can guess, I had to do execute the program from the command line.  Normally, you’d do this with the exec method off of the Runtime, however, this causes some problems with permissions.  The way to do execute something from the command line is to use the ProcessBuilder object and it’s dead simple.

List command = new ArrayList();

ProcessBuilder processBuilder = new ProcessBuilder(command);
Process process = processBuilder.start();


As you can tell looking at this simple example, command is a list of String which becomes the command you’d normally type into the command line interface. The processBuilder object sets all the wonderful things up to run an operating system process and when you specify start() it turns it into a Process where you can then wait for all the return value.

Of course, there is a lot more to it than just this, but for my case, all I needed was a quick and dirty command to be run and this did the trick.  You can read more about it on StackOverflow.

See some of my code on GitHub

Just a quick update, I’ve decided to publicize the fact I’m using GitHub to store all the example code I’m creating for my website articles.  You can find the links to the repositories in the articles themselves or you can just see all the things I’m working on by going to my user page on GitHub.

You’ll see the unit-testing-examples repository if you poke around my repos.  This is going to be the basis for one series of articles about unit testing, programming, and designing a simple application you’d see in production.  I don’t want to give away too much but I think the articles are going to be great. 🙂

Principles of Good Design as Applied to Software Engineering

Most people know Apple’s iconic designer, Jonathan Ives, but few have heard of another influential designer, Dieter Rams.  Dieter Rams was the chief of design at Braun from 1961 to 1995 and head of design until his retirement in 1998.  Some examples of his work include: 606 Universal Shelving System, the Braun T3 pocket radio, and the Braun T1000 radio. These are considered iconic and many of the new Apple products share more than a passing resemblance to these iconic designs.

Many industrial and user interface designers have been inspired by his work and design principles and I’ve often wondered how his ten principles for good design could be applied to software engineering and programming.  While maybe not intuitively obvious, I believe they can be applied to programming since the principles of design are always the same.  Additionally, one should never stop learning and what better way to learn than to learn something tangential to what you enjoy?  There are always universal lessons and principles that can be adapted and applied elsewhere.

Rams’ Design Principles

Rams’ design principles are simple and straight-forward and are summarized as follows:

Good design:

  • is innovative –The possibilities for innovation are not, by any means, exhausted. Technological development is always offering new opportunities for innovative design. But innovative design always develops in tandem with innovative technology, and can never be an end in itself.
  • makes a product useful –A product is bought to be used. It has to satisfy certain criteria, not only functional, but also psychological and aesthetic. Good design emphasizes the usefulness of a product whilst disregarding anything that could possibly detract from it.
  • is aesthetic –The aesthetic quality of a product is integral to its usefulness because products are used every day and have an effect on people and their well-being. Only well-executed objects can be beautiful.
  • makes a product understandable –It clarifies the product’s structure. Better still, it can make the product clearly express its function by making use of the user’s intuition. At best, it is self-explanatory.
  • is unobtrusive –Products fulfilling a purpose are like tools. They are neither decorative objects nor works of art. Their design should therefore be both neutral and restrained, to leave room for the user’s self-expression.
  • is honest –It does not make a product more innovative, powerful or valuable than it really is. It does not attempt to manipulate the consumer with promises that cannot be kept.
  • is durable –It avoids being fashionable and therefore never appears antiquated. Unlike fashionable design, it lasts many years – even in today’s throwaway society.
  • is thorough to the last detail –Nothing must be arbitrary or left to chance. Care and accuracy in the design process show respect towards the consumer.
  • is concerned with the environment –Design makes an important contribution to the preservation of the environment. It conserves resources and minimizes physical and visual pollution throughout the lifecycle of the product.
  • is as little design as possible –Less, but better – because it concentrates on the essential aspects, and the products are not burdened with non-essentials. Back to purity, back to simplicity.

 As Applied to Software Engineering…

Looking at the ten design principles, I’m sure you’re already seeing parallels between them and good software engineering.  We’ll go down the list and examine the meaning of each statement in the context of software engineering.

First, good software engineering is innovative.  At first glance, this might seem to support the idea of using the newest and freshest technology available.  However, looking at the rest of the rule, it specifies innovative technology can never be an end in itself.  It means, use the appropriate job for the tool and eschew the idea of using the newest technology if it doesn’t fit your problem.  Using an older library in a new way can be more innovative than just using the newest technology.

Secondly, good software engineering makes a product useful.  In the case of the software engineer, this is about easy-to-understand code (User Interface concerns fall more under UX/UI people although coding is involved there too).  This falls inline with many of the principles put forward by Clean Code by Bob Martin.

Good software engineering is aesthetic.  The design of the program (or programs) is clean and simple, like the codebase, it is easily understandable, contains enough abstractions to make sense and can be easily modified to encompass new requirements.  This is a tough point to understand, it’s hard to know when the proper amount of abstraction has been reached or know what is easily understood by others.  A few point to think about are, is the design simple?  Are you using queues instead of actors, data instead of syntax, and polymorphism instead of inheritance, switching, or matching?  Have you pulled apart the design to the base components?  Can you easily describe each part of the system?  Can you extend the design by simply removing one or two components and replace them with two or three new ones?

It makes a product understandable.  A follow on to the second and third points, the design and codebase of the program can be grokked by a new developer with little effort.  This isn’t to say it is going to be some work to look through the codebase, but it should be obvious how all the modules interact.  As a software developer, you should aim for simplicity — and note: simplicity does not mean easy :).

Good software engineering is unobtrusive.  Again, another principle of Clean Code, the codebase and design does exactly what it needs to do, no more no less, yet has the ability to be modified and integrated within another project easily and simply.  This leads to question of “how do we know the system can be modified easily and simply?”  If it isn’t, how do we fix that? And, does it mean a programmer new to the project can understand the code?  Unfortunately, this is a very interesting problem and deserves an article all to itself, but let me touch on this quickly.  In my opinion, a system can be modified easily when there are distinct breaks in modules in the code defined by interfaces that allow for good unit testing of each module proving the system is not overly coupled.

It should be honest.  Write and design simple to understand and modify code.  Consider everyone who is going to be working in the codebase, everyone should be able to understand, extend, and fix bugs within the code and adjust the architecture.  I’m not advocating a cowboy coder to come in and do a large refactor (e.g. rewrite) because in their mind it is cleaner, but again, it should be at the right abstraction at all points.  It also relates to the idea of unobtrusiveness with knowing when code is easily modifiable.

It is durable.  It uses the best tool for the job at hand; advocating code reuse and proven projects versus reinventing the wheel.  It means eschewing technologies not completely tested for your situation, opting instead for proven technologies and methodologies.  It means extending proven technologies where needed and using ideas from the bleeding edge, to make a quality product meant to last.

Thorough to the last detail.  The software’s design is meant for stability and ease of extension. The code base is well thought out and easy to understand.  The unit tests, functional tests, and integration tests express all (or as close to all) issues that can occur at the each abstraction layer.  There is sufficient documentation to understand, deploy, and modify the software.

Is concerned with the (computing) environment.  There are no memory leaks (yes, you can still have them in managed memory languages!), the algorithms are as efficient as possible for their purpose (run time vs. memory) and care is taken not to use too many resources.  The code itself is clean, straight-forward, and easy to understand.  This does not mean to go prematurely optimize and read all the papers on new, hot data structures, but it means to make good rational choice that can be explained and understood as a starting point.  As learn more about the problem and have integration testing and testing on the environment, you can begin to focus on the areas of code that need to optimized and understood better for a faster or more elegant solution.

Is as little design as possible.  As has been brought up in earlier points, the design focuses on the specifications, not the “what could happen” or “wouldn’t be cool if…” questions.  The problem is solved and designed in an elegant and easy-to-undersand way.  The code reflects this by being easy to read, understand, and modify.  You can think of this as, not being overly complicated when it comes to designing classes and methods.  Don’t use an enormous amount of variables unless you need them.


Good design principles can be applied anywhere design is used.  The definitions that Rams’ used were obviously questions he asked himself about his products, but it illustrates a point not laid out in his principles of good design:  you must constantly question yourself, your process, and of course, your design and implementation.

Additional Notes

I noticed multiple people have written about Rams’ design principles as they apply to software as well, you can read their articles here, here.  A wonderful talk about simplicity in design by Rich Hickey can be watched here (you really should watch it).

Additionally, I stayed away from a lot of questions that can come up. I know I didn’t touch on the idea of the right solution versus the correct solution which I’ve talked about a little before.  Also, I didn’t talk about what happens if you have conflicts between teammates on if you believe your implementation is good and they don’t, or even if your idea for the design of the software is different than someone else on your team.  These are all topics I’d love to talk about but they really deserve their own posts where we can delve into them in detail.

If you like, dislike, or have anything to say about the article, let me know in the comments below!

Is Java growing in the right direction?

Over at Vanilla #Java, there was an article showing Java’s popularity as a language compared to others and then it ranked them based on the level of abstraction at which they were designed.  The most popular language is C with 17.9% and next was Java with 17.4%, followed by Objective-C, C++, and C#.  So, of the top five languages, four are C or direct decedents of C and they all lower-level languages whereas Java is a higher level language.  He argues that although Java is adding lots of neat higher level features like Lambda expressions, Type Annotations, and Virtual Extensions they’re neglecting the ideas of structures and other lower levels of constructs that are important in areas like mobile and embedded systems which are likely to become more dominant in the future.

From my perspective, I think adding in more higher level constructs is a great thing as the cost of computation and storage is becoming negligible at this point for all devices.  It’s much more important to focus on making the programmers more productive since they’re the true overhead cost of product development at this point.  Now, I don’t mind Java supporting lower-level ideas like structs (I still enjoy writing C/C++ code) but I wonder about the time it takes to implement a lower level feature when hardware has Moore’s Law working for it.  Right now, I think Java (and C# for that matter) makes good trade-offs between high-level and low-level features to keep itself relavant in today’s market even if it is missing out on some of the embedded device market.

I’m completely ignoring the state of the alternative JVM languages like Groovy, Clojure, Scala, etc. which I think add some very nice possibilities to the JVM programmer.

If anybody is reading this and cares to comment on the state of Java, feel free to do so in the comments below. 🙂