Lexical vs. dynamic scoping

In this blog I want to give you some examples about the difference between lexical and dynamic scoping. I will not dive into the details. You will find a lot of other resources about it. Here is a small list: Scope (computer science) Javascript — Lexical and Dynamic Scoping? What is lexical scope? The following JavaScript and Bash examples are almost equivalent in structure. Nevertheless they will lead to different results, because of the different scoping type these languages use. Lexical scoping – e.g. JavaScript In languages with lexical scope (also called static scope), name resolution depends on the location in the source code and the lexical context (also called static context), which is defined by… Read More »Lexical vs. dynamic scoping

Explicit access permission design with user roles

Most applications need access control and must therefore implement user permissions is some way. In a lot of the projects my colleagues asked me how I would implement user permissions. Since a lot of them found my thoughts an interessting way of implmenenting user permissions I want to share my thoughts here with you too. Maybe you can benefit from some or all of them or even create a completely new design. Annotation based permissions Usually you will implement permissions using annotations, but there are pitfalls and issues common to all annotation bases permissions. Annotation based user permissions code will often look like this: @Roles(“admin”) public class SomeClass { public… Read More »Explicit access permission design with user roles

How to test randomness?

In this blog I want to show you one way of how to test randomness. Randomness often appears when programming games. Let’s say you have a class that uses randomness, like a dice. When someone throws a dice it can be in one of six states 1,2,3,4,5 or 6. Thus you might implement the dice using a Java Random like the next example code shows. Can you test that class? A natural reflex is to say: “No, you can’t test it, because a test always makes an assumption about the result. Since the result is random you can never make an assumption that will always come true. Thus the test… Read More »How to test randomness?

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>( at org.apache.commons.lang3.time.FastDateParser.getLocaleSpecificStrategy( at org.apache.commons.lang3.time.FastDateParser.getStrategy( at org.apache.commons.lang3.time.FastDateParser.access$100( at org.apache.commons.lang3.time.FastDateParser$StrategyParser.letterPattern( … 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: 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