I am writing a getter function, and the requested object can not be found. Do I return null? Or do I throw an exception? The answer is “it depends.” Sometimes both options are needed, but how to decide? Continue reading
Category Archives: Example Code
Variable Names & Key Values
Constructing JSON in Java can be a bother. The most standard JSON library requires objects to be created, and then called to put the members on there. So much easier in JavaScript, but it is not Java script. Still, one technique to improve the readability is to name the variable in java to match the key in JSON. Simple concept, easy to do, I sometimes wonder why it is not obvious to all. Continue reading
Brainless Getters & Setters are a Waste
Someone long ago set a pattern that all members should have a getter and setter method. Some are persuaded that this is OO and this is encapsulation, so a lot of inexperienced programmers do this by default. But this is a problem. Continue reading
Proper Stream Patterns
Java has streams for bytes, and streams for chars. Learn to use them correctly. It can be daunting at first, but if you just learn a few basic patterns, it all works well. Continue reading
Error Message should be clear, plain, and direct
This post is about a real error message I received and puzzled through. Luckily, because I was working with open source code, I was able to see a copy of the source, and only then did I understand what the message meant. An error message should not require you to know and understand the source, in order to understand the message, and I think this is the root of the mistake that many programmers make. Continue reading
#33 Eliminate Exceptional Cases before the Main Logic
When writing code, you naturally want the code to be readable so it can be maintained easily. Generally, you try to reduce the cognitive load, and one recommendation is to handle exception cases that might occur up front, in a small, localized block of code, before falling through to the main block of logic. Continue reading
#27 Don’t Declare Variables at the Top
Somewhere long ago you attended a course that said that all variables should be declared at the top of the method. Modern languages allow you to declare the variable at the point in the code that it is initialized and this is a significant advantage. However, the outdated idea of declaring all variables at the top of the method persists. This post explains why this is a bad idea. Continue reading
Working Around Java’s SSL Limitations
The Java run-time environment (JVM) is able to support connections to other servers using SSL, but it has this very inconvenient behavior of refusing to connect to self-signed servers. A self signed server has the public key necessary to ensure private communications, but does not have a certificate that proves who it is. In the default mode of operation, when connecting to such a server, Java SSL subsystem with throw an exception and prevent all communications. But there are good reasons why a service that you want to communicate with privately may not have a certificate, and this post tells you how to accomplish this. Continue reading