Gripping Reading, All The Way Through!

Your system throws up an error message. It's tempting to read the first few words and jump to a conclusion. 'Oh I know what that is...' But you really have to read the entire error message. Often the best bit isn't until the very end.

Error messages are not like those predictable hollywood films that you can walk out of before the end, and still know exactly how it finished up. "Oh yeh, they defused the bomb, he got the girl and the dog learnt a new trick."

Here's two messages I had yesterday:

Could not open new database 'C:\{...Path to Database..}\DATABASENAME.MDF'. CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file C:\{...Path to Database..}\DATABASENAME.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
File activation failure. The physical file name "C:\{...Path to Database..}\DATABASENAME_log.ldf" may be incorrect.
The log cannot be rebuilt when the primary file is read-only.

In this case, those last few words pinpointed the exact cause of the problem.

Not long after that, I got this message:

Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
Exclusive access could not be obtained because the database is in use.

Again, it was the final words that pinpointed the problem.

It's so easy to read the first few words and start making assumptions. In the first example the assumptions might be "oh, i must have the wrong name for the log file". In the second case the wrong assumption is "oh, the query is taking too long and needs to be 'optimised'..."

I've said before that you have to read the error messages. I'll add now that you have to read the entire error message.

(names of databases have been changed to protect the data)

 

Sql Down Under Code Camp

If you are going to be in Australia in October (2006), between Saturday the 7th and Sunday the 8th, you ought to attend the Sql Down Under Code Camp.

The main organiser Greg Low is simply a legend. We (at advantech) ask Greg for help whenever we hit a problem that is well over our heads. One of the guest speakers, Itzik Ben Gan, is a right genius. We use tricks he showed us (in a previous talk) for a lot of our more advanced sql solutions. We use those tricks every day. Other speakers like Geoff Orr and Adam Cogan are well known experts.

The camp is in Wagga Wagga, which is centrally located (read: 'the middle of nowhere') and it is free!

 

F# -- the 3 minute guide!

By popular demand, we're going to spend three minutes learning as much as we can about F#.

So what is F#?

F# is "a mixed functional/imperative/object-oriented programming language"

Sorry -- that description's meaningless to all but a handful of people. What I want to know is:

Is F# really "Fortran .Net?"

No! An earlier name for F# was: "Caml.Net" because this is essentially a ".Net" implementation of "Caml"

That doesn't help, unless you know what Caml is...

Caml is a popular French variation on the ML Programming language, where ML means 'meta-language'.

Then I take it F# has nothing to do with Sharepoint's Caml language?

Nothing whatsoever. It's unfortunate that the sharepoint team used that name. Enough said.

You know what? Let's just get our hands on the code...

(Continues)

If you've got the .Net Framework version 2.0, then all you need is F#:

Download and unzip. You'll have a nice folder full of goodies.

As you can see there's an ".MSI" (microsoft installer file) included. Run that... and things should go smoothly.

(Oh wait... some problem with a missing file... a html file? Who cares? Ignore!)

It installs an interactive console, a visual studio plug in, a manual, release notes and samples.

Let's tinker with the F# Visual Studio plugin

Open visual studio 2005, go to the tools menu, "add in manager" -- and click the interactive F# tool, to load it.

Now there's a new tool window, not unlike the 'immediate window' that we know and love.

It contains this chunk of text:

MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved
F# Version 1.1.11.12, compiling for .NET Framework Version v2.0.50727

NOTE:
NOTE: Flags: --gui run a session with a GUI event loop.
NOTE: --exec just run the scripts on the command-line.
NOTE: Commands: #r <string>;; reference (dynamically load) the given DLL.
NOTE: #I <string>;; add the given search path for referenced DLLs.
{{blah, blah blah}}
NOTE: Bug reports to fsbugs@microsoft.com. Enjoy!
NOTE:
NOTE: Visual Studio key bindings:
NOTE:
NOTE: Up/Down = cycle history
NOTE: CTRL-C = interrupt session
NOTE: ALT-ENTER = send selected source text to FSI session (adds ;;)


