September 21st, 2007
We are used to having PKs in databases as integers.
The reason for this is that computers are good at numbers and an integer is a very versatile and fast number representation.
But there are drawbacks in the business layer. A customerID can be compared to a orderID like so:
if( customerID == orderID ){…
This is not a good thing.
CustomerIDs should only be comparable to other CustomerIDs.
They should also not be addable, subtractable, multiplyable and being greater than anything else. A CustomerID is equal or not equal to another CustomerID. All other comparisions are not of this world.
I have yet to figure out how to implement this.
A comment of what to use as a primary key is found here.
September 21st, 2007
>There are in SQLServer2005 ways to version control it through tools by Microsoft. Since I havn’t had the time to learn how to and some of us use other databases there is a common technique that works so so but at least gives us a chance of reverting to an older version.
Any time I decide that this is version x.y. I check in the database as binary. Image or backup doesn’t matter as long as it is recreateable without anything else.
After that all updates must be scripted. These scripts are checked in in the same folder as the database.
One can then get the database and use the date stamps on the scripts to get the exact version of the database.
After a while there are too many scripts so we check in the database again.
One then sorts the files on date and starts by getting the database. Then run the scripts to the proper version.
This is not a perfect way; it is easy to forget to script a change. But it is a start at least.
September 21st, 2007
My prefered way to write XML comments in C# is to write the first sentence on the first row like this:
/// <summary>This method does nothing.
/// <summary/>
public void Foo()
{
//NOP.
}
because when one folds the code (ctrl-m-o) one can see the first row like this:
/// This method does nothing. …
public void Foo() …
This is not an issue in vbnet.
To create a document out of this use Sandcastle which takes over after nDoc which is discontinued.
More comments on commenting are found here and here.
September 13th, 2007
>In C#, C and C++ the exclamation sign (!) is used for negating a boolean value.
This is considered a bad thing because it is so small and easy to miss.
if( !IsPostBack ){…
I would like to write
if( Not( IsPostBack ) ){…
instead. But I have yet to find a way to overload it such in C#.
Even with the improved readability of “Not” it is easy to think wrong. It is way too easy to get lost in negations in a just slighty complex boolean expression.
A way to improve readability is to avoid the useage of ! or negations at all:
if( IsPostBack ){
// Does nothing.
}else{
…
}
September 13th, 2007
With no information you have one task.
1) deduce the right information
With the wrong information you have to:
1) understand the information
2) find out that the information is wring
3) deduce the new information
September 13th, 2007
>A friend and I collect things that counterwork their own purpose.
One of them is GhostDoc which among other things tries to figure out comments to write for your methods.
Example:
public void GetUserByUserID( int userID )…
would probably render a comment like
///<summary>
///Get the User of the specified userID
///<summary>
///The user ID.
///<parame name=”userID”>The User ID</param>
Let’s say the method does exactly what it look like. No side effects or strange behaviour due to historical reasons at all. Why do we need a comment for then?
Ok, a correct comment does not damage anything.
But consider if it is not totally correct. It only gets active users, it returns null if a user is not found, userID = 0 does something or userID = -1 does something else. Then GhostDoc created a comment that is wrong.
The base fault here is that the programmer didn’t comment his method at the first time. We can blame him but hand up anyone who hasn’t forgotten to comment a method. So this is not a valid excuse.
There is also no way of telling the automatically created comments (that might need a check) from the manually entered comments (that also might need a check but at least are not automatically inserted with faults)
What GhostDoc does wrong is to enter possible new comments that are wrong, to enter false information to a program. And this is done automatically.
Update: I just found Bumblebee that does approximately the same.
September 13th, 2007
There is a nifty operator in dotnet2, ??
Use it to get the first thing that is not null.
string name = firstName ?? lastName ?? email ?? “Has no name”;
sets name to the first value that is not null.
To set a string to empty if it is null but keep it otherwise:
message = message ?? string.Empty;
which is the same as
if( null == message ){
message = string.Empty;
}
One might consider it harder to read but I think it is a matter of getting used to.
Beware though of its low precedence.
string user = userName ?? string.Empty + description;
will always return only the userName if there is one. Alas, insert a pair of parenthesises:
string user = ( userName ?? string.Empty ) + description;
On the side I prefer
string.Empty
to
“”
because with string.Empty I am totally sure the string is empty. There is no chance what so ever that a stray space slipped in between the quotes.
September 11th, 2007
Some people complain about stored procedures as bad because they inject business logic into the database.
My answer to this is that then the stored procedures are used the wrong way.
Stored procedures should be used to get data in and out the database. Used this way they represent the holes in and out of the database.
Name the stored procedures cunningly and you have an easy way to find the only ways to insert data into the customer table, the only way to update the order table and only ways to select multiple rows from role.
I have not found the perfect naming scheme. Anyone?
September 11th, 2007
It is very easy to inherit from your controls in dotnet/VS.Net. Just add a class and let it inherit from any Control. That’s it!
To use it either reload the project (possibly the IDE) and drag it from the toolbox to the left. This is only possible in VSNet2005 I think. Or do as I, open the myclass.designer.cs file and edit the declaration in the bottom. Recompile and edit the compiling error at the instantiation. That’s it!
VSNet2005 does not interfere with your hacking of the .designer.cs file. Unless you are building for WinCE that is.
Why inherit then?
There is really no need to do it all the time.
But keep it in your head so the moment you need some common functionality in your form you can put it in BaseForm.
Update:
Always inherit from the grid control. There is a very good reason regarding type safety and differentiation of data and view for this. I hope to write about this in a not to far future.
Update:
Hacking designer.cs in VSNet2008 for WindowsMobile(6.1) works alright but you will probably get an “InvalidCastException” and a pointer to something like
((System.ComponentModel.ISupportInitialize)(this._myControl)).BeginInit();
The error is explained here and the solution is to just comment/delete the rows
((System.ComponentModel.ISupportInitialize)(this._myControl)).BeginInit();
and
((System.ComponentModel.ISupportInitialize)(this._myControl)).EndInit();
September 10th, 2007
Before you start fixing a bug. Make sure you can recreate it.
Before changing the first line of code always reproduce the bug.
Without having the same indata and getting the bug and the same indata and not getting the bug you can never be sure it is fixed.