Pages: 1 2 3 4 5

JUnit testing System.exit: A small framework

To add to my post on unit testing code that calls System.exit() I've wrote a little JUnit @Rule that allows you to set expectations in a simplified way.

The code is available on github.com/kay/assert-exit

Usage:

...

Read more

There are no comments on this post.

Testing code that calls System.exit in Java

It sometimes comes up in the course of unit testing where a unit of code calls System.exit(status). If you've a sane codebase this is normally within a main class. Consider an insane codebase:

Consider

public class DisgruntledPenguin {
    public static boolean happy() {
        System.exit(5);
    }
}

...

Read more

There are no comments on this post.

Detecting, locating and fixing referenced based heap memory leaks (Java)

A common problem we all have to deal with is fixing memory leaks. In garbage collecting languages like Java we typically expect not to ever need to worry about memory management. However the limitations of how a garbage collector works means that we can still create leaks. Which defeats the advantage of a garbage collector.

How do leaks get formed?

The Java GC determines which objects to collect by looking for references to the object. If there is no references, then the object is no longer in use and can be safely destroyed without making the application unstable. If an object is now unused but still referenced, a leak occurs. Let's create a leak:

...

Read more

There are no comments on this post.

Strong, Soft, Weak and Phantom References (Java)

There are four distinct forms of references in the JVM, and indeed many of these apply to other garbage collected languages.

  • Strong references
  • Soft references
  • Weak references
  • Phantom references

It's important to know the differences, what affect they have on the collector and when you should be using them.

...

Read more

There are 2 comments on this post.

Rename all jpeg files by their exposure date (Bash)

I like to organise my files by when they were taken. So I wrote a script to do it for me. You will need the jhead tool installed. On Debian/Ubuntu/etc this is in the jhead package.

#!/bin/bash
 
rm renameJPEG.restore.sh
 
for file in *.jpg *.jpeg *.JPG *.JPEG; do
        if [ ! -e "$file" ]; then
                continue
        fi
        echo "Inspecting $file"
        DateTime=`jhead "$file" | grep "Date/Time"`
        FoundCount=`jhead "$file" | grep "Date/Time" | wc -l`
        if [ $FoundCount -gt 1 ]; then
                echo "Found too many results for: $file"
                continue
        elif [ $FoundCount -eq 0 ]; then
                echo "No valid headers found."
                continue
        else
            BaseFile=`echo $DateTime | awk '{ print $3 }' | tr ':' '-'`
            for i in `seq 1000`; do
                if [ ! -e "$BaseFile - $i.jpg" ]; then
                    echo "New file: $BaseFile - $i.jpg"
                    mv "$file" "$BaseFile - $i.jpg"
                    echo "mv '$BaseFile - $i.jpg' '$file'" >> renameJPEG.restore.sh
                    break
                fi
            done
        fi
done

This will rename all the files to the format YY-MM-DD - i.jpg where i is a uniquifier. Note that if you've some files with bad headers then you will get the wrong output.

...

Read more

There are no comments on this post.

Web Designers vs Web Developers

It's been a long time. We deserve a post! Even if merely a link.

This is so true. Web Designers vs. Web Developers. Combat trousers are awesome. The principle reason I dislike suits is that there's never enough pockets. When you have more than 10 pockets in your trousers all of your development life, then you put on a suit and are restricted to 4. It the bane of my professional life.

...

Read more

There are no comments on this post.

Object pools (Python)

A very common problem in software engineering is optimising out the problem of construction and destruction of objects. I don't necessarily mean objects exclusively in the context of object orientated programming. I mean objects as in data objects, which could be a large array or a struct and so on.

If you don't need objects for a long time it does seem silly to construct an object, initialise it, then destroy it and discard it. Especially as memory allocation can be expensive.

The most common approach to minimising the impact of this is object pools. An object pool is a collection of pre-constructed objects, sometimes even pre-initialised objects. When a program wants to use an object for a short period of time they can check-out an object, configure it to their needs (if necessary), use the object and then check it back into the pool.

...

Read more

There are no comments on this post.

Brute forcing the touring knight problem

The touring knight problem is a maths problem that takes the movement of a knight in chess and attempts to find a path around a two dimensional board such that every single square is visited exactly once.

There are more sofisicated solutions but as you might have imagined, brute forcing is the most naive approach to solving this problem.

By trying every possible combination of legal move from your current position you can follow through every legal path and check whether you've completed the board or not.

...

Read more

There are no comments on this post.

Prototyping in Python

Our good friend Randall was talking about how he liked prototype inheritance such as that in JavaScript. He enjoyed how you can add, modify and remove methods from JavaScript classes and have them apply to all object instances.

Amazed as he was, he's now forcing me at gun point to write a post about it!

The first thing you have to understand about Python classes is that all they are is a collection of references to members. Methods are just function pointers that are referenced by the class, not embedded or copied into the class. This is why all methods require that the first parameter be self, the current instance. Instances themselves contain the real state and reference the class. When you invoke a method on the instance it looks up what function to call.

...

Read more

There are no comments on this post.

Ternary operator in Python

People are always wondering if Python has a ternary operator. The answer is yes but no.

There is no recognised ternary operator. However it is very simple to construct one out of logic operations.

Python short circuits conditional evaluations. This means that given a series of logic operations, if it can plainly see that a certain branch of that logic is unreachable or of no consequence it wont even attempt to execute it.

...

Read more

There is 1 comment on this post.

Pages: 1 2 3 4 5

Powered by Debian, Guinness, and excessive quantities of caffeine and sugar.