09 June 2011

.net Naming Conventions (Part 2)

The part two of the article will be a little bit tricky. I may not be able to suggest a concrete idea for you on how to name some of the controls on the windows form. All I can do is to give you ideas about the pro’s and cons of the name you will give to the control. Well, let’s start with the easy ones.

Windows forms
I used the prefix “frm” followed by the name of the form with the first letter of the word capitalize. Very easy to read. Easy to search on intellisence grouping and you can easily identify your forms with their prefixes.

Examples:  frmHome      frmProducts       frmLogdetails


Well now enough for the easy part. Let’s get to the hard part. Well be talking about these controls first the Label, Textbox, Buttons, Combobox and Panels because these controls are what I used the most and has the most conflict.
When I was just starting out vb.net it was very easy for me to name this controls. See the example below:


Now the first con with these is when you have to change control type. At first you only expected to put names on Textbox right? But how about the issue regarding the returning students? To solve the issue you decided to change it to a combobox instead so that if the name already existed you just have to enable his account again. What if there are 20 statements in your code using the control txtName? It would really take time to change those names to cbxName right?
Here is a possible solution to the problem:


With this type of naming convention you can easily change control types without having difficulty to change their occurrences on your code. Let’s use the problem that we had above. At first we were using a textbox for our name. With this convention we use the NameField as the name for our textbox. If we decide to change it to a combobox we will just delete the textbox control then add a combobox control and give the combobox the same name because it has the same purpose on the form. It is a field for our Name.

Now, due to larger demands for friendlier and better GUI, we also have to come out with more uses with our controls. Make something different from what most control usually do. Just like the example below.
You can’t make those buttons by simply using the default button control so I used panels for those buttons because panels support transparent backgrounds and able to contain images. With the naming convention that we used above, we can simply name the panel CloseCommand and MinimizeCommand without having any problems identifying its purpose. We don't even have to emphasize on the name that it is panel because we don't use it as a panel in our form. It is a command button.

If you feel that it’s getting clearer now, well it’s not. Let’s take a look back with what we are missing here. Ok I remember, how about the picturebox? The checkbox? In short the other controls. What kind of suffix shall we give them? This is the point where it gets really tricky.

I’m a developer who promotes readability and integrity, even if your not that good at programming I want you to be able to understand what the program is all about. I would not say that the solution I made is the one you should follow, as I said before it really depends on your team what conventions to use to promote readability and integrity on your project.

Here are some examples of the names I used for the controls since I stick with the “frm” naming for my forms:

NameTextbox     SubmitButton     LogoPicturebox     ActiveCheckedBox     BackgroundPanel


I used the names above for the controls that are used just the way they are supposed to. Now for the controls that are used in other ways:

ClosePanel_Cmd      MinimizePanel_Cmd


I put an underscore plus the abbreviate description of what it will do as a suffix to its name and control type. Through this method you can easily identify that the panel has other purpose on the form.

You might be thinking now, how about the change in control scenario? Do we have to waste a lot of time to renaming the deleted control with the new one?

I found two ways to solve this.

● Using quick replace

  1. Change your control type from textbox to combobox then give it a name.
  2. Go to your code, then press ctrl + F and the Find and Replace diaglog box will appear.
  3. Click the quick replace tab, type the name of your deleted textbox in the “Find what” field then type the name of your combobox on the Replace with Field.
  4. Then click replace all. You’re done.

● Using rename

  1. Go to your code and right click on the name of your textbox control.
  2. Choose rename, and then type the name you want for your combobox control.
  3. Now delete the textbox control on your form. Replace it with a combobox and name your combobox   with the name you choose to rename the text box in the code.
  4. That’s it you’re done.

I made all this adjustments on the names based on my experiences, it's working great for me when I'm doing vb.net projects at school. 

I hope you learn a lot from this article. I'm not really sure if I could still make a part 3 of this since I'm already running out of idea on naming conventions. But if you have questions, clarifications and suggestions feel free to leave a comment below maybe then we could compile your comments to create a part 3 of this article as a close up on .net naming conventions. 

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More