Don’t store references to boost::shared_ptr

Note: This entry has been restored from old archives.

I should have known better, in fact I’m pretty sure I did know better, but it is so easy to fall into the trap of using reference members as often as possible. Actually, in other code I’ve not done this, so I’ll call it a “typo”, yeah. 🙂

No time to go into details with examples and all that, best to keep things simple anyway.

Avoid storing auto/smart/shared pointer references in the name of avoiding premature optimisation. You always have to think: will this be destroyed before this is destroyed. If it takes more than 10 seconds to work that out then steer clear of the reference path! Hoping it’ll all be OK is asking for trouble. Especially if you don’t own the calling code!

A shared pointer instance stores little state, so just copy the damn thing. This way the target instance will be destroyed when nothing needs it anymore, which is the whole point. Better a few copies than having things vanish earlier than you expected because you tried to game the system.

My new personal rule is: always copy shared/auto/smart pointers.

If the copy is a problem it’ll show up in profiling later and that’s when you work out how to fix it.

(Alas there isn’t an easy way around avoiding circular references.)

Letchworth, another one bites the dust

Note: This entry has been restored from old archives.

Yesterday I took half a day off to inspect a place in Letchworth (it’s a 2 hour tube+train journey each way.) The place had much potential! Small but roomy enough for the two of us. In a bit a messy state, but that’d be professionally fixed before we got there. A smallish electric-top stove, but I found a gas connection behind it so we’d have the ability to put in something nice. A back yard to die for, seriously. Massive by UK standards, maybe 10m wide bu 30m deep, and in two parts with a dividing hedge. Clearly the back ⅔ was once the veggie garden but has been grassed over at some time. There’s a fruit tree in the middle of the rear section, probably apple, and blackberry vines throughout the hedging. The yard was a bit rough around the edges, but that’s exactly what I’m after – something I can play with without worrying about messing up manicured lawns.

The place would be dead quiet, set back far from the road (a cul-de-sac), seemingly surrounded by older folk, and with nothing but extensive gardens and greenery to the rear. Only a 590 metre walk from the station according to my GPS, walking through the central shopping district. The entire route open, well lit, and probably very safe.

Problem: No shower! Tiny little bathroom, but that’s OK. But no shower?! C’mon? Bloody English.

*sigh* So, good yet fatally flawed. It’d be an ideal purchase, a little work and you’d probably make up for any short-term value loss (unless things go really pear shaped.) In the long run it’d be a safe investment I’d say, thanks to the location. Not sure if it’d be freehold or leasehold, but if the latter it’d have about 900 years left as this is part of the original Garden City “demo village” built in the early 1900s. The building is certainly listed though, probably grade II, so there’s very little prospect of extension the place. It’s end-of-terrace so an end-side rear conservatory may be allowable (I think the place at the opposite end has an extension of the sort.) That’d provide for a better, more open, kitchen space I think. Also, maybe, a sympathetically designed far-rear-shed-cum-office could be possible. Gah, I shouldn’t be thinking about this stuff, depressing.

I’m trying to arrange to see another place this week. On paper it looks good. According to the agent I spoke with yesterday the area isn’t as nice, but should be safe. It’s twice as far from the station compared to the place I looked at on Monday, I timed the walk today: 10 minutes at a brisk pace (probably 15 for Kat.) Not too bad. In the evening I tried researching the street but could find no record of crime in the area and nothing else untoward. The main claims to Internet fame seem to be that a taxi driver is registered there and the Letchworth Buddhist centre was, until recently, located at one end. Being the location of a Buddhist temple is surely a good sign, right? Maybe we’ll find out.

I also tested out the commute to Kat’s work (The City) using the non-fast train since the direct LET-KGX trains only run during commuter hours. I went via train to Finsbury Park, then Victoria line to Kings Cross, then Northern to Bank. After walking from Bank to the vicinity of Kat’s office 1 hour and 10 minutes had passed. Not too much more than the current commute (barely over an hour.) However this was the slower-than-fast train, double-however the time was taken from Letchworth station and doesn’t account for walking from some other location in town. Overall it shouldn’t work out as too bad a commute though, not worse than the current arrangement anyway.

(As for Colchester, not possible for a couple of logistic reasons.)

Looking at Letchworth

Note: This entry has been restored from old archives.

Today we trekked to Letchworth and spent the day there. I’ve mentioned Letchworth before, the “garden city,” it’s one stop north of Hitchin on the Cambridge line. It has an interesting history, though I’ll leave it up to Wikipedia and various other sites to document the details.

