How to separate WinForm code

September 4th, 2007

A “normal” form contains lots of event handlers. Everything from click handlers to drag’n’drop and OnPaint. After a while they grow numerous and entangled enough to be a bug source of their own; whenever something is corrected there is a risc a new bug pops up.

I have a way of writing code to avoid this. For some time at least. :-)

The rule is that no control should be aware of another. (except in some simple cases) So the myButtonOnClick does not update any other control but only try-catch and call a method.
This means there are 50 event handlers in the form and as many methods. Some buttons do the same thing and call the same method but more often two different methods call a third one.

I split the form into regions. One is called “Event handlers” and another “Presentation logic”. Then there are other regions too when needed.
This way the code and logic is split naturally into the graphics and the logic behind it. If it smells like the top of 3 tier then you are right. Just because we talk about n-tier in a solution doesn’t mean it is not applicable on smaller parts too.

It comes naturally to work in one of the regions then, mostly in the Presentation logic region, and to not worry about the controls and their rendering.
If the form is Big then use partial class of dotnet2 and split the class into several files.

I have not found a natural way to name the methods handling the presentation logic.

Rotate tasks

July 26th, 2007

I have an idea of how to induce communication in a project.

I really wanted to do pair programming but didn’t dare to insert it into the project due to some reasons.
After a while I noticed that even with templates to code from, people did things in their own way (naturally) and that people had different knowledge about the customer’s plans.

I then thought about rotating the tasks, like one only works with a task for 2 days and then everyone switches. This forces people to communicate (i.e. ask others what they have done) and to sync their programming styles with each other.

I never forced this though.
But if anyone have – feel free to tell me the outcome.

Communication, communication, communication

July 26th, 2007

Arguably one of the most important things in a project is communication.

In a project I started all briefing for new participants with the words
“I have three things that are the most important things in this project. They are communication. Communication. And communication.” I then wrote this on the black board and let it stay there for the rest of the meeting.

I still believe communication is one of the most important things and if anyone has any input about how to communicate better – please drop me a line.

What I am primarly looking for is
– how to aid communication, how to make it naturally
– how to communicate lots of information in little time
– how to store communicated information
– how to search and retrieve communicated information

An example of voting

July 26th, 2007

How to vote when:
You have a project with several stake holders.
There are x things to do with y money so some prioritizing is a must.
You can quantify the stake holders’ influence, like the amount of money invested in the project.

Answer:
Give each stake holder a number of markers according to their investment.
Spread the tasks on a table and let each vote with their markers.

Easy, visible and should induce communication.

Hungarian notation

July 23rd, 2007

Hungarian notation was, and is, a good idea.
Unfortunately prefixing the variable with the type has today become suffixing:
sCustomerNumber -> customerNumber
sPassword -> passwordString
nMax -> maxValue

What is the reason for this? Is it harder to read nCustomer than customerNumber? I don’t think so. Instead more space/characters are used for communicating the same information.

Think of the ever present”customer number”. Written as customerNumber at first look it looks like a number, a series of figures. Now we know that a customer number has dashes and letters in them so it is really a string, like customerNumberString. Written with hungarian notation it is sCustomerNumber and clearly a string.

Now I have come to use PascalCasing or camelCasing like everyone else but only for strongly typed languages like C# which can by itself differ between strings and integers. For loosely typed languages like Javascript or PHP i still use hungarian notation.

My rules for hungarian notation in Javascript is like:
sXXX for strings
nXXS for integers (bytes, long integers, whatever)
fXXX for floats (doubles, decimal, whatever)
oXXX for many objects (oUser, oBoat, whatever)
myclassXXX for other object (userProtagonist, accountMain, accMain, whatever)
rsXXX for record sets
bXXX for booleans
anXXX for arrays of integers
asXXX for arrays of strings
cXXX for constants and characters – I am not totally happy with this

There has been no reason to make it more complex than that.

Everything takes at least 15 minutes

July 18th, 2007

In my experience no task takes less than 15 minutes.
If the very work is 30 seconds the rest is still used for administration, coffe, bathroom, email, web, usenet, talk, phone, looking out the window, rereading, testing, adjusting table, moving screen, listening to song, youtube, blog, slashdot, stretching, scratching, looking at blue led, picking nails, cleaning keyboard, preparing for home going, preparing for next day, cleaning black board, moving picture in cubicle, watering plant, removing old plant, tripping over old cofee cup, looking at bird, wondering about the name of the slow disc drive for commodore 64 and humming a song that refuses to leave the brain.

This means that a maximum of 8 things are done each day.

What priority levels to choose

July 17th, 2007

When choosing how to number priorities I usually use 3 levels:
1) Critical. These are show stoppers. Without these fixed, the whole system is a waste of money.
2) Workaround exists. The customer have to call you (the developer), they might have to buy new hardware or send a backup copy by airmail deluxe. But a workaround still exists.
3) Cosmetic. This is anything that the users can figure out the solution for by themselves.

These 3 levels are not enough so they have 3 priorities in turn. 1, 2 and 3.

All in all 9 levels.

It is so simple it seems to work.

A simple sort in C#

July 16th, 2007

Here is a snippet that shows how to easily sort a class by a string property:

userList.Sort(
delegate(Customer customer1, Customer customer2)
{
return customer1.No.CompareTo(customer2.No);
});

Find.

Laziness is considered Good

July 16th, 2007

Getting the same amount of work done by as little effort as possible should be a worthy cause, shouldn’t it?

tags: | categories: Miscellaneous | no comments »

Wanted: Tool to move your code elsewhere

July 15th, 2007

I wish for a tool built into VSNet that easily moves your code elsewhere.

Like this:
You write a constant declaration right in you code, where you are, to quick and easy code andor debug. This constant should be elswhere, like in the class top or a common file. So just mark the constant and tell the IDE to move it to [choose here].
That would allow you to keep on coding without having to wind back and forth in the source. Doing this quickly would help you to avoid accidentaly forgetting to move the constant out of the method.