Archive

Archive for April, 2009

Lazy People Make Progress

April 22nd, 2009 Comments off

Progress isn’t made by early risers. It’s made by lazy men trying to find easier ways to do something.

-Robert Heinlein

This came up today on the ‘Quote of the Day’ box on my iGoogle page.

It strikes me as being very true, and very applicable to many software developers. It’s not that we don’t work very hard; but it’s that that we’re so lazy that we will often work very hard to save ourselves the effort of having to work on stuff we hate doing.

Someone, like me, who is a compulsive programmer, would much rather spend an hour writing code to automate a boring task than spend half an hour actually doing the task manually. Even if we’re only ever going to have to perform the task once!

There’s some great stuff about compulsive programmers in Computer Power and Human Reason by Joseph Weizenbaum, written way back in 1976, but full of very useful insights into today’s world even so.

It’s Optional

April 19th, 2009 Comments off

What’s the best way to store and retrieve optional settings or preferences for users for your C# application? For simple stuff, there’s no easier way than creating settings in Visual Studio:

Settings dialog

Note that you can use settings for both simple values like bool and string, and for more complex values like colors.

These settings get persisted in a file called [your program name].exe.config.

And you can refer to them in code like this:

    private void GetBackgroundColor()
   {
         this.BackColor = Properties.Settings.Default.BackgroundColor;
    }
 
    private void SetBackgroundColor(Color backcolor)
    {
          Properties.Settings.Default.BackgroundColor = backcolor;
          Properties.Settings.Default.Save();
    }

Well, that’s all very well, simple, and very useful. But it gets messy if you want to set up a lot of options and particularly if you need options using your own enums, in various categories or for different states or objects in your application – the Settings dialog is just a very simple, line by line set of options with no structure.

My own preference* is to create one or more Options classes, and to use the nifty Xml Serialization stuff which I talked about previously on this blog to load and save the class.

Here’s a considerably cut-down version of my Options class from my software Chapter Master:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Options
{
	public enum ImageGrabOptions
	{
	    None,
	    CropFromTop,
	    CropFromLeft,
	    CropFromRight,
	    CropFromBottom,
	    CropFromCentre,
	    Pad
	}
 
	public enum SaveOptions
	{
	    None,
	    RenameOriginal,
	    PromptForNew
	}
 
	[System.Xml.Serialization.XmlElement()]
	public ImageGrabOptions ImageGrab = ImageGrabOptions.CropFromTop;
 
	[System.Xml.Serialization.XmlElement()]
	public SaveOptions SaveOption = SaveOptions.PromptForNew;
 
	[System.Xml.Serialization.XmlElement()]
	public int PadColorAsInt = Color.White.ToArgb();
 
	[System.Xml.Serialization.XmlIgnore]
	public Color PadColor
	{
	    get {
	        return Color.FromArgb(PadColorAsInt);
	        }
 
	    set {
	        PadColorAsInt = value.ToArgb();
	    }
	}
 
	public Options()
	{
	}
 
	public bool Save(string filename)
	{
	    return Seralize.ToXmlFile(filename, this);
	}
 
	public static Options Load(string filename)
	{
	    return opts = (Options) Seralize.FromXmlFile(filename, typeof(Options));
	}
}

Where Serialize is a class containing just the Xml serialization routines I wrote based on this Code Project article.

Note the way that the Color is serialized in lines 27-40, the same sort of workaround as is needed for TimeSpan values.

I like this approach because among other things, it allows different sets of preferences to saved and loaded as required.

Accessing these option values is very simple (this code is a fudge written just to demonstrate my general approach):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private readonly string MySettingsFolder = 
      System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
      + @"\ProgramName";
 
private OptionClass MyOptions;
 
private void GetOptions()
{
     MyOptions = Options.Load(MySettingsFolder  + @"\Default Options.xml");
}
 
//....and thus code like:
 
picCover.BackColor = MyOptions.PadColor;
if (MyOptions.ImageGrab.CropFromBottom)
{
     //........
}
 
private bool SaveSpecialOptions()
{
    return MyOptions.Save(MySettingsFolder  + @"\Special Options.xml");
}

* I’m happy to have others tell me why there are better ways. As always, I certainly don’t claim to be any kind of expert. This is just the way I like to do it. If you think it’s dumb, tell me so.

Categories: Programming Tags: , , ,

Recent Reading

April 18th, 2009 Comments off

My fortnightly summary of what I’ve been reading and listening to.

While I’ve been reading a fair bit over the last fortnight, I haven’t completed very much in the period.

I’m part way through:

  • Trunk Music by Michael Connelly (Audible audiobook)
  • The Pillars of the Earth by Ken Follett (Ebook on my iPod)
  • Decline and Fall of the Roman Empire Vol I by Edward Gibbon (Ebook on my iPod)

I confess that I’m reading Gibbon’s massive treatise on the iPod just to prove that it can be done, and how well the iPod Touch/iPhone works as an ebook reading platform, something I’m growing increasingly to believe. Even Gibbon’s extensive footnotes work pretty well thanks to intelligent formatting by Gutenberg (from where I sourced the book).

On the down-side, Lexcycle, who produce the Stanza ebook software I had been using to read books on my iPod, dropped the ball. They released a new version incorporating a dictionary lookup feature which manages to interfere with the comfort of reading (the feature pops up if your finger dwells a fraction of a second too long on the screen when you are turning pages). They have promised to fix it, but in the meantime I’m using the almost-as-good eReader from Palm.

Friend of the Devil  by Peter Robinson.

Ebook on my iPod Touch.

I grew up in what is now West Yorkshire in the United Kingdom. Robinson was born not far away from where I was born, and only a year before me. His series of novels about Detective Chief Inspector Alan Banks are all set in this part of the world, so many of the places he writes about are quite familiar to me from my childhood. This, of course, adds to the interest I have in this series.

But even if you don’t know this area of the world, DCI Banks is an engaging and multi-layered character with a complex private life, and the cases he encounters are full of interest and mystery. In this novel, the 17th in the series, a young woman is raped and murdered in the town of Eastvale; and in what seems a completely different case, a woman quadriplegic is found murdered in her wheelchair at the top of a set of cliffs facing over the North Sea. How these two cases – one handled by Banks, one by his colleague and ex-lover Annie Cabot – are related only becomes clear as the book progresses. A really intriguing read, and the ending was not at all obvious for almost all of the book.

Various Blogs

Here are some links to a few of the blogs I read regularly:

Coding Horror  by Jeff Atwood

This is always must-read stuff for me. Jeff Atwood talks intelligently and interestingly about the craft of programming, and continually introduces me to new thoughts, and links to things I ought to know or to think about.

I, Cringely  by Robert X. Cringely

Cringely wrote one of the best, and funniest, books about the early days of the computer industry which I have ever read: Accidental Empires (or, How the Boys of Silicon Valley Make Their Millions, Battle Foreign Competition, and Still Can’t Get a Date)

He writes regularly and very intelligently about technology. While he’s occasionally a bit too self-important and self-congratulatory for my taste, he’s never less than thought-provoking and well-informed.

Whimsley  by Tom Slee

This British-born Canadian doesn’t blog anywhere never enough so far as I am concerned. He writes very clever and amusing stuff, sometimes at great length, about the digital economy. For example, he dedicated dozens of well-thought-out posts to demolishing the book The Long Tail by Chris Anderson; and he has written amusingly about the hidden flaws in the way that Google and Amazon work.

I hope Slee keeps on blogging, because I want to keep on reading his stuff.

Journal  by Sam Pepys

This guy blogs just about every day, and it’s all full of his rich life in London, all the stupidities and corruption of the politicians and bureaucrats that he has to work with, about all the women he bonks (he’s a very naughty man!), his long-suffering wife, and the renovations he’s having done to his house. Just lately, he’s been rather worried about the spread of a dangerous infectious disease in the city, seemingly on the rise every day. And about the progress of the current war with the Dutch, of course.

Fascinating reading. Oh, did I mention that this guy is writing in the 1660s?

The Infernal Engine

April 12th, 2009 Comments off

The devil is in the details....

The Infernal Engine

(First published in October 1995)


It came to me only recently that spell-checkers are the invention of the Devil.

No, seriously.

Well, an invention if not of the cloven-hoofed gentleman, then certainly of someone or thing whose intent is the Destruction of All We Hold Dear.

It was General Jack D. Ripper, I think, who informed us that fluoridation of drinking water was all a dirty commie plot to Pollute Our Precious Bodily Fluids. These days, with the end of the Cold War, I would hesitate to point the finger in the same direction, but I ask you, have you ever heard of a Russian spell-checker? You bet your sweet babushka you haven’t.

I came to this revelation only recently, I must admit. But it all came clear to me when I was reading an article in “The Age” newspaper, on the concerns that parents have about the apparent decline in their children’s reading and writing skills. The article reported (unidentified) teachers as claiming that it was no longer necessary to teach children how to spell. “After all,” they went on, “there’s always the spell-checker”.