I always like to skip past the rather bland hello world programs and straight onto my personal favourite:

> do print_endline "hello there ladies ;-)" ;;

hello there ladies ;-)
>

That certainly worked a treat. Not exactly inspirational stuff though.

Two crucial lessons in there: "do" calls a function, while ";;" seems to terminate a statement.

Never happy with just one implementation, i hit upon another way to achieve the same result...

I open a blank text file (in Visual Studio) and type the following:


do printf "ahh, we meet again\n"


I highlight this text and hit "[Alt] + [Enter]"

Magically, the output turns up in the interactive F# window!

Were you paying attention that time? I didn't type into the F# window. You can type in any window at all (within visual studio). Then highlight the text and press alt-enter. The highlighted text is evaluated by the f# interactive tool. Nifty stuff.

Functions! Hurry Up with the Functions!

Oh, you want me to write a function in this functional language... yes i was hoping to avoid that.

Okay -- now I've written "My First Function" in F#, here it is:


let NextNumber x = x + 1

What I'm hoping this means is:

"Let me define a function called 'NextNumber' that takes an input, x, and returns x + 1"

When I select the text of my little function, and hit Alt-Enter, the following text appears in the interactive window:


val NextNumber : int -> int

This is the 'signature' of the function, more or less. It means something like:

"the 'value' NextNumber takes an integer as input and outputs an integer."

Fair enough. Now from the smidgen I know about functional programming, i know they do love their recursive functions. I soon find this keyword rec, meaning recursive, which can be used for any function that defines it's output in terms of its input.

You'd prefer an example? Well let's write a function that adds up all the numbers from 1 to X.

And let's do it using recursion.


let rec sumto n =
  if n < 2 then 1
  else n + sumto(n-1);;

This one basically says:

"Let me define a recursive function called 'sumto', that takes the input n. If n is less than 2 then return 1, otherwise return the value (n + sumto ( n - 1)). ."

Okay, now we can use our function like so:


sumto 100

Which returns the following result in the interactive window:


val it : int = 5050


(Twenty minutes on the calcu'later, i can confirm this to be correct.)

Notice that both the function we wrote earlier and this result we've just calculated are described as 'values'. I take this to be a sign of the way a functional programming language treats functions as first class citizens.

Now I can jump to something I've been wanting to demonstrate since the start. The |> operator. Let me put that in a heading:

The Pipeline Operator, |>

That's a bar symbol, followed by a greater than. And it's similar to the pipe operator you recall from your DOS-filled youth.

Instead of typing 'sumto 100', as we did above, we could pipe the parameter into the function, like so:


100 |> sumto //returns the same values as 'sumto 100'... i.e.
val it : int = 5050

Here we've said something like: "direct the value 100 into the value 'sumto'"

We can use this logic to re-write the 'sumto' function from above:


let rec sumto n =
if n>1 then
n + (n-1 |> sumto)
else 1

What's so important about this |> operator? Isn't it just a redundant little syntactic bit of acrobatics?

Yes and no, old buddy. Yes and no.

Imagine some deeply nested function calls, like so:

a(b(c(d(10)))) ;;

You've got to admit that it looks ugly, but also that it's horribly ass about. [That's a technical term for back-to-front].

"a(b(c(d(10))))" means "apply the function d to the value 10, then apply the function c to the result of that, then apply the function b to the result of that... and so on."

A less ass-about syntax is:

10 |> d |> c |> b |> a ;;

The above code could be described as "direct the value 10 into the function d, then direct the result of that into the function c, then..." and so on.

It's easy to match the code with the description of what's happening. Or at least, easier than it would be if we used the original ass-about syntax ('a(b(c(d(10))))')

(I fully expect that the preceeding pipeline operator description is completely wrong... I await a clarification from don syme, of course ;-) )

Okay -- that's as far as we can possibly get in three minutes. We didn't have as much fun with curry as i'd have liked.

I'll finish with some links for the intrigued reader:

F# links:

The best place to start: Quick Tour -- almost a cheat sheet.

resources

articles:

 

syntactic sweeteners, part ][

(A small update to yesterdays thoughts on syntactic sugar)

First here's a chunk of Linq, that i think might be fairly valid (though i haven't (a) compiled it or (b) ever actually compiled any linq code... so whadda i know??)

