Naming is hard – or is it?

Phil Nash from level of indirection

Following Peter Hilton's excellent ACCU talk, at last week's conference in Bristol, "How to name things - the hardest problem in programming", a few of us were discussing some of the points raised - and some not raised.

He had discussed identifier length without any mention of Uncle Bob's guideline, whereby the length of a variable name should be proportional to it's scope (i.e. large or global scopes need longer, descriptive, names whereas in smaller, local, scopes shorter, more concise - even single letter - names are appropriate). This seemed all the more of an omission given that he later referenced the book, Clean Code.

It wasn't that Peter disgreed with Uncle Bob (who doesn't, half the time?) that surprised me but that he didn't even mention it in passing. I thought it was fairly well known. Actually I double checked and it is not discussed fully in the book, which only says, "The length of a name should correspond to the size of its scope". This is expanded considerably in Clean Coders (video) episode 2. Also, of course, this is not really "Uncle Bob's rule". Kevlin Henney recalls that he first heard of it in the 90s and it may well have been kicking around before that. Bob calls it "The Scope Rule".

Kevlin was one of those discussing this afterwards. After initially toying with The Scope Rule in the 90s he came to consider it not particularly useful. This, too, surprised me as I had found it worked quite well for me. Or so I thought. Further discussion with Kevlin led to the conclusion that I had read more of my own interpretation into The Scope Rule than I had realised! So I started musing over exactly what my interpretation was.

A transparent reference

As it happened a concept key to clarifying matters came from another great talk at the same conference just the day before. Didier Verna's "Referential transparency is overrated". In this talk Didier discussed various ways that useful idioms in Lisp required violating referential transparency. At one point he explained how "hidden" variables may be introduced by one macro that were then used by another. This worked because the thing being referred to was named very generically - so both macros agreed on the name. He drew on the term "Anaphora" from linguistics, which is where one part of an expression - usually a pronoun - stands in for a more specialised part - such as a person's name - introduced earlier in the context. For example, just now I used the word, "he" to refer to Didier Verna. It was clear who I was talking about because his was the most recently specified name in the current context. In fact I used this anaphoric term a couple of times - and many, many, times in this article. If I had had to fully qualify "Didier Verna" every time writing would quickly become very cumbersome. Anaphora is used very frequently in natural language - usually to good effect.

Scope Creep

I believe this is key to understanding why and when shorter identifiers can be used too. When I had been talking with Kevlin it had become apparent that we had different interpretations of the word, "scope". I realised that I had subconsciously expanded the specific technical meaning to include a more general idea of "context" - including the anaphoric context.

To make this clear I might write some (C++) code like this:

	std::string s = getNextString();
	if( !s.empty() )
		std::cout << "received string: " << s << std::endl;

Many corporate, or personal, coding standards would balk at such practice! Single character identifiers? Way to obfuscate the code!

But how has it obfuscated anything? Look at it as an anaphoric entity. In this case the variable name 's' is anaphoric. We know it is "the next string" because we saw it being introduced by the function call, "getNextString()". We then use it twice on the next couple of lines. There are no other strings being introduced in the same vicinity to confuse it with, and the context in which it is used is kept small. There is no ambiguity and the full identity is revealed in the immediate vicinity.

Sustainability

But what if we add more code, or move parts of this elsewhere? Certainly code evolves over time in ways that can make things less clear if we don't change them. That's true regardless. Naming of the entities at play should always form part of your consideration when refactoring or otherwise modifying existing code. Does it make this code less "sustainable" (to reference another property that Kevlin likes to talk about)? I don't think so. In the worst case, if you don't immediately notice that a short name has become unclear because it's usage has drifted out of anaphoric range, you'll notice the next time you look at it and, momentarily, think "why is there an free variable called 's' here? What on earth is that. You'll take a moment to find it's original declaration, work out what it is, then decide to encode that in the name by renaming the variable at that point. Variable renaming is one of the safest and most ubiquituous refactorings around so I have no qualms about deferring such identity expansion to such a time as it is needed.

Why?

But what about the other side of the argument? Is there any advantage to using a short, even single character, variable name in the first place?

This is often cast as a matter of optimising for typing speed - in world where we typically read these names many many times more than we write them.

While introducing, even small, speed bumps to writing code might discourage spending more time than necessary writing code (which in turn may discourage certain refactorings) it's not really about typing performance at all - It's about readability! Consider again the linguistic definition of anaphora: substituting an, unambiguous, subsequent reference to an entity with a shorter form (e.g. a pronoun) that means the same thing. We do this all the time in natural speech and the written word. Why? Because it would sound unnatural and cumbersome to fully qualify every entity we talk about all the time!

The same applies in programming. Where it is perfectly clear from the immediate context what an identifier refers to then using greater verbosity actually increases the cognitive friction! The more unnecessary and redundant noise and ceremony we can strip away from our code the easier it will be to read, in a shorter period of time. That fact that anaphora is so common in natural language should give us a clue as to our ability to code with it's use in a natural and efficient way.

