Monday 15 September 2014

False excuses for not writing unit tests


A few common reasons used to justify not writing unit tests.


1 - the developers are elite/rock-stars/gods and write code which does not need to be tested.

Let's assume for a moment the assertion above is true and that non-trivial code can be written so perfectly that is is bug free (unlikely...).  Even then the value of a test suite is not only about checking quality of the code when it is written. It's also about:
  • Preventing regression bugs when that same code is changed further down the line.

  • Documenting the behaviour of the application by demonstrating how the api should be used, and what results are expected for each use case. 

  • Ensuring quality of the design.  A program which has been written with testing in mind from the start will almost necessarily exhibit sound design characteristics: loose-coupling and high cohesion in particular. Otherwise testing becomes so overly difficult and messy that it is close to impossible.




2 - Unit testing slow us down.

The reasoning here is that time which is spent writing unit tests could be better spent on other tasks such as bug-fixing or adding new features to the app.  This is undeniably true.... in the short term. In the medium to long term all the code that has been churned without being properly tested is likely to break.

These breaks will be costly. At minimum there is a damage to the firm's reputation, not counting the time being spent debugging the issue and fixing the code. So it's a trade-off, speed vs quality of code. 



3 -focus on end-to-end tests (a.k.a system tests) rather than unit tests.

Since unit tests take time to write and maintain an alternative is to test at the highest level possible and simply check that the overall flow of the application is as expected, without testing the details of how the flow is generated. This is seen as a more efficient way of testing because these tests focus directly on the output of the application, which is what matters to the user. A few problems with this approach:

  • end to end tests are slow:  testing the whole application is order of magnitude slower than testing a single class, especially when there are dependencies to third-party systems involved (cost of network calls, database access...)

  • end to end tests are brittle: a chain is only a strong as its weakest link. Any change in the chain of components used by the application can alter the expected user output. Finding which component is responsible for the change in behaviour can be tricky to spot and debug.

  • end to end tests are complex, especially in a distributed environment where it is necessary to initialise each node or component to the specific state expected by the test.  In particular test static data needs to be created, and maintained in line with changes to the application logic. (think database reset between tests). 

  • also see arguments advanced above on point 1) - lack of unit tests also impacts the quality of the code, likelihood of regression and self-documenting feature.


4- Finally there are a few circumstances when unit tests are not strictly required:

- when building a prototype, maybe with the sole purpose of checking the technical feasibility of a new design and the code is bound to be thrown away.

- some features cannot be reliability tested at the unit-test level. Hardware failover, recovery from database failure, or more generally any scenario involving several machines, cannot be reliably tested by unit-tests running on a single machine and jvm.

Thursday 4 September 2014

Java rocks

There are hundreds of programming languages out there, what makes Java stand apart in 2014 ?


 - Java is everywhere: on mobile phones (Android apps are developed primarily in Java), on laptops and desktops, microcomputers (e.g. Raspberry pi). It runs on macs, linux and windows operating systems. It powers the business systems of large multinationals and startups alike. It is currently the second most popular programming language, or number one, function of your source.

 - Performance Measuring performance can be tricky as it's dependent on multiple factors (hardware, application code being measured, benchmark used to measure the performance to name just a few). The consensus though it that Java is in the same league (as fast if not fastest) as the fastest languages around, namely C and C++.

 - An evolving language Java is more than 10 years old but still evolving. The latest release Java 8 in March of this year brought a raft of changes such as lambda expressions, parallel operations, and a revamped date api.








Why not use Java ?
The most common criticism of Java is that it's a verbose language. Wether that's true or not is debatable, verbosity being largely function of the developer skills to start with. More importantly  verbosity is not readability. Most developers would prefer reading 10 lines of crystal-clear code than 1 line of terse and cryptic code, especially when code is being read much more often than it's written.