Monday, December 31, 2012
Friday, December 28, 2012
Why Andriod is better than the other smartphone platforms
Apple iThingy: $99/year
Android: $0
If I'm trying to sell the app in your app store, then fine, make me pay the $99/year. But if all I want to do is write a silly little app and put it on my own phone to try development, or calculate my biorhythms, or lottery numbers, or whatever, then don't shaft me $99 for the privilege.
Friday, December 21, 2012
Google Car
While doing the school run this morning I was passed by one of those Google Self Driving Cars, just before arriving at Ben's school.
Apparently no one has programmed the car to observe the 25mph speed limit when students are present law yet, as it passed me while I was doing 25m/h. Some of you Googlers might want to get right on that little detail before you kill a 6th grader.
Still, it looks cool driving along with it's little turret spinning around on top.
Then a shiver went down my spine and I turned to Ben and shouted:
Oh, how we laughed ... well, I thought it was funny, but Ben just said, "Er, right. Crazy man. I think you can let me out here. I'll walk the rest of the way to school.".
Well, it was all a bit of fun. But now I just saw this, and I'm thinking maybe human enslavement by robots isn't too far off in the future ...
Thursday, December 20, 2012
The Web We Lost
- http://dashes.com/anil/2012/12/the-web-we-lost.html
Monday, December 17, 2012
Exception or Return?
When I first read about programming, I learned that "multiple returns from a function" were a bad idea.
I definitely agree with the sentiment. Most of the memory leaks I see in code are due to functions bailing, such as the example below:
int doWork(int c) { char *memory = (char *)malloc(BIGBLOB); if (!do_thing_1(memory)) { return -1; } int m = do_thing_2(memory); int e = m*c*c; free(memory); return e; }
In the example above (incase you didn't catch it) the memory
malloc
'ed on the first line of the function ewould be leaked if the call to do_thing_1()
returned false.The pattern I've adopted to avoid these kinds of memory leaks, and properly handle clean-up, is to use exceptions.
I define an error class that can be thrown at the various places within the code, then a single exception handler can interpret the value thrown and print an error message describing what went wrong. The code looks something like this:
bool doBigJob() { bool retval = false; char *memBig = NULL; char *b = NULL; try { int a = doThingOne(); if (-1 == a) throw (Problems::FIRST_PROBLEM); int s = getBigNumber(); memBig = new char[s]; b = (char *)doThingTwo(); if (NULL == b) throw (Problems::SECOND_PROBLEM); char c = b[a]; if (c == 'E') { // Arbitrary reason to abort! throw (Problems::THIRD_PROBLEM); } // Do meaty stuff... while (0 != s) { --s; memBig[s] = c; } retval = true; } catch(Problems::_problem p) { std::cout << "Failed for reason " << (int)p << std::endl; } catch(std::bad_alloc) { // This exception handler is required for new() std::cout << "Failed for bad alloc\n"; } // Clean-up, with null checks, // This is safe in the exception cases if (NULL != memBig) delete[]memBig; if (NULL != b) undoThingTwo(); return retval; }In the example above
Problem
conveys what went wrong. Actually, Problem
is simply an enum, as shown below:
namespace Problems { enum _problem { NO_PROBLEM = 0, FIRST_PROBLEM = 1, SECOND_PROBLEM = 2, THIRD_PROBLEM = 3 }; }This pattern is based on a technique I encountered in an old job, where they used GOTO to achieve a similar thing. Of course, people tell you you must never use GOTO, but I actually think the way it is used here is perfectly valid:
bool doBigJob() { bool retval = false; char *memBig = NULL; char *b = NULL; int a = doThingOne(); if (-1 == a) goto done; int s = getBigNumber(); memBig = (char *)malloc(s*sizeof(char)); b = (char *)doThingTwo(); if (NULL == b) goto done; char c = b[a]; if (c == 'E') { // Arbitrary reason to abort! goto done; } while (0 != s) { --s; memBig[s] = c; } retval = true; :done if (NULL != memBig) free(memBig); if (NULL != b) undoThingTwo(); return retval; }The thing is, I don't encounter my approach being used anywhere in code I read. People seem quite happy to use the dangerous multiple returns from a function approach. I know in Java, at one point, it was felt that using exceptions was too expensive and they should be used sparingly. But when I look at the code backing exceptions in C++, I don't think there's any real downside to doing it. I definitely think it is a cleaner way to handle these (exceptional) error cases in the flow of execution of complex functions. As my code sample suggests, you most likely need to deal with platform exceptions in many places, for example when using the
new()
operator.
I suppose another approach would be to simply propagate Problems
exceptions up to a higher layer. But another problem with C++ is that it's not clear what exceptions are going to get generated by a function, so it's not clear to me that this would make life easier for a caller of a function.
What I need is a good book on the subject. I wonder if this one of Herb's is any good?
Design-Code-Test-Ship-Design-Code-Test-Ship-Design-Code-Test-Ship-...
I realise it's a little bit slower than "Code-Ship-Code-Ship-Code-Ship-Code-Ship", but generally I've found it results in a more satisfactory outcome.
Thursday, December 13, 2012
Iranian comedy
If I were writing a sitcom in Iran, or Afghanistan, I think I would make a cartoon loosely based on the "Family Guy". But for a subtle twist I would call it "Daddy Jihadi".
It would be a gut bustingly hilarious cartoon about the daily life of a well manored, yet bumbling and ultimately unsuccessful fundamentalist terrorist.
Needless to say, his home life would be just as disappointing as his day at work. His wife would be a terrible woman, who went outdoors of her own volition, read science books and had girl friends over and complained about her husband.
He would probably have a pet goat that, unbeknownst to him, was a secret agent for Mosad and is vey sucessful at espionage and such like. Of course it goes without saying that the goat is also unaware that his master is the enemy who's plots are what he subverts every day.
And naturally each episode ends with Daddy Jihadi taking out his frustrations on the goat (in a method left to the imagination of the viewer , but it should result in much bleeting).
The goat probably would have some punchline about why his masters unfair behaviour legitimises his political views.
Anyway, that's the jist of "Daddy Jihadi ". Look out for it on Fox Sundays.
Friday, December 07, 2012
We are not amused.
In a statement, it said: "Police were called at approximately 9.25am on Friday, December 7, to a report of a woman found unconscious at an address in Weymouth Street, W1."London ambulance service attended and the woman was pronounced dead ...
Scotland Yard has launched an investigation and is treating the death as "unexplained".
Nothing unexplained about it. Liz sent in James Bond to rub her out. Hello, didn't you people see the Olympics opening ceremony!
If I were those Ozzie DJs, I'd be getting worried about now.
Politicing
http://m.guardiannews.com/lifeandstyle/2012/dec/07/david-cameron-christmas-card
There's no way that Milliband guy would get elected to power in my country. Look at his family photo, they're just too ugly!
Tuesday, December 04, 2012
dartmouth band at the los gatos christmas parade
Great pperformance on Saturday (December 1st) by Dartmouth Middle School's symphonic band. They won first prize for middle school band and went on to win the sweepstakes as best overall band.
They did a great job. The weather had been terrible all morning, but thankfully the sun came out and shone down on the parade route just as Dartmouth started to march.
Here's video of them playing after the parade outside Los Gatos high school.