Now I've only mentally organised my thoughts around this as a result of ruminating on those two talks - and some of the offshoot discussions - but I realise this is essentially how I had interpreted The Scope Rule. Now I've worked it through when I go back and compare it with what Mr Martin actually said his version sounds like a poor proxy for the anaphoric interpretation.

So naming - good naming - is still hard. We've only just discussed one narrow aspect here. But perhaps this has made some of it that little bit easier.

ACCU Conference 2015

Products, the Universe and Everything from Products, the Universe and Everything

We spent last week at the ACCU Conference in Bristol having our brains filled with gloopy tech goodness. It was (as ever) a real blast.

chandler_carruth presenting 'C++: Easier, Faster, Safer' at ACCU 2015

We had our demo rig with us as usual, and a steady stream of folks came along to chat to us and acquire caffeine from the expresso machine on the table next door. We came back absolutely exhausted, so no detailed photoblog this time I'm afraid!

However, for me the highlight was Chandler Carruth's closing keynote C++: Easier, Faster, Safer, in which he talked about how Google were using Clang and LLVM to (among other things) perform large scale automated refactoring (cue lots of furious scribbling...)

The synopsis of the keynote says it far better than I could:

Over the past five years, the prospect of developing large software projects in C++ has changed dramatically. We have had not one but two new language standards. An amazing array of new features are available today that make the language more elegant, expressive, and easy to use. But that isn't the only change in the last five years. LLVM and Clang have helped kick start a new ecosystem of tools that make developing C++ easier, faster, and safer than ever before.

This talk will cover practical ways you can use the tools we have built in the LLVM and Clang projects. It will show you what problems they solve and why those problems matter. It will also help teach you the most effective ways we have found to use all of these tools in conjunction with modern C++ coding patterns and practices. In short, it will show you how to make *your* C++ development experience easier, faster, and safer.

Next year's conference is provisionally scheduled for 19th-23rd April. See you there!

ACCU Conference 2015

Products, the Universe and Everything from Products, the Universe and Everything

We spent last week at the ACCU Conference in Bristol having our brains filled with gloopy tech goodness. It was (as ever) a real blast.

chandler_carruth presenting 'C++: Easier, Faster, Safer' at ACCU 2015

We had our demo rig with us as usual, and a steady stream of folks came along to chat to us and acquire caffeine from the expresso machine on the table next door. We came back absolutely exhausted, so no detailed photoblog this time I'm afraid!

However, for me the highlight was Chandler Carruth's closing keynote C++: Easier, Faster, Safer, in which he talked about how Google were using Clang and LLVM to (among other things) perform large scale automated refactoring (cue lots of furious scribbling...)

The synopsis of the keynote says it far better than I could:

Over the past five years, the prospect of developing large software projects in C++ has changed dramatically. We have had not one but two new language standards. An amazing array of new features are available today that make the language more elegant, expressive, and easy to use. But that isn't the only change in the last five years. LLVM and Clang have helped kick start a new ecosystem of tools that make developing C++ easier, faster, and safer than ever before.

This talk will cover practical ways you can use the tools we have built in the LLVM and Clang projects. It will show you what problems they solve and why those problems matter. It will also help teach you the most effective ways we have found to use all of these tools in conjunction with modern C++ coding patterns and practices. In short, it will show you how to make *your* C++ development experience easier, faster, and safer.

Next year's conference is provisionally scheduled for 19th-23rd April. See you there!

ACCU Conference 2015

Products, the Universe and Everything from Products, the Universe and Everything

We spent last week at the ACCU Conference in Bristol having our brains filled with gloopy tech goodness. It was (as ever) a real blast.
chandler_carruth presenting 'C++: Easier, Faster, Safer' at ACCU 2015
We had our demo rig with us as usual, and a steady stream of folks came along to chat to us and acquire caffeine from the expresso machine on the table next door. We came back absolutely exhausted, so no detailed photoblog this time I'm afraid! However, for me the highlight was Chandler Carruth's closing keynote C++: Easier, Faster, Safer, in which he talked about how Google were using Clang and LLVM to (among other things) perform large scale automated refactoring (cue lots of furious scribbling...) The synopsis of the keynote says it far better than I could:
Over the past five years, the prospect of developing large software projects in C++ has changed dramatically. We have had not one but two new language standards. An amazing array of new features are available today that make the language more elegant, expressive, and easy to use. But that isn't the only change in the last five years. LLVM and Clang have helped kick start a new ecosystem of tools that make developing C++ easier, faster, and safer than ever before.

