Electrotek

From programming to day dreaming

Half-marathon in 4 weeks? – Easy

Others might say it’s dangerous due to a high risk of injuries, etc., but for me it was a very pleasant experience. I had four weeks for my preparation: did 13 runs, 2 of them were more than 10 km long.

I did my first half-marathon at a relaxed, easy pace; finished with 1:51:28 (Update: real calculated time is 1:50:12). No injuries, no problems, just had a very good time :)

I have started thinking about doing a full marathon, will probably require_a_bit_more_preparation…

Save changes in Vim even if you forgot to sudo

If you forgot to run Vim with sudo and don’t have permissions to save a file, you can use a little trick from within Vim:

:w ! sudo tee % 

Write buffer [w] to external (system) command [!], the command is [sudo tee] and argument for tee is current filename [%]. Usually you would also redirect tee output somewhere (since it splits standard input by writing into argument file and standard output), in our case it could be tee % > /dev/null, but I don’t think it’s necessary here.

Probably a known thing, but anyway, love it, so just wanted to share…

JDK 7 new language features (Project Coin)

Tonight I’ve played a bit with JDK 7 small language changes. I find some of them quite handy.

The new stuff:

I’ve always missed this one – strings in switch:

String string = "foo";

switch (string) {
    case "foo":
        System.out.println("bar");
        break;
}

Readability counts; underscores in numeric literals and nice binary literals (I personally think that the underscores in number literals look a bit ugly/hacky/unclean):

long l = 1111_2222_3333_4444L; // I is card number
double d = 0_0; // I is cute, 1337h4x0r style!
byte b = 0b00101010; // I is binary

System.out.println(l); // 1111222233334444
System.out.println(d); // 0.0
System.out.println(b); // 42

Less code for generics, meet the Diamond operator:

List<String> list1 = new ArrayList<String>(); // Pre JRE 7
List<String> list2 = new ArrayList<>(); // Diamonds are forever

Multi-catch:

try {
    // Uuuuu scary stuff here...
} catch (Exception|Error e) {
    // http://ns.c2.com/cgi/wiki?PokemonExceptionHandling 
}

No tedious resource closing in finally block, less code and it looks much cleaner:

/**
 * AutoCloseable will close the resource when it's not needed.
 * 
 * @see http://download.java.net/jdk7/docs/api/java/lang/AutoCloseable.html
 * 
 * @param file
 * @return
 * @throws IOException 
 */
public static String readFirstLine(String file) throws IOException {    
    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
        return reader.readLine();
    }
}

Simplified varargs method invocation – haven’t checked this improvement yet.

On the side note – Ubuntu 11.04 and Unity are awesome on my Eee netbook, I’ve been checking Unity during development and I honestly thought it will not be ready for the release.

Operator Precedence and concatenation

Today a colleague of mine was having a problem with running a JPQL query which for some reason failed with a rather cryptic exception.

The code looked something like:

String query = "select s from SomeEntity e "
        + "join e.something s "
        + "where something "
        + variable == null ? "and something" : "and other";
System.out.println(query); // What is the output?

Even experienced developers were puzzled by this and we had a good laugh after the mystery was unsolved :)

Operator Precedence

Follow

Get every new post delivered to your Inbox.