What I have to say about it is: we’re keen on it. We’ve now seen Stevenage, Hitchin, and Letchworth close-up and have a good feeling for these original three moving options. Stevenage is out, we were very disappointed by both the old and new town centres. Picking between Hitchin and Letchworth is difficult though, they’re very different places. The main drawback of Hitchin is that it there is a 15 minute walk between the town centre and the rail station, which significantly limits the viable housing locations (must be somewhere between the two, essentially a 7.5-minute-radius circle.) In Letchworth the train station is central so the viable area is a 15-minute-radius circle around the town centre.

In a practical sense there isn’t much else to differentiate the two towns. They both have a large number of shops. Hitchin has, perhaps, a few more choices for the same things. Letchworth has a much larger supermarket, though we do try to avoid the things. They both have butchers that look good (judged by ample advertising of game being available on-season), acceptable coffee places, and some central green-space. Hitchin is much more historic, while Letchworth is greener and more open. This latter point is is probably Letchworth’s main attraction aside from the better transport arrangement. Hitchin’s opposing next-best major attraction is the large and regular market (3 days a week.) As far as the market goes though, Hitchin is only a 5 minute train ride away followed by a 15 minute walk (weekend trains seem to be every 30 minutes.)

Coffee is at least a little important of course. In Hitchin we didn’t see anything much in the way of coffee places other than Starbucks… could be worse. In Letchworth the only chain place we saw was Costa, which is about the same as Starbucks as far as espresso goes — the huge bonus of the Letchworth Costa is free wifi (Starbucks has wifi but it is stupidly expensive for casual use.) Neither of these options are really great on the espresso scale and won’t make up for the loss of Rickmansworth’s Cinnamon Square and Coffee Cube goodness. So, using Costa-wifi, we hunted down other coffee places. We found a couple of promising options and gave them a try (we actually found 3 but the 3rd seemed to not do coffee at all, that’s the ‘net for you.) The first seemed promising but turned out to be terrible, ick. The second didn’t seem promising, we thought we’d found a “caff”, but turned out to be surprisingly good. This latter place was Moon’s Café in “The Arcade” off Letchworth’s main drag “Leys Avenue.”

No doubt we haven’t tried all the coffee places in town. Also, according to the sites above and one of the agents we spoke with today, there are big plans for the “café culture” in Letchworth. Well, that remains to be seen and I can’t imagine it’ll be seen in a hurry. Promising though, the plans for the town centre do look good.

Some other differences include:

  • Rent is lower in Letchworth, maybe about 100 quid difference between equivalent places.
  • Despite being one stop further away from London, Letchworth has a slightly faster train service to Kings Cross. A 25-minute no-stop train every hour.
  • Real estate agents are friendlier, that’s probably just luck though. We dealt with 2 very pleasant agents today (Country Properties and Willows), but in Hitchin spoke with 4 that were either indifferent or even bordering on hostile and 1 that was very good (Norgans).

On the topic of agents, I’ve got all the way to here without mentioning how we went with looking for places today! The summary is: so/so. We looked at two places. The first was about 15 minutes walk from the station (our limit) and can best be described as “poky.” It also smelt of cigarette smoke, a smell you generally have to replace the carpet and repaint to get rid of. The second place was an apartment, shitty kitchen, no outdoors space, and on the ground floor. In other words: the exact opposite of what we’re looking for (we only looked at it because it was on the way back from the previous one.) The only thing these two places had going for them was price, one was a full £400 a month less than our current rent – for good reason though.

It’s not all bad, there are two decent looking options but one requires 24 hours notice before inspection and the other isn’t ready for inspection yet. Hopefully I’ll be able to arrange to see them sometime next week. They’re both semi-detached and available in the right time-frame. One is more central being only 2 minutes from the shopping area and 10 from the station (opposite side of shopping area.) The other is a bit further out, 15 minutes from both the shops and the station. However this latter one seems nigh on luxurious and, on paper, seems to be our pick.

In the end I think we’ll be focusing on Letchworth for our move, it’s far easier to target just one town. Time is ticking away, it’s now less than a month before we have to be out of our current place.

Bjarnterview

Note: This entry has been restored from old archives.

If you have an interest in C++ (and CS/SE education) it is highly worth your time reading this interview with Bjarne Stroustrup.

Choice quote:

“Learn to use the language features to solve simple programs at first. That might sound trivial, but I receive many questions from people who have “studied C++” for a couple of weeks and are seriously confused by examples of multiple inheritance using names like B1, B2, D, D2, f, and mf. They are—usually without knowing it—trying to become language lawyers rather than programmers.”

The linked JSF++ and Performance TR are likely to be worth reading too, but with both being well over 100 pages I’ve only had time for a brief skim.

Computer God

Note: This entry has been restored from old archives.

I’ve had a string of thoughts that require deeper consideration. The thoughts start with:

There is a god, her name is Hope.

And eventually reach:

If computer intelligence doesn’t get religion then the Singularity cannot occur.

There’s a heck of a lot between those two points, I’m not sure I’ll ever be able to translate it from strings of thoughts to strings of words.

What do wine and tech have in common?

Note: This entry has been restored from old archives.

Answer: an epiphytic marketing industry.

More specifically: a plethora of meaningless awards and certifications and the companies and organisations responsible for them.

Go to the wine section in your supermarket and you’ll see dozens of bottles with little silver, gold, or bronze “medals.” Read the labels and find out what awards they’ve won. Often it’ll be “best” of some ridiculous niche category like “best merolt-shiraz blend from the west side of Dead Man’s Hill.” Seriously, many of them are about that precise, covering all of 3 or 4 wines. That’s if they explain anything about the award at all, in other cases it may just have a year and the name of some unheard-of wine show, or grand-sounding “challenge.”

In my years of drinking wine I’ve come to the conclusion that there is very little relationship between awards and my enjoyment of the wine. However there is some relationship between the awards and the price of the wine. As far as I see it you’re better off going for cheaper wines with no awards. It’s hard to remember this sometimes, we grow up instilled with such a strong sense that everything must be ranked that this “medal” technique hammers right into our subconscious bypassing rational thought.

The technology sphere has a similar system, whereby a plethora of publications, organisations, and dodgy websites give out awards like they’re going out of fashion (I wish.) Many of the awards will have dozens of categories that really only contain 3 or 4 competing products. Sometimes even less since some of these schemes require you to pay up to be considered (and I expect many wine awards are the same.) These rankings are usually of little technical merit, even often judged by non-experts based on marketing material rather than any practical results. Techies may dismiss them yet, disturbingly, they can be an important part of selling a product. The inexpert are easily swayed by these seeming ticks-of-approval, as with wine it can be exactly this sort of meaningless ranking that gets you short-listed in the mind of your customers (most products are not sold to experts.)

Certifications are similar. Take IGT/DOC/DOCG in Italian wine for example, a set of rules that define how you must make your wine if you want to market it in certain ways. It seems as if you’re claiming some guarantee that your wine attains a minimum standard of quality. In reality the only real meaning is that the wine complies by a set of rules that grant it the acronym and, like the awards, enjoyability bears little relationship to the certification. The fact is that mere Vino Da Tavola wines (“table wine,” the term for uncertified wine) seem to be as good a bet if you just want to enjoy a glass. Further, most people don’t know what all the classifications mean anyway! Quality is rarely certified, typically the most you can read into it is location, grape blend, and process – in the hope that prescribed bounds increases the chances you’ll enjoy your wine.

In technology systems exist that are much the same. (Also for people, but that’s another issue.) Some are tick-box standards or rule systems, like the currently hyped SOx and PCI. The problem with these is that compliance really only means that you’ve “gone through the motions,” there’s no guarantee that you actually care or are pro-active about the problems the systems supposedly address. Another form of certification is “product X achieved Y with system Z” – any tech person I know looks at these things and shrugs, sometimes muttering an expletive. It’s a funny old situation where successful companies are created around products that give certifications that the entire base of technical experts in the relevant field dismiss as not being a whole lot of use! But the fact is that, while they’re rarely a useful assessment of the practical effectiveness of a product, they’re often an important cog in the marketing machine. People want benchmarks, people want ratings – how else can they judge the quality. The underlying problem is that there are few experts but many buyers.

Where the whole thing goes most horribly wrong, in the tech industry at least, is when this system starts feeding back into itself. Regulations are created that spawn whole technologies, such as SOx and PCI. We get technology specified by the regulations rather than focused on solving the problems the regulations were invented to address. Defacto-standard rankings are created that measure a quantity that isn’t actually a useful part of a technology’s function. We expend vast engineering effort tuning technologies to do well in the rankings rather than addressing real problems. Successful companies are born from the precise specification and measurement of the wrong things!

