Refactoring Deeply Nested if Statements

Deeply nested if statements are a pain to read and maintain, so you should try to avoid them as much as possible. Here are a couple of suggestions on how reduce deeply nested if statements to a more manageable size.

Most of these suggestions can be applied to most languages that have the if statement, at least most object oriented or procedural languages. But my main focus when writing this were Java and PHP.

Early Return

Early returns should be used a lot more often by pretty much everybody. It’s one of the easiest ways to avoid or reduce deeply nested if statements and one of the first things I would look for when refactoring code.

The main idea is that when checking a value, the negative case is moved up-front, the error handled, and then the method is exited:

boolean test1 = ...;
if (test1) {
    boolean test2 = ...;
    if(test2) {
        boolean test3 = ...;
        if(test3) {
            // do the stuff
        } else {
            // some error, handle it
        }
    } else {
        // something else
    }
} else {
    // some other error, handle it
}
return;

Here, you can move the checks to the top of the method and remove three levels of nested if statements:

boolean test1 = ...;
if(!test1) {
    // some other error, handle it
    return;
}
boolean test2 = ...;
if(!test2) {
    // something else
    return;
}
boolean test3 = ...;
if(!test3) {
    // some error, handle it
    return;
}
// do the stuff
return;

Refactoring Simple Boolean Checks in if-Statements

How to refactor simple boolean checks in if statements and avoid bad practices. This is the first post out of a number of posts dealing with refactoring. This particular post is aimed at beginners, but further posts will contain somewhat more advanced concepts. The concepts presented here can be used in a lot of programming languages, but the article is written with Java, PHP, and JavaScript in mind. For one-liners, the general formating will look like this:

Bin Packing Algorithm (Java)

In this post I will present example Java code for solving the Bin Packing Problem. I will not go into what exactly Bin Packing is, as there are enough websites that do a good job describing it. As always, wikipedia is a good start to get a general overview: Wikipedia Bin Packing Problem. Bin Packing: Brute Force Solution Bin Packing: First Fit Decreasing Algorithm Download Source Code for Bin Packing Problem Bin Packing: Brute Force Solution To check all possible solutions for the bin packaging problem (brute force), a recursive approach can be used: Iterate over all bins, try to put the current item in the bin and – if it fits – call the same method with the next item.

Linux Mint: Remove PPA

Linux Mint does not provide the option to remove a ppa. If you try it, this will be the result: sudo add-apt-repository --remove ppa:somePPA/ppa Usage: add-apt-repository [options] repository add-apt-repository: error: no such option: --remove But you can still manually remove a ppa in Linux Mint by removing the file that defines it. All ppa are defined in /etc/apt/sources.list.d . So you can do the following: # find the correct ppa file to delete by listing all ppa files: ls /etc/apt/sources.

Comparison of Free UML Tools

There is a lot of different UML modelling software to choose from. Here, I will create a short overview over the most common free uml tools. This list is not meant as an exaustive review, but more to give you an idea what each of the tools can to so you have an easier time choosing the uml tool that is best for you. Gaphor ArgoUML Dia Violet UML Editor UMLet yEd Papyrus Modelio Further free UML Modelling Software What’s the best free UML tool?

Linux: Installing Ruby on Rails

The official Ruby on Rails documentation has a good guide on how to install Ruby on Rails. I still had some problems setting everything up, so here I will describe how I installed Ruby on Rails on Linux (LMDE). Installing Ruby on Linux Before installing Ruby on Rails we need to install Ruby as well as Ruby Gems and sqlite: sudo apt-get install ruby rubygems sqlite3 Installing Ruby on Rails on Linux sudo gem install rails -V -V because otherwise you will get no direct feedback.

Installing QGIS on LMDE (Linux Mint Debian Edition)

The solution that I found for installing qgis (Quantum GIS) on LMDE (Linux Mint Debian Edition) is not optimal, but the only one that worked for me. I am getting QGIS directly from the QGIS Debian repository and I am temporarily adding the main debian repository to my sources, because QGIS depends on some packages that are not included in LMDE. LMDE: Install QGIS Add QGIS to your sources.

Linux command line Image Manipulation for Android Developers

Sometimes while developing Applications for Android one has to transform a lot of images at once. In those cases, it is easier to use the command line than standard programs such as gimp. Resize multiple Images via Command Line for i in *.png; do convert "$i" -resize 50% "${i%%.png*}.png"; done I use this a lot when developing for Android, as it is best to supply differently sized images for different devices to save memory.

Java – Image Manipulation

Handling Images in Java is fun. Here are some often used image manipulation methods. Negating colors of an image Change all pixels in BufferedImage to one color Changing opacity of image Adding two BufferedImages (with given opacity) Saving BufferedImage to a file Loading a BufferedImage from a file Most of these methods come from my java heatmap creating class so if you want to download them get them there.