(continues....)

List people = DB.GetAllPeople();
var oldPeople =
   from p in People
   where p.Age > 29
   select new {p.FirstName,
             p.PensionCardNumber};

Console.WriteLine("Some old People:");
foreach (var x in oldPeople) {
   Console.WriteLine(x.FirstName + " is old.");
}

Now here's my idea for how the syntax could get even simpler, by using more "inferences"....

List people = DB.GetAllPeople();
var oldPeople =
  from People
  where value.Age > 29
  select new {value.FirstName,
            value.PensionCardNumber};

Console.WriteLine("Some old People:");
forevery (oldPeople) {
   Console.WriteLine(value.FirstName + " is old.");
}

Okay -- so this is a bit like yesterday's "in (people visit Person p)" example -- except here we simply say "forevery (people)" and then access the keyword "value" to refer to the member within the collection.

Also in the linq query instead of "from p in People" we just say ""from People" and then rather than use the variable 'p' we use the keyword 'value', (much like in a property setter).

I'm really gonna stop thinkin about silly syntax soon. This is perhaps not as bad as the time a few months ago when i started trying to invent ancestors for xml.

 

BabySteps In WPF And XAML

One thing I'm not gonna write is a "Getting Started with Windows Presentation Framework" article.

If you want to get started with wpf/xaml/xbap etc, just go to LearnWPF.com and see what Joseph Cooney has to say. That site is a fantastic starting point.

Joseph is from an amazing family of programmers.