It is a little reassuring that no number of unreliable or misleading guarantees makes a crap product a good one and that’ll be the stumbling block for many solutions riding a wave of medals. Alas, it is much harder for the inexpert to know the technology they’ve bought is crap than it is for the inexpert to know the wine they’re drinking tastes like copper coins. (This doesn’t matter so much for wine, there are many buyers and you can get by just fine if you never sell a second bottle to the same person. You typically have much more restricted markets with technology and rely on people coming back for more.)

I started typing this up with the intention of jotting down just a couple of paragraphs comparing wine medals to technology awards. Now it’s >1000 words later and I’ve left many loose ends flapping around in my mind. Someone who knew enough about the industry, marketing, and human psychology could probably write a decent paper (or book) on this. “Technology Defined by Marketing” or, more scathingly, “Selling Widgets to Idiots.” I expect it’d probably be boring and wouldn’t change anything anyway.

[[[FYI: The root thought occurred to me as I sipped an insipid and tinny French red that had a silver medal. “Silver Medal International Wine Challenge” it says, it’s also from a ‘Cru’ status village, and is AOC (of course.) We have a lot of seeming guarantees that this is going to be a damn fine drink. Wrong!]]]
[[[P.S. I don’t think all awards and certifications are meaningless. I also don’t think they’re all (or even mostly) the product of unethical exploitation of the expertise vacuum. To narrow down the more suspect ones look out for those that involve the exchange of money, especially where that money goes to a for-profit entity.]]]
[[[P.P.S. Yes, I have read that the introduction of wine standards certifications in Italy, France, and Spain improved the average quality of wine. Alas I am not so old as to have known wine back before the systems were introduced. As a consumer in the modern wine market (and we’re taking 3 or 4 different wines a week) my observation thus-far is that at a given price level certifications (or awards) don’t mean much when comparing my enjoyment of wine. Further, I’ve drunk several non-certified wines in both France and Italy that are far above the average enjoyability. I’ve even had an French wine producer rant at me about how much he hates AOC since his best wine is always a single-varietal and thus cannot qualify. (If you’re only familiar with “new world” wines (i.e. Australia) you probably don’t have a clue what I’m on about, since the “new world” mostly sticks to naming wines based on the grapes that made them and single-varietals are common. Anyway, there’s a lot more to these “old world” systems than I have time to go into, there are some good justifications for AOC/etc.)]]]

Mr Chicken said

Note: This entry has been restored from old archives.

Mr Chicken said …

I have a feeling that the author of this SMH article is taking advantage of the situation here, despite of the seriousness of the news item. Beginning 4 sentences with “Mr Chicken said” seems just a little too deliberate.

John Chicken is a rather unfortunate name though. At least he’s probably going to most be addressed along the lines of “representative for the defence,” I assume (or whatever the proper court-speak is), and if he gets to Judge it’ll be plain old “your honour.” Doesn’t change the fact that he’d be Judge Chicken though.

Further notes on C++ lambda (N2550)

Note: This entry has been restored from old archives.

Since my earlier post (content here should mostly make sense independent of the post) I’ve taken some time to explore the C++ lambda proposal further, specifically document N2550 [pdf] that fully defines the proposal.

The first note is that the “In a nutshell” example used to illustrate the use of the method is not reassuring. First they introduce the functor:

class between {
  double low, high;
  public:
    between(double l, double u) : low(l), high(u) { }
    bool operator()(const employee& e) {
      return e.salary() >= low && e.salary() < high;
    }
}
....
double min salary;
....
std::find if(employees.begin(), employees.end(),
                between(min salary, 1.1 * min_salary));

Then they provide the lambda equivalent:

double min salary = ....
double u limit = 1.1 * min_salary;
std::find if(employees.begin(), employees.end(),
                [&](const employee& e) { return e.salary() >= min salary && e.salary() < u limit; });

This seems to be the same example that the Beautiful Code blog picked up on.

I have to admit however, this is growing on me just a little. Maybe like mould growing on damp bread.

To dissect:

The [&] introduces the lambda expression, in this case the inclusion of a lone & indicates that the closure created will have reference access to all names in the creating (calling) scope. This is why the code can use min_salary, if the lambda was introduced with only [] the code would be erroneous. You can, and possibly should in most cases, specify exactly what the closure can access and in this case that would be done using [&min_closure]. The normal meaning of & is retained here, the variables the closure accesses are references to those in the enclosing scope — for better safety in this case it is maybe better to use [min_closure], this will ensure the closure will not modify the original value (consider is passed to the closure by value.)

