Jul 17, 2008

Code Policies - the first step to get rid of boring code policy violations is made.

As I and Vesko (only!) discussed - it is great to follow code policies, and they are saving time. A lot of time. I agree.
I really like to have so strict policies at work so I can find for example the ObjectDataSource of some dropdown, without knowing its name. For example i will type "objDsSomething" or "odsSomething" or "ODSSomething". It really saves time.

I don't really agree with Vesko's opinion that only in the begining you are violating the code policies. I think even developers who worked on the same project for year will violate its policies (not so often offcourse).

The problem is that we are all human beings and we all make mistakes. I decided to do something to help myself get rid of at least the boring code policies - such
as naming conventions.
Here is what I have so far (I started this thing few days ago and I am working on it for about an hour daily after I was all day at work so it may not look very promising but I think it has potential):



When you click on "Validate Current Document" the document which is currently open will be validated against predefined naming conventions. If you violated something you will have this output:



I have few more problems to solve and will be really glad if someone can point me the direction:

1. I need to find a way to add this menu to the context menu for the document and also I would like to add hot key so you can for example validate the document by pression "CTRL + V + D" or something else (this is low priority).
2. I would also like to be able to check the code itself for inadequate assignments, method calls and so on. The CodeModel approach I think, will not allow me to do so.
3. I would like to change those standard icons with custom ;) (low priority).

Currently this thing can fly only for naming conventions.

Jul 15, 2008

C# standard way to check if a character is lower case

Okay, I am guilty ;). I found the "C# way", I forgot to check the char to see if it offers some static methods to check if a character is lower case. IT DOES :). I ovelooked it... Here is how will you check if a character is lower case in "C# style":

char.IsLower(c);

Furthermore the char type offers more useful methods as : .IsUpper(char c) ;). IsNumber, IsPunctuation and so on, so I will stick to it for now. Sorry for misleading you (if I did :)
.

Check if a character is lower case (C#)

I am not quite sure if there is a "standard" way to check if a character is lower case but here is what I came up with:

private bool isLowerCase(char c)
{
  
return c > ((int)'a') - 1 && c < ((int)'z') + 1;
}

I think another approach would be to check if the character against the lowercase version of the character but this way you will have to first convert it to string (as the char type doesn't support .ToLower()), then to get the element at index 0 (so you again have a char variable. Here is the method to do so:

private bool isLowerCase(char c)
{
return c.ToString().ToLower()[0] == c;
}

the last approach is to check the character with regular expression:

private bool isLowerCase(
style="color:Blue;">char c)
{
return Regex.IsMatch(c.ToString(), "[a-z]");
}

Note that I haven't tested those approaches, I only worked with the first one, the other were written just to point you other approaches. If you want to use some of them you will need first to test them (although I think they will also work like charm). The other think to consider is the performance, when I have some free time I may perform some tests to see which approach is the fastest.

Jul 14, 2008

C# SqlParameter strange behaviour (or not so strange?)

I had strange experience with the SqlCommand.AddWithValue method.
Consider the following code:

SqlCommand cmd = new SqlCommand();
cmd.CommandText =
"SELECT * FROM Products WHERE ProductID=@ProductID "+
cmd.Parameters.AddWithValue(
"@ProductID", 12);

Can you spot error?
It is a developer error ;). The problem is the "+" after the command text line.
What is strange is that it won't generate compiler error. Instead this, the text will become:

"SELECT * FROM Products WHERE ProductID=@ProductID @ProductID"

(the name of the variable added will be appended to the command text)
Offcourse in the normal case you will not have + between the CommandText = and Parameters.AddWithValue, but if
you accidently miss this tiny error
it will be pretty hard to spot it later (as the compiler will not complain).


Be careful ;)

Jul 13, 2008

Code policies?

I was thinking the other day about how much efforts does it take to strictly follow your company code policies.
They are saving time for sure, but they are also wasting time.

First of all the developer needs to think about the policies all the time. This districts him/her from the real problems.
Second - after the task is completed the developer again needs to check if his / her code follows the code policies.
Third - if there is a code review - the code reviewer will also need to check if there isn't a problem with those code policies, class by class, member by member.

Some of the rules are very very foundamental as for example not to reference the DataAccess Layer from the User Interface Layer.
Other are company level rules - such as
naming conventions, using some classes on some places and not using some classes on some places. Also - using your project util classes instead implementing the same functionality in your class.

While some of the things aren't very easy to note in a code, other are obvious. And while some of the things aren't very easy to automate via AddIn or external program, other can be easilly automated.

In my opinion the developer should be focussed develop some more useful functionality, instead thinking about which rule will he / she broke.
Please note that by writing the above sentence I am not saying - "Drop the code policies!", but "Automate the code policies!". If I had some tool to check if I violate a rule while writing code, I will rewrite my code in order to satisfy this rule. But after I don't have I need to think about those rules while developing, which districts me from what I need to achieve.

I would really appreciate if you drop a comment on the following question(s):

"Does the code policy bother you? Does it take from your time? Does it districts you?"

Thanks in advance!