His brothers Dominic (the japanese cooney) and Patrick (F#) are also programmers and bloggers.

My question for these three prodigious programmers is this:

What sort of computer, if any, did they have when they was kids? And what sort of programming did they used to do, if any?

I'm guessing commodore 64. Or Amstrad (64k or 128k ?) But I await the right answer.

Meanwhile, here's the WPF community site. Something I don't get is why so many sites try to steal the digg look n feel?

I dig what digg does, but i do doubt that duplicating digg is a must-do. (Try saying that three times quickly. While drunk.)

I'll be seeing Joseph at a tech.ed talk he gives today... maybe he'll shed some light on his background.

 

sweet syntactic sugar

consider the following chunk of code:

List<Person> people = db.GetAllPeople();
foreach (Person p in people){
  console.writeLine(p.Name);
}

Day dreaming during a tech.ed lecture this morning, I thought:

Ah, but what if the syntax waz reversed?

What if the following code compiled to the same thing under the hood....

List<Person> people = db.GetAllPeople();
in (people visit Person p) {
  console.writeLine(p.Name);
}

Okay... it mightn't change our lives... but it's a wacky thought none the less. Don't reject it too quickly. Drink it in. Suck it up. Chew on it. Swill it around before you spit it out.

When you type the words "in (people visit..." the intellisense would have a chance to think:

people hey?? What type of objects are collected inside that? Person is...

You'd get a nice little intelliswiftness in there i thinks.

Nextly, whatif, instead of calling a Object.Method(), we allowed the syntax: Method Object, for example, instead of:

console.writeLine(p.Name);

Imagine you were allowed to say:

WriteLine Console (p.Name);

That's right!

Method First, Object Aft!

When you write the method name "Writeline" the intellisense then thinks to itself...

Writeline hey?? What objects do i have with that method? What static methods do i have with a method like that?

This is a different kind of intellismarts you're calling on -- it's about putting verbs before nouns, like that steveyegge article joel spolsky pointed to recently (you know the one). tink about it.

 

Momentum as a substitute for Quality

Y'all know the famous maxim from Fred Brookes: "Adding manpower to a late software project makes it later." (see addages named after people at wikipedia)

Yet big, late projects constantly get more people added to them.

Just today we heard about a large local project, to which far too many more resources have just been added. Why? we asked, why?

There's a rarely mentioned side effect of adding more people to a project: momentum.

Yes: Adding more people will make a project cost more per day.

Yes: It will slow down the daily progress, making it take longer.

And Yes it will lower the quality of the output, as personal responsibility becomes diluted.

But:

Adding more people may decrease the velocity, but it increases the mass enough to give a larger overall momentum.

And a larger overall momentum makes it harder to stop.

The people who make the decision to add more people are desperate to keep the project moving. In their mind, the only way to fail is to have the project canned by higher powers.

Budget overruns are not failure. Time overruns are not failure. The only true failure is if the entire project is stopped.

An object with huge momentum is harder to stop. Throw in consultants. Throw in hardware. Throw the bike on! We're gonna ram our way through! Unstoppable!

The problem with huge momentum is that when it does stop it makes a hell of a mess.

To stretch the metaphor: A fast moving train can still be derailed.

To mix a metaphor: assigning blame becomes a fruitless task. Blame is spread over a larger area.

Once failure is big enough, it becomes a success on other levels. Think titanic. Think war. Think 'time to polish your resume'

 

Tech.Ed Sydney

this is me

I'll be at Tech.Ed Sydney this week. If you want to catch up, I can be contacted at leonbambrick at gmail etc.

To better facilitate your facial recognition algorithms, I've published a picture of myself. If you see me come up and say "hi".

I'll be giving a cabana session on "how to skip lectures by attending cabana sessions". Details will be announced privately after the event.

 

Concurrent Tautologys (all happening together at once)

I step away from the inkernet for five minutes and of course I miss six million little things.

  • L Sharp .NET is "a powerful Lisp-like scripting language for .NET."
  • Chickenfoot is "a Firefox extension that puts a programming environment in the browser's sidebar ... a superset of Javascript that includes special functions specific to web tasks." (meanwhile i still haven't gotten dirty with grease monkey...)
  • hAxe is an OO language that can compile to js, flash or server-side byte code on apache webservers. It's strictly typed, but uses type-inference (...not unlike Ruby, F#, Powershell... and C# 3.0 ;-) ) .
  • Livewriter is a microsoft tool for bloggin. Looks buggy for now but bound to be popular later.
  • Marcos Meli seems to have done a nice logo for the larkware 1000 competition.
  • hanselman gets into dynamic languages
  • Eric Sink thinks you might as well get into multi-core programming.

  • Multi-core programming... concurrent programming... It's coming at us from three different directions now:

    1. Your next computer will have 32 or so processors
    2. Your server-side code may get to execute across hundreds or thousands of machines
    3. Your ajaxian scripts could share their processing load over hundreds of zombie client machines

    (Yeh, okay, one of those is pure sci-fi)

    On J.Spolsky's advice I read a lot of steve yegge stuff recently (particularly here and here), wherein he discusses the sort of problems you have at a server-side monster like Amazon. He keeps coming back to functional programming. (Lisp in particular, for him.)

    The new client-side multi-core scenario is also tilted toward functional programming.

    To that end, and of course by popular demand, the next 3 minute tutorial will be on F#. It's taken me a lot longer than three minutes to write of course, and it isn't ready yet. But it will be soon enough.

    Meanwhile, here's a quote that keeps running around in my head.... Wirth's Law:

    "Software gets slower faster than hardware gets faster."


    solve: one squared plus two squared plus three squared plus four squared equals three cubed plus three.

     

    What next for the 3 minute guides??

    Lately I've written a bunch of 'introductions' to newish technologies.

    Watir, JSON, Powershell (Part 1, Part 2)-- all of them completely new to me.

    If you've been interested in this series to date, can you make a suggestion for what technology to tackle next?

    Some ideas currently:

    • F# -- microsoft's functional programming language
    • Ruby On Rails -- an absolute beginners guide, targeted at Microsoft developers
    • WPF (formerly avalon)
    • WCF (formerly indigo)

    What is there that scares you? Come on, 'fess up.

    (I bet it scares me too. So let me go off and research it for the both of us ;-)

    Or, what technology has you intrigued?