This talk will cover practical ways you can use the tools we have built in the LLVM and Clang projects. It will show you what problems they solve and why those problems matter. It will also help teach you the most effective ways we have found to use all of these tools in conjunction with modern C++ coding patterns and practices. In short, it will show you how to make *your* C++ development experience easier, faster, and safer.
Next year's conference is provisionally scheduled for 19th-23rd April. See you there!

Groovy: Baby Steps

Tim Pizey from Tim Pizey

Posting a form in groovy, baby steps. Derived from http://coderberry.me/blog/2012/05/07/stupid-simple-post-slash-get-with-groovy-httpbuilder/


@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
@Grab(group='org.codehaus.groovyfx', module='groovyfx', version='0.3.1')
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.Method
import groovyx.net.http.RESTClient

public class Post {

public static void main(String[] args) {

def baseUrl = "http://www.paneris.org/pe2/org.paneris.user.controller.LoginUser"

def ret = null
def http = new HTTPBuilder(baseUrl)

http.request(Method.POST, ContentType.TEXT) {
//uri.path = path
uri.query = [db:"paneris",
loginid:"timp",
password:"password"]
headers.'User-Agent' = 'Mozilla/5.0 Ubuntu/8.10 Firefox/3.0.4'

response.success = { resp, reader ->
println "response status: ${resp.statusLine}"
println 'Headers: -----------'
resp.headers.each { h ->
println " ${h.name} : ${h.value}"
}

ret = reader.getText()

println '--------------------'
println ret
println '--------------------'
}
}
}
}

GNU Emacs 24.5 on (X)ubuntu 14.10

The Lone C++ Coder's Blog from The Lone C++ Coder&#039;s Blog

GNU Emacs 24.5 was released on April 10th. I’m in the process of setting up a dual boot Windows/Linux machine right now as I’m slowly moving away from Mac OS X, mainly because of the cost of the hardware but also because I don’t like it that much as a Unix-y development environment anymore. Xubuntu 14.10 only comes with Emacs 24.3 and it looks like 15.04 will “only” include 24.4 so now is a good time as any to manually install 24.

Symantec sold a C++ compiler?

The Lone C++ Coder's Blog from The Lone C++ Coder&#039;s Blog

Stuff you find that shows you’ve been around this programming business for a while: Original copy of Symantec C++ (née Zortech C++) 6.1 Most people these days are surprised that Symantec actually sold a C++ compiler at some point. I used this particular copy in my first, not very successful business venture - I started out using Walter Bright’s Zortech C++ compiler which eventually morphed into Symantec C++.

Speaking: ACCU 2015

Pete Goodliffe from Pete Goodliffe

I'm pleased to announce that I'll be delivering the opening keynote at the awesome ACCU 2015 developer conference in Bristol this April. The talk is called "Becoming a Better Programmer" (it's no coincidence that this is the same title as my new book and my magazine column).

I'm really looking forward to it. I think it'll be great fun and, hopefully, really useful.

The session synopsis is:
You've come this conference to improve your skills. You're here to learn: to learn new technologies, to learn new techniques, and to fuel your passion by meeting like-minded people. 
Becoming a better programmer means more than just learning new technologies. It means more than practising techniques and idioms. It's about more than passion and attitude. It's the combination of all these things. That's what this session will look at. 
Pete Goodliffe, author of the new book Becoming a Better Programmer, unpacks important mindsets and techniques that will help you improve as a programmer.
You'll discover specific tools that will help you review your current skillset, and you'll learn techniques to help you “become a better programmer”.

More info about my session is available here.

Videos: MPC Sneak Peeks

Pete Goodliffe from Pete Goodliffe

In my day job I am the software lead for Akai's MPC product line. This is a product I'm immensely proud of, and I'm very proud of the the work the software team puts into this iconic music controller.

Over the last few months we've been producing in-house videos for the new releases we've been baking here at Akai towers.

We recently released the latest in this series for the 1.8.2 update. Check it out on YouTube here:

MPCv1.8.2 sneak peek

If you fancy seeing the whole set, we have also produced videos for:

The more recent of these have been shot completely in-house; I purchased a Canon 70D and some lights, and rigged up a Heath Robinson autocue. It's all shot in our development offices. Usually unnecessarily late into the night.

We love creating these videos, and connecting directly with the passionate MPC users.

Delete Jenkins job workspaces left after renaming

Tim Pizey from Tim Pizey

When renaming a job or moving one from slave to slave Jenkins copies it and does not delete the original. This script can fix that.


#!/usr/bin/env python
import urllib, json, os, sys
from shutil import rmtree

url = 'http://jenkins/api/python?pretty=true'

data = eval(urllib.urlopen(url).read())
jobnames = []
for job in data['jobs']:
jobnames.append(job['name'])

def clean(path):
builds = os.listdir(path)
for build in builds:
if build not in jobnames:
build_path = os.path.join(path, build)
print "removing dir: %s " % build_path
rmtree(build_path)

clean(sys.argv[1])