WordPress Timezone Plugin – Handling Daylight Savings Time

I’ve been dealing with Daylight Savings Time issues at work. It’s a pain. I found out Hawaii and Puerto Rico don’t observe DST the hard way. It came to a surprise to me that WordPress that does not support Daylight Savings Time handling at all. I posted my last post and realized the time stamp was off by an hour. WordPressers have to manually update their blog settings to account for the hour(s) offsets from GMT whenever DST comes and goes. Luckily, there is a plugin that automates this process for us. Check it out here.

WordPress Timezone Plugin – Handling Daylight Savings Time

Ruby: ! versus not operator

I was writing some code in an erb template that looked like this:

<% if @user && not @user.errors.empty? -%>
#do stuff
<% end -%>

This resulted in a syntax error. Huh? What’s wrong with this?

Turns out it’s an outstanding Core Ruby bug.

As shown in the ticket if we have a method foo

def foo(parameter)
  puts parameter.to_s
end

using the ! and the not operator on various method calls show us that the ! and not operator are not interchangeable in practice.

foo(!(1 < 2)) # works fine
foo(not(1 < 2)) # generates syntax error
foo(not 1 < 2) # generates syntax error
foo((not 1 < 2)) # works fine

Changing the my code to the following fixed my issue:

<% if @user && !@user.errors.empty? -%>
#do stuff
<% end -%>
Ruby: ! versus not operator

MacBook Pro – Where’s my delete key?

I’ve had my MacBook Pro for a couple of months now and I’ve officially switched over to a MBP at work as well.  All this time I have been working without a delete key.  That thing labeled ‘delete’ on my keyboard is really what I have known in the past as a ‘backspace’ key.  Naturally, I assumed Ctrl+delete would give me back my true delete functionality, but that didn’t do it. It turns out the magic key combination is Fn+delete. My productivity should go up now! Yay!

MacBook Pro – Where’s my delete key?

Google TechTalk: Linus Torvalds on git

I came across this Google TechTalk with Linus Torvalds on git. I’ve never seen or heard Torvalds speak before. He certainly has a good sense of humor…or arrogance :).



It’s been almost a year since this tech talk and I still really haven’t seen large adoption of git. However after some painful experiences with Subversion, maybe it’s time to take a closer look at git. git sounds great in theory and design, but in practice I’m not yet convinced there is much value add.For example, the point that a truly distributed system allows you to make commits when you are unwired since the commits only go to your local repository, in my opinion, isn’t much value add. I am never really away from a wireless point or ethernet cord when I intend to code. The internet is just too much of a valuable resource for me to be disconnected when I’m coding, whether it be googling an obscure exception or reading online documentation.

The design of git is kind of reminiscent of what maven2 did for the build process and dependency management, with the idea of local repositories and selecting with distribution repositories you trust to pull down your dependencies.

I like the idea of the network of trust. I think that’s where the big win for git is. If I know a coworker is working on a piece of code and won’t be done until the end of the week, I know not to “trust” their branch until they tell me it’s ready.

git seems to encourage more communication between developers, which is great if the teams are practicing Agile software development. The idea of “Individuals and interactions over processes and tools” really needs to be instilled into the developers for git to work though. With so many branches around, there needs to be more communication between developers as to what features or branches are ready to be merged into the next release branch.

I guess only time will tell if git will be accepted by the masses.

Google TechTalk: Linus Torvalds on git

Breaking Java Habits: Ruby increment/decrement operators

My background in C++ and Java has instilled in me some programming habits that don’t play nice in Ruby. When I want to increment or decrement a number, my fingers instinctively go to ‘++’ and ‘–’’.

Unfortunately, this syntax is not supported by Ruby. As a language design choice, Ruby opted to not support this syntactic sugar to increment/decrement a variable.

I will have to break my habit and start going with:

// increment x
x+=1
// decrement x
x-=1

Another thing to note is that the prefix increment/decrement operators like ‘–x’ or ‘++x’ do nothing. The signs are interpreted as unary operators.

Breaking Java Habits: Ruby increment/decrement operators

Java WTF: Reflection API and Annotations

I got some exposure to custom Java 5 Annotations today and came across a noobie mistake.

Here’s my annotation:

public @interface Annotated {
}

Pretty simple, right?

Here’s a test class that uses the annotation:

import java.lang.annotation.Annotation;

@Annotated
public class Main {
    public static void main(String[] args) throws Exception {
        for(Annotation ann: Main.class.getAnnotations()) {
            System.out.println(ann);
        }
    }
}

What does this print?

Nothing!

Well, WTF?

Turns out Annotations are not run-time accessible unless you declaratively specific it via @Retention. The updated annotation looks like this:

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Annotated {

}

By default, the Retention is specified with RetentionPolicy.CLASS which, per the javadocs, “Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.”

RetentionPolicy.RUNTIME allows the annotation “to be recorded in the class file by the compiler and retained by the VM at run time, so they may be read reflectively.”

File this one under, RTFM.

Java WTF: Reflection API and Annotations

Java WTF: Calendar vs Date

I’ve been doing some date math at work and I came across something I thought was wack.

Check this out:

Calendar cal1 = Calendar.getInstance();
Thread.sleep(500);
Calendar cal2 = Calendar.getInstance();
Thread.sleep(500);
Date now = new Date();

System.out.println(cal1.before(cal2));
System.out.println(cal1.before(now));

What do you think this snippet prints (It compiles, I promise!)?

Looking at the code, it shows that we are getting three snapshots in time, waiting half a second between snapshots, and assigning those values to cal1,cal2, and now, respectively. I put in the Thread.sleep() to illustrate the point more clearly. The first print statement compares the first and second snapshots in time, returning true if the first is before the second. The second print statement returns true if the first is before the third snapshot.

Logically, we would expect both print statements to prints true. However, this outputs:

true
false

But, how could the first snapshot be before the second snapshot but not the third?

Putting up the javadocs, gives us our answer. The Calendar before and after methods always returns false if you don’t pass it a Calendar object. Why? I have no idea. Why not just have the method signature take a Calendar object? I mean, I guess this allows you to extend Calendar to be able to call after/before on whatever kind of classes you want to support…but does it really make sense to compare a Calendar to a Monkey class? This is what I call a Java WTF.

Java WTF: Calendar vs Date

Hacks, Tricks, and Techniques

I’m reading Out Of Their Minds – The Lives and Discoveries of 15 Great Computer Scientists by Dennis Shasha and Cathy Lazere. I’m almost half way through but I just read an awesome quote that I wanted to share:

In the culture of computer science, an idea that works in one situation is called a hack, an idea that works twice is called a trick, and an idea that works often and pervasively is called a technique.

Out Of Their Minds, 1998

Hacks, Tricks, and Techniques