Skip to content

Java

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

Clean Code with underscores in literals

As of java 1.7 the java language specification allows integer literals to contain underscores in order to make them more readable. If it is applied in a good way it can enormously increase the readability of your code. In java 1.6 one had to define integer literals like this: int iterations = 10000000; long bytes = 0b11010010011010011001010010010010; With java 1.7 such integer literals can be written more readably by using the underscore as a  thousands separators. int iterations = 10_000_000; or when using binary literals one can use the underscores to group bytes long bytes = 0b11010010_01101001_10010100_10010010;   Further information can be found at http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1 Recommended Reading  

The MVC pattern implemented with java swing

MVC Pattern Basics The MVC pattern describes a way to organize the components of a graphical user interface. Therefore it must cover all aspects of a user’s interaction with an application. If we take an analytic look at a graphical user interface we will understand why the MVC pattern is composed of the 3 elements model, view and controller. Model A model is an abstraction of something that you want to present the user in a view. Therefore there are two stereotypes of models: UI-model A ui model is an abstraction of a ui component’s state. Therefore it has properties like: focus, enablement, selection and so on. A good example… Read More »The MVC pattern implemented with java swing

The difference between pojos and java beans

Java Beans and Pojos are often used and misunderstood terms. Some think that a pojo is a java bean and vice versa. In fact it is not. There are differences that matter. The pojo If you want to know what a term really means you have to find the source context in which the term has been created. So for the term pojo it was originally created by Martin Fowler, Rebecca Parsons and Josh MacKenzie at a time when J2EE had it’s hype. At that time J2EE developer were more busy with technology instead of concentrating on the business value. The problem arised because J2EE requires a lot of techology code in your… Read More »The difference between pojos and java beans

GDPR Cookie Consent with Real Cookie Banner