I see a pitfall here, the non-& capture means the closure can access values by the enclosing name and modify them but the modifications will not propagate to the enclosure. It wouldn’t surprise me if, for clarity and readability, the & form (pass by reference) becomes standard and the default form (pass by value) becomes “not done.” I feel it may be preferable for the “pass by value” to actually be “pass by const reference.”

The (const employee& e) gives the “parameter declaration” of the lambda. Essentially we’re declaring a function, or functor, that takes a single const reference to an employee as an argument. Note that this mirrors the operator() of the between functor. There’s more to the declaration than is given here, it can also sport an optional exception-specification (um?) and a return type. Let’s skip the exception specification since I don’t feel up to the task of trying to explain that one. (I suspect it is because the result of a lambda is under the hood a functor and it isn’t insensible to export this part of the definition to the user rather than enforcing no specification.) The return parameter is likely to be more regularly interesting, in this case it is not given but is actually -> bool – why? Because of paragraph 6 of 5.1.1, which essentially says that the return type is defined by the type of the argument to the return statement in the lambda.

The lambda expression itself, or code of the closure if you prefer, is between the { and }. That part is clear enough I expect.

The return type is defined by the top-level expression of the return statement in the expression. This is &&, clearly enough the return type is a bool.

Read the document [pdf], it’s worth the time – it’s a short document.

So?

Um, still not sure, but seeing all that is specified by the document does open up the possibilities much further. I said earlier that I think this lambda syntax has the potential to make code clearer where the function-object (or binder) code replaced achieves a simply expressed end. On further consideration I think this is especially the case where the expression is not easily described with a reasonable functor name (i.e. the functor approach for “greater_than” is fine and possibly better than the lambda equivalent.) The question, for maintainability, becomes: is:

    between(min salary, 1.1 * min_salary))

better than:

    [&](const employee& e) { return e.salary() >= min salary && e.salary() < u limit; });

Personally I still don’t see enough of a win to the latter syntax to justify the extension. Saving the code that defines the functor doesn’t look like a big win and the latter code, while more explicit at place-of-use, doesn’t seem so stupendously more clear to justify the extension either.

What if…

What if the extension allowed for creation of closures more in keeping with their much-hyped use in other languages? Would I, monkey boy, be convinced then? Quite possibly. Scroll down to Chapter 20 in the document and peruse “Class template reference_closure“.

Looking interesting. As I see it the reference_closure definition will permit the creation of instances-of-lambdas (closures.) These can be returned from a class as “closures” giving very specific restricted write access to the internals of a class. Vague shadows of how this could make the design of code I’ve worked on in the past more straightforward are beginning to form. That’s far from an indication of usefulness however, the shadows aren’t any better than “deformed rabbit” right now. I’m still a bit confused as to the lifetime of variables local to the scope of the enclosing function, as opposed to member variables, in relation to an exported “closure.” Especially since the closure template defined seems to restricted to reference-only closures.

I don’t have time to explore deeper at the moment, but it’s certainly food for thought and brings this C++ lambda/closure extension more in line with the hype around it in other languages. (Kat’s work has recently jumped on the closure bandwagon (best thing since Agile?) and it seems that their picture of closures in Perl very much mirrors what would be achieved by this reference_closure. At the same time… it is also achievable with everyday functors, but maybe the new specification makes it all easier and more accessible? Perhaps that is the question.)

The point of all this text is: RTFM [pdf]. Herb Sutter’s post is a quick-n-dirty illustration of simple things that can be achieved using the specification — he refers to the specification itself for your further investigation. I’m envious that he had an implementation that will compile his lambda examples, presumably VC++.

[[I use “closure” and “lambda” almost interchangeably here, however I’m far from confident in my use of this language. Reading up on this online does not help much as there’s a gap between the CS definitions of the terms and many commonly blogged use-cases. In practise it seems a lambda is an in-place definition of function code, what we often refer to as an “anonymous (unnamed) function”, that can be passed down to called functions (and is most often seen defined in the place of a parameter interchangeable with a function reference.) Practice again seems to define a closure as inline function code that can access and modify values in the scope where it was defined but can be returned to and invoked by calling code, this matches fairly well with the wikipedia definition of the term. YMMV.]]