Stats for December 2007

Top 10 posts

Position Last Month Title Posting Date
10 7 Chocolate Crunch Cake 04-Aug-2007
9 4 What’s new in Visual Source Safe 2005? 28-Oct-2005
8 8 Visual Studio 2005 on Vista 15-Apr-2007
7 New Entry Getting started with Spatial Data in SQL Server 2008 01-Dec-2007
6 6 What is a DAL? (Part 2) 05-Sep-2007
5 5 SQL Exception because of a timeout 17-Oct-2005
4 New Entry Follow up on hiring a software developer 19-Dec-2007
3 9 What is a DAL (Part 3) 13-Oct-2007
2 2 Internal Error 2755 caused by folder encryption 15-Oct-2006
1 3 What is a DAL? 28-Aug-2007

Browser Usage

Position Last Month Browser Percentage this month Percentage last month Change
5 5 Mozilla 0.58% 0.42% +0.16
4 4 Safari 1.01% 0.79% +0.22
3 3 Opera 4.17% 1.51% +2.66
2 2 Firefox 29.33% 33.49% -4.16
1 1 Internet Explorer 64.85% 63.66% +1.19

Location of Visitors

Position Last Month Location
10 New Entry Brazil
9 6 Netherlands
8 New Entry Israel
7 6 Germany
6 New Entry Poland
5 5 Australia
4 4 Canada
3 3 India
2 1 UK
1 2 USA

New Year Resolutions

It is coming to that time of year where tradition says that people should set resolutions for themselves to make their life better.

So here are my resolutions for the coming year:

  • Reduce my weight by half a kilo per week. That is 26 kilos for the year. If you work in old money that is 57 lbs. That puts my target weight as 78 kilos, which is only 3 kilos short my the ideal weight for a person of my height.
    • I will be entering the Chest Heart and Stroke Scotland charity bike ride again this year. So I’ll be getting my bike fixed
    • I intend to walk more. Don’t know where to, but I think what I’ll do is just take more scenic routes from place to place
  • To get through my stack of books. I have a stack of books, some of which have been on the stack for about 18 months now. I want that by the turn of 2008 into 2009 that my stack of books consists of nothing older than a month. What’s on the stack?
    • Application Architecture for .NET: Designing Applications and Services.
    • Agile Estimating and Planning
    • Code Complete (Second Edition)
    • Implementation Patterns
    • Beautiful Code
    • and about a half a dozen other technical books, plus an array of non-technical books.
  • Update my website more frequently. I have a number of series running on my blog that I intend to turn into articles at some point. I also have a number of posts that would work well in the Tips & Tricks section.

Finally, for the first time in my life I will be working on 2nd January! If you don’t know, in Scotland 2nd January is a public holiday. I managed to resist last year, but since I work for an English company now (for first time since leaving university) I don’t get that as a public holiday any more.

Technorati Tags: ,,

The value of smaller methods

A while ago on a forum I advised someone that their method was too long and difficult to read and maintain. I suggested that they break the code into smaller methods as it would improve the maintainability and readability of the methods. I got a response back from another person on the forum suggesting that it was stupid to do that as it made debugging more difficult because you had to “step-in” to so many methods and that made things more difficult. At the time I didn’t respond to that. However, I’m hoping to change that with this post.

I’m prompted to write this because I’ve been catching up with the DotNetRocks podcasts and they have an advert on some episodes running up to to the TechEd in Barcelona about winning a big monitor so you can more easily read “that impossibly long line of code”. I don’t like impossibly long lines of code (or even possible yet very long lines of code).

I actually used to work with someone who’s code reviews included the very strict “thou shalt not exceed 80 characters per line” and although I don’t stick to a hard limit I don’t tend to go over about 100 characters per line, which is the amount of code that will comfortably fit on a regular monitor (1024×768) with various bits of screen real estate taken up on either side (for things like the solution explorer and toolbox) without having to scroll horizontally.

In free flowing languages like C# it is possible to to allow one code line take several screen lines. A line of code in C# is terminated with a semi-colon rather than a new-line marker. It is possible, therefore, to insert new-lines in the middle of a code line. However, before doing this I’ll see if there other ways to make the line shorter.

For example, the following line of code:

someObject.SomeMethod(GetWidget(variableA, variableB), anotherObject.SomeProperty, someFieldOnThisClass);

In a normal application the above line would be indented some way because it would be contained in a method, in a class, in a namespace, it may possibly be inside a conditional statement (e.g. an if statement) or loop (e.g. a foreach statement). That adds, typically, another 16 characters to the line length.

This line of code is also more difficult to debug because in order to step-into SomeMethod you first have to step in and then out of GetWidget and SomeProperty.

So, to reduce the line length you can refactor the code so that it is built up over a number of lines instead

Widget widget = GetWidget(variableA, variableB);
int propertyValue = anotherObject.SomeProperty;
someObject.SomeMethod(widget, propertyValue, someFieldOnThisClass);

On the long line of code that saves 33 characters, so the code is now somewhat easier to read as it doesn’t get to the point where any horizontal scrolling is necessary. It is also easier to debug as it is possible to directly step into SomeMethod without getting waylaid in and out of GetWidget and SomeProperty first.

For cases where you can’t extract any more out of the line there is still the ability to wrap the line of code onto a new line of text.

In general I’d say that any method that exceeds a screen in size is too big and should be considered for refactoring. I don’t use this as a hard limit, just a general guide. But why put that limit on the size of a method?

But lets take an example:

public void DoSomething(bool someParameter)
{
      if (someParameter == true)
      {
          // Some processing that takes more than a screen.
          // Assume it contains nested ifs, fors, while, etc.
      }
      else
      {
          // Else what? What was the original if statement
          // I guess we'll just have to scroll up to see
          // (and just hope I match it with the correct if)
      }
}

