Marty Andrews

artful code

Tuesday, October 31, 2006

Carbon Neutral in Australia

There's been a boom in awareness around environmental issues in Australia in the past 18 months or so. In particular, the local water crisis and the refusal of our Prime Minister to agree to the Kyoto Protocol have been big news. This week, I watched An Inconvenient Truth and was fascinated by the clarity and enormity of the global warming issue as presented by Al Gore. It triggered some discussion between my wife and I about going carbon neutral, so I began doing some research into it.

Today I offset the carbon produced by my house and car, effectively making a big chunk of my life carbon neutral. It was surprisingly cheap, costing only about $300 Australian dollars per year. To become carbon neutral, the simplest thing to do is to purchase carbon offset products. You measure the amount of carbon produced by your life using some simple calculators, and the companies that sell the carbon offsets make sure an equivalent amount of carbon is reduced from some other emissions. The David Suzuki Foundation provides a good description of carbon offsets and how they work.

Whilst the issue is a global one, I wanted to purchase carbon offsets locally to help raise local awareness. A quick search of the market found both Men of the Trees, who provide a tree planting program, and Neco, who also plant trees amongst other carbon abatement activities. Whilst tree planting is an important activity, it doesn't conform to the Gold Standard for carbon offset products, which badges them as being compliant with the Kyoto Protocol. Tree planting in particular is not a permanent change, and doesn't address the root cause of our dependance on fossil fuels.

Eventually, I found Climate Friendly, who sell carbon offsets that fund local wind farms in Australia and New Zealand. Wind farm technology does comply with the Gold Standard, so I was was happy that I was both supporting local awareness and offsetting my carbon output as best I could.

Thursday, October 26, 2006

Batch rename refactoring for IntelliJ IDEA

I've recently joined an existing project that has about 200k lines of code already in it, with no consistent coding standard applied. We decided that we wanted to consistently apply an underscore prefix for names of instance variables in classes. A quick grep through the source code told me that there were about 12000 (yep - thats twelve thousand) cases that didn't conform to the standard. I didn't want to do all of them by hand. IntelliJ IDEA has a feature called "Structural Search and Replace", but that will leave me with broken code. I really want a "Structural Search and Refactor". That doesn't exist, so I figured I'd build one.

I used the "Stuctural Find" feature inside IntelliJ to find all instance fields of a class. The template for that exists already. I set the regular expression on the field name to be [a-z][a-zA-Z0-9]* to make sure I didn't pick up things that already had the underscore, and ticked the box saying "This variable is target of the search". After running that, I get my 12000 or so results in the find results window, and I can use a hotkey to navigate back and forth between them. My cursor appears right on the variable I want to rename.

At this stage, I have a sequence of keypresses I can use to refactor each variable name,

apple - alt - down# go to next misnamed variable
shift - F6# rename the variable
home# go to the start of the variable name
_# insert an underscore
enter# complete the refactoring
apple - S# save the file

I tried recording an IntelliJ macro to record the events, but unfortunately the macros don't work across modal dialogs. The rename refactoring pops one up, so that didn't work. Instead, I downloaded an OS keyboard macro tool called Keyboard Maestro. I put that sequence of keys into it and had it repeat every second while IntelliJ was active. I left it running overnight, and when I got up in the morning I had 12000 renamed variables that all conformed to our coding standard. Excellent!

Monday, October 23, 2006

Consulting independence

I've recently left ThoughtWorks to take up the life of an independent consultant. After nearly a decade working for medium sized consulting organisations as an employee, its a new adventure for me. In my first role, I'm working with my good friends at RedHill Consulting. Pairing full time with them makes my brain hurt, but thats the way I like it. I'm also maintaining a close relationship with Cogent Consulting, and will be collaborating with them in an ongoing basis. If you think any of us can help you out, let me know :-)