That was bad enough, but then I went on to read that the “whole-learning” approach depended on children learning to read, not by spelling out the sounds, but by learning to recognise the whole word in context, using “picture queues” and other non-verbal evidence.

Picture queues? Putting aside images of portraits lined up to get into a gallery, I puzzled over this for a long time, until I realised that what was meant were “picture cues”, that is, hints from illustrations. The article, I realised with a sense of impending dread, had been spell-checked!

“Queues” is a perfectly good word, of course. It would have passed the spell-checker easily. But it was the wrong word, and entirely the wrong meaning in the context of the sentence. A queue is something long and boring, the epitome of inaction. A cue, on the other hand, as in a billiard cue, is something which prompts something else into action.

A perfect example, in other words, of why spelling is important, and why spell-checkers are the invention of the Devil, or of someone who admires him.

Later in the same edition of the newspaper, in an article discussing ethics, we see “principle” used incorrectly instead of “principal”, greatly confusing the sense.

No newspaper in Australia now employs a proof-reader. The profession of proof-reader is going the same way as that of gas-lighter. Archaic, unnecessary, old-fashioned. Yet its loss will leave us all in the dark.

The problem with spell-checkers, of course, is that they don’t check spelling. They merely have a long list of combinations of letters which are known to be valid English words; and the best they can do is to report that a certain combination is not in its list. This entirely negative check does pick up some mis-typings of course, otherwise no-one would use spell-checkers. But as any Scrabble player knows, there are some very weird combinations of letters which are perfectly valid English words. Worse still, people can add their own words to the list in the spell-checker, if they find the checker regularly picking them up and questioning them. Ah, blessed relief: no more annoying “Ignore, Change, Add?” messages from the spell-checker. But the result is, that over time, the list of accepted spelling combinations in the checker gets longer and longer, and fewer and fewer mis-spellings are actually identified.

Spell-checkers would still have their uses even so, but because of sheer laziness, or for short-sighted economic reasons in the case of the newspapers, no-one who has run a spell-checker over an article feels any impulse or duty to read it to pick up any other errors, let alone to check for sense and coherence. The result is that the quality of journalism (never very high) is plunging rapidly. One frequently these days comes across sentences or phrases in published material which simply do not make sense, or which are so badly mangled that one has to give up in disgust. So no example is set to young people of good writing, and so the standards of literacy continue to drop into the abyss.

A friend of mine recently tried to convince me that within five years keyboards will be a thing of the past: all computer input will be through voice-recognition.

“But what about words which sound the same, but have different spelling?”.

“Oh, artificial intelligence will sort all that out,” he said airily. “Besides, we have spell-checkers.”

The trouble with artificial intelligence, of course, is that mostly it is artificial stupidity: dumbness repeated over and over again a million times to achieve a result. Like trying out a million possible moves in a game of chess. Like checking every word against a vast list of possibilities.
But the worst thing of all about spell-checkers is that I reckon they are going to halt the process of spelling reform. Lord knows, the English language is full of odd and non-phonetical spellings: that’s probably why we need spell-checkers in the first place. Laugh, cough, bough, though. Night and fright. Photograph.

Without the invention of the spell-checker, there was at least some hope that over a period of time English spelling would become more rational: the Americans have at least made a start on it. But if my spell-checker, based on an Australian dictionary, rejects “center” in favour of “centre”, “color” in favour of “colour”, and refuses to play along if I attempt “nite” or “foto”, what hope is there for spelling reform? We are all going to be locked into late 20th Century spellings of the particular country we base our dictionaries on. The process of slow shiftings in spelling will just stop.

Similarly, old, obscure or esoteric words will drop from use because the spell-checker won’t let them through – try using a great word like “logodaedaly”, for example, and have the Microsoft Word nanny peremptorily interrupt you and tell you to correct that word it has underlined in red for you, you naughty person!

To close, I can’t do better than to quote from Ambrose Bierce’s The Devil’s Dictionary, written long before spell-checkers, but presaging their invention:

Lexicographer, n.

A pestilent fellow who, under the pretense of recording some particular stage in the development of a language, does what he can to arrest its growth, stiffen its flexibility and mechanize its methods… Let the dictionary (for example) mark a good word as “obsolete” or “obsolescent” and few men thereafter venture to use it, whatever their need of it and however desirable its resoration to favor – whereby the process of impoverishment is accelerated and speech decays. On the contrary, the bold and discerning writer who, recognizing the truth that language must grow by innovation if it grow at all, makes new words and uses the old in an unfamiliar sense, has no following and is tartly reminded that “it isn’t in the dictionary”…

Ignore, Change, Add?

Performance Optimization WordPress Plugins by W3 EDGE