Skip to content

Blog

Breaking static dependencies using Java 8 lambdas

Static dependencies are usually bad , because that dependencies are based on the class level and class objects can not be replaced at runtime in contrast to object instances. That’s why static dependencies are usually a pain if it comes to tests. When you want to write a unit test you usually want to mock the dependent objects, but you can’t do that if the dependent object is a class. Timing is for example hard to test, because it is based on the static call to System.currentTimeMillis(); and the result of currentTimeMillis changes all the time. Of course it does, because it’s the nature of time but that makes it also hard… Read More »Breaking static dependencies using Java 8 lambdas

Global java vm arguments

When you start a Java virtual machine you can specify various vm arguments. Some alter the memory management, some the garbage collection and some are system properties. But you can also specify global vm arguments that are picked up by every virtual machine that starts within an environment context. These global options are specified using the _JAVA_OPTIONS environment variable. _JAVA_OPTIONS=-Xmx1024m When you start a JVM while the _JAVA_OPTIONS environment variable is set you will see something like Picked up _JAVA_OPTIONS: -Xm1024m on the command line. The JVM tells you that it has found global java options and picked it up. Useful _JAVA_OPTIONS use cases Certificate management In a lot of… Read More »Global java vm arguments

One dot per line saves time

Today I want to introduce you to a simple rule that I apply when developing software. This rule often saves me a lot of time and it is simple to apply. I usually write code in Java and so the rule is made for Java programming in the first place, but it might also be adapted to other languages. I call that rule One dot per line The dot character in Java is primarly used for dereferencing. This also means that it can lead to NullPointerExceptions. The nature of NPEs is that they contain few context information. java.lang.NullPointerException at org.apache.commons.lang3.time.FastDateParser$TimeZoneStrategy.<init>(FastDateParser.java:869) at org.apache.commons.lang3.time.FastDateParser.getLocaleSpecificStrategy(FastDateParser.java:637) at org.apache.commons.lang3.time.FastDateParser.getStrategy(FastDateParser.java:606) at org.apache.commons.lang3.time.FastDateParser.access$100(FastDateParser.java:73) at org.apache.commons.lang3.time.FastDateParser$StrategyParser.letterPattern(FastDateParser.java:234) … This is primarily a… Read More »One dot per line saves time

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

Progress Object Pattern

When a user executes a long running background task an application usually wants to report progress about the task to the user. Application developers often do this by passing a ProgressListener to the background task’s  method and the method uses the ProgressListener to report it’s progress. But you can also design it in another way and I would like to introduce another pattern here. At least I would like to show an example of how to implement a progress object pattern with Javas Swing. The complete example code is available at gitthub: https://github.com/link-intersystems/blog/tree/master/progress-object-pattern. But before I introduce the progress object patten let us take a look at the progress listener concept. Progress listener A progress listener is conceptually a callback that… Read More »Progress Object Pattern

A plug-in architecture implemented with java

Instead of building monolithic applications developers prefer modular and extendible applications. Either they want to use the flexible architecture by themselfs or provide a plug-in api for other developers. In both situations one must define a modul’s purpose and it’s boundary. This module boundary is also called the service provides interface (SPI) or also a plug-in api. Java’s jar file specification describes how service providers can provide their services so that others can lookup them. This pattern is known as the service locator pattern. Since Java 1.6 the ServiceLoader has been added in order to make service provider lookup easy for application developers. Prior to Java 1.6 applications developers had to implement the lookup on their own based on the jar… Read More »A plug-in architecture implemented with java

Enums as type discriminator anti-pattern

In Java 1.5 enums were introduced to ease handling of constant values. Enums can improve code quality, because they are types that the compiler can check. But they can also be misused and lower the code quality. In this blog I want to discuss the misuse of enums as type discriminators and show you why this kind of usage is an anti-pattern from an object-oriented perspective. Type-switches What I call a “type-switch” is just any conditional statement that does type checks in order to execute type specific code. Here are some examples of type switches. HumanState humnanState = …; if(humanState instanceof HappyState){ singHappySong(); } else if(humanState instanceof SadState){ singDirge(); } HumanState humanState =… Read More »Enums as type discriminator anti-pattern

Singleton implementation pitfalls

In this blog I want to show implementation pitfalls of the singleton pattern and show you why enums are the better singletons. The singleton pattern is widely known and discussed a lot of times. Developers often prefer to implement a singleton using a final static field. But this approach has some pitfalls or rather malicious code can compromise the singleton pattern. In the next sections I will show you the possible attacks on singleton implementations that compromise the pattern. Maybe you will use this techniques to implement a kind of workaround some day, but I hope you will not. Static Final Field Singleton The purpose of a singleton is that only 1 instance of a… Read More »Singleton implementation pitfalls

Instanceof vs. polimorphism

An instanceof check is often an indication for misplaced source code. Some developer say that instanceof checks are ugly code, but there are reasons against instancof checks beyond a subjective feeling. In this blog I want to compare instanceof checks with polymorphism and discuss the downsides. This blog is intended to be used as a basis for desing decisions and discussions about code design. Instanceof motivations Everytime we write an instanceof check in java we are interessted in the capabilities of an object. We want to know if an object has specific features or methods or if it has not. This can also be implicit features that are derived from the type. In this case we have 2 options: Introduce… Read More »Instanceof vs. polimorphism

GDPR Cookie Consent with Real Cookie Banner