Skip to content

Design

Make method pre-conditions explicit by design

A method usually makes assumptions about the arguments that are passed into it by a method invocation. The conditions that the arguments must fullfil are named the method’s pre-conditions. Thus a good method implementation checks if the arguments are in an expected state so that the method can execute. Often developers use basic data types as method parameters like String, Integer and so on, but a method usually doesn’t allow every possible value that these types can take. E.g. a method might declare an integer as parameter, but only accepts positive integers. Such a method might validate it’s pre-condition in this way: public double divide(double dividend, double divisor){ if(divisor == 0){ throw new IllegalArgumentException(“division by 0 is not… Read More »Make method pre-conditions explicit by design

A type-safe named query design approach for JPA

The starting point In a JEE you normally use the Java-Persistence-API JPA to access the data. JPA provides named queries which are statically defined queries with an unmodifiable query string. Because the query string is unmodifiable a client must still be able to provide parameters that concretize a query. Those query parameters are placed in the query string in a special format, e.g. :username. So if we define a named query in the simplest way it will look like this: @Entity @NamedQueries( { @NamedQuery( name = "findOrdersByUsername", query = "select distinct o from Order o JOIN order.user AS u where u.username = :username") } ) public class Order { ...… Read More »A type-safe named query design approach for JPA

Handling complex object state with design

Some APIs that implement a complex object state management often violate the single responsibility principle and make the usage very difficult. When you do all aspects of the complex state management in one inferface the interface’s methods must be invoked in the chronological correct order. Such APIs are hard to understand and to integrate. What we really want are APIs that guide the client code programmers in such situations. Lets take a look at an API that breaks the single responsibility principle and let us think about the consequences for the client code programmer. If you take a look at the FTPClient API of apache commons you can see that all… Read More »Handling complex object state with design

GDPR Cookie Consent with Real Cookie Banner