The same would go for any code that uses a lot of conditional or looping code because it is easy to get confused with what the end curly-braces go with because the opening part is off the screen and the context is lost.

Another example would be looking through an overly long method to see if any code is repeated, or perhaps the code at one point does the same as in another method. That block of code within the method would be an excellent candidate for refactoring out.

I once went on holiday and to come back to find that another developer had finished writing a control that I’d started. I didn’t recognise much of my original code, but I did find a 1500 line Render method that manually wrote the HTML to the stream. There was code in there that inserted breaks and spacers between bits of data. In fact, I found several (dozens of) instances of the same handful of lines of code that just by refactoring that out I got the Render method down a good few hundred lines.

If I had been writing that method I would have worked out what the constituent parts were and written smaller methods for each of those parts. The control was a customised control to write out an editable table in a way that the DataGrid didn’t handle. I think my render method would have been along the lines of:

protected override void Render(HtmlTextWriter writer)
{
    RenderHeader(writer);
    RenderSpacer(writer);
    bool isAltLine = false;
    foreach (ControlData dataItem in data)
    {
        RenderData(writer, dataItem, isAltLine);
        RenderSpacer(writer);
        isAltLine = !isAltLine;
    }
    RenderFooter(writer);
}

Each of the subordinate Render methods (such as RenderHeader, RenderData, RenderSpacer and RenerFooter) would also be split up in to smaller chunks. For example the RenderHeader would have iterated through all the column headers and called some RenderColumnHeader method. The RenderData method may, for example, iterate through the data and call specific render methods for each type of data to be displayed.

So, as to the objection that it makes debugging more difficult because you have to step into more methods, you can see that it should make debugging simpler. In the above example it becomes easier, for instance, to concentrate on a problem with rendering a line of data rather than fight through a mountain of code before getting to the bit that renders that data.

As I hope you can see it would be much easier to be able to concentrate on small parts of the process rather than have to take the whole process in one go.

Technorati Tags: ,,,

 

Forum Signatures II

It sometimes amazes me the things people put in forum signatures. Sometimes they distill knowledge in to a short pithy phrase, other times they are just humorous and occasionally they are just plain bizarre.

Here are a collection of some signatures that I’ve spotted recently:

Re-quoted forum posts

The clue train passed his station without stopping.” – John Simmons / outlaw programmer

It’s the “give it to me” attitude that I get really irritated with

Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks” – Pete O’Hanlon

We are a big screwed up dysfunctional psychotic happy family – some more screwed up, others more happy, but everybody’s psychotic joint venture definition of Code Project

Otherwise [Microsoft is] toast in the long term no matter how much money they’ve got. They would be already if the Linux community didn’t have it’s head so firmly up it’s own command line buffer that it looks like taking 15 years to find the desktop.

“I controlled my laughter and simple said “No,I am very busy,so I can’t write any code for you”. The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible.”
– Mr.Prakash One Fine Saturday. 24/04/2004

Geeky

Deja View – the feeling that you’ve seen this post before.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” – Rick Cook

God is Real, unless declared Integer.

I still remember having to write your own code in FORTRAN rather than be a cut and paste merchant being pampered by colour coded Intellisense – ahh proper programming – those were the days

SELECT * FROM User WHERE Clue > 0
0 rows returned

A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects. –-Lazarus Long

Software Zen: delete this;

Quotes

“On one of my cards it said I had to find temperatures lower than -8. The numbers I uncovered were -6 and -7 so I thought I had won, and so did the woman in the shop. But when she scanned the card the machine said I hadn’t. I phoned Camelot and they fobbed me off with some story that -6 is higher – not lower – than -8 but I’m not having it.” -Tina Farrell, a 23 year old thicky from Levenshulme, Manchester.

“There are three sides to every story. Yours, mine and the truth” ~ unknown

“There is no wealth like knowledge, no poverty like ignorance.” Ali ibn Abi Talib

A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn’t see the clouds at all – he’s walking on them. –Leonard Louis Levinson

It has become appallingly obvious that our technology has exceeded our humanity. – Albert Einstein

“You can have everything in life you want if you will just help enough other people get what they want.” –Zig Ziglar

“…when two opposite points of view are expressed with equal intensity, the truth does not necessarily lie exactly halfway between them. It is possible for one side to be simply wrong.” – Richard Dawkins

“If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.” — Alfonso the Wise, 13th Century King of Castile.

Just in terms of allocation of time resources, religion is not very efficient. There’s a lot more I could be doing on a Sunday morning. — Bill Gates (13 January 1997)

Unknown Origin

“mostly watching the human race is like watching dogs watch tv … they see the pictures move but the meaning escapes them”

Where are we going? And why am I in this handbasket?

You always pass failure on the way to success.

Humorous

I saw a wino eating grapes. I said,”Dude, you have to wait.” Mitch Hedberg

Geeky Christmas

Continuing the season’s diversions, XKCD has some excellent cartoons, a lot of them reflect the reality of people with a technical profession and are passionate about it. And this one is no exception.

Christmas Back Home

 

Technorati Tags: ,,,

 

 

Christmas Humour

Just a little humour for Christmas. Twelve Days of Christmas (Indian style)

Spam messages are down

It seems that the spirit of the season of goodwill is also bestowed on us by spammers too. I’ve seen a noticeable fall off in the amount of spam my blog gets over the last 36 hours or so. The same is true when I look in the spam folders of my gmail and Yahoo accounts. I wonder how much faster the Internet is running as a result…

Technorati Tags: