PROGRAMMER DESK

July 27, 2009

.Net Coding Standards

.Net Coding Standards 

Written by – Vinay Hatwal 

28-Jul-2009

 

 

 

 

 

Code, anybody can write. By having only few months of programming study or experience, anybody can write a “working application”, but writing a good code is like a passion. By writing a working code you can develop an application but as soon as it will become larger, even you will not be able to understand it. Even when u will want to add some new functionality or you will want to modify your application, you will face a lot of problems.

 

So do not just write Working Applications, always write Good Code for your application so that u can make it programmer friendly.

 

Writing a good code should be included in your good habits, in your hobbies, so that your application should be highly manageable. For this purpose you have to follow some standards regarding writing code for you application not only in .Net but also in other programming languages.

 

After a lot of study and a lot of analysis on the Coding Standards, I am summarizing these coding standards in my article.

 

1)      Naming Conventions

 

Object Notation Good Bad
Class,

Structure,

Method, 

Property

Enumeration,

Namespaces,

Delegates

 

Pascal Casing EmployeeMaster,

StudentMaster

employeeMaster, EMPLOYEEMASTER,

employee_master

Interface Pascal Casing but start with I IUnknown iUnknown ,

iunknown,

IUNKNOWN

Variables

Objects

Method Parameters

Camel Casing userName

userId

userAge

Userid

User_Id

USERID

UserId

Class level variables Camel Casing starting with a underscore _userName

_userId

Userid

User_Id

USERID

UserId

userId

Constants All Caps with underscore CONS_SUPERUSER Cons_superuser

consSuperuser

 

2)      Variable Naming Conventions

  • Do not use Hungarian Notation with the variable names because they will become more complex to understand with it. Like –

 

Dim m_strRefUserName as String

 

It shows module level string variable with reference for holding Username. Now think lot of variable declaration in your project. Don’t you think these will create a lot of confusion?

 

  • Do not use underscore (_) with local variables.

 

  • Do not use single character variable name like I, j, k. Instead use meaning full variable names like Index, Counter.

 

  • Do not use abbreviation with the variable names. Like instead of writing

 

Dim add as string

 

Use

 

Dim address as string

 

 

  • Never use reserved keywords for variable names.

 

3)      Good Programming Practice

 

  • Do not hardcode numbers. Instead use XML Serialization Classes to provide the common setting for your Software. So that you would be able to change these values without making changes in the code.

 

  • Never hardcode the Strings, Images and Icons. Always use resource file.

 

  • Always use is as prefixing Boolean return type methods –

 

Private Function IsListEmpty() as Boolean

 

End Sub

 

  • Use String.Empty() instead of using “” for the empty string.

 

  • Try to use less class level variables. Instead use local variable. Because you can’t be sure which method is responsible for changing its value.

 

  • Do not use String for Discrete Values. Instead use Enums.

 

  •  
    • Never hardcode a path or drive name in code. Get the application path programmatically and use relative path.
  •  Error messages should help the user to solve the problem. Never give error messages like “Error in Application”, “There is an error” etc. Instead give specific messages like “Failed to update database. Please make sure the login id and password are correct.”

 

  • Do not have more than one class in a single file.

 

  • Use the AssemblyInfo file to fill information like version number, description, company name, copyright notice etc.

 

 

  • Logically organize all your files within appropriate folders. Use 2 level folder hierarchies. You can have up to 10 folders in the root folder and each folder can have up to 5 sub folders. If you have too many folders than cannot be accommodated with the above mentioned 2 level hierarchy, you may need re factoring into multiple assemblies

 

  • If you are opening database connections, sockets, file stream etc, always close them in the finally block. This will ensure that even if an exception occurs after opening the connection, it will be safely closed in the finally block

 

  • Always create the Log for your application. So that you can easily investigate where is the problem in your application. Even u can log for many of the information like

 

  • Error Messages
  • Incomming Data
  • Outgoing Data
  • Warning Messages
  • Results
  • Normal Messages
  • DateTime
  • Etc

 

  • Always mark your name, date of creation, and main use at the top of the module. And make a revision history with the same information whenever the changes are required.

 

 

  • Always write default values of the Setting with the constants.

 

  • Do not code like a structure programming. Always use OOPs with .net.

 

 

4)      Architecture

 

  • Always use multi layer (N-Tier) architecture.

 

  • Never access database from the UI pages. Always have a data layer class which performs all the database related tasks. This will help you support or migrate to another database back end easily.

 

 

  • Separate your application into multiple assemblies. Group all independent utility classes into a separate class library. All your database related files can be in another class library.

 

5)      Comments

 

  • Do not write comments for every line of code and every variable declared.

 

  • Write comments wherever required. But good readable code will require very less comments. If all variables and method names are meaningful, that would make the code very readable and will not need many comments

 

  • Always write XML Comments to describe the functionality of a class and Methods. They will help you and others to understand the functionality of the Method at the time you r using them.

 

 

Resources

 

www.dotnetspider.com/tutorials/BestPractices.aspx

http://www.irritatedvowel.com/Programming/Standards.aspx

July 3, 2009

Embed Google Search in Your Custom Applications

Filed under: Embed Google Search in Your Custom — vinayhatwal @ 5:36 am
Tags: , , ,

Embed Google Search in Your Custom Applications

Written by – Vinay Hatwal

Dt. 03-Jul-2009

 

Dear all,

This time I came with the topic “Embed Google Search in Your Custom Applications”. With this we can embed Google search in our application. First of all I will discuss the tools which you need to install on your PC to use the Google Search

 

  • .Net Framework 3.5
  • Google Search API for .NET 0.2  (GoogleSearchAPI_0.2.zip)
  • GoogleSearchAPI_0.2.zip
  • GoogleSearchAPI.dll

 

 

Steps to do

 

1)      Create a new project with vb.Net 2.0 or 3.5.(Windows Application)

2)      Add Reference of GoogleSearchAPI.dll

3)      Add following controls

  1. RichTextBox  – RichTextBox1
  2. Label- Label1
  3. TextBox- TextBox1         
  4. Button- Button1

4)      Your Interface will look like

inter1

 

5)      Add the following coding in your Form1.cs file

 

 

 

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        ‘Declaring Generic IList for IWebResult Interface

        ‘You can use -

        ‘1) IWebResult

        ‘2) IVideoResult

        ‘3) INewsResult

        ‘4) INewsResult

        ‘5) IImageResult

        ‘ AND MORE

        Dim results As IList(Of Google.API.Search.IWebResult)

 

        ‘Taking result

        ‘calling of Google.API.Search.GwebSearcher.Search(search text, Number of search result would be returned at a time)

        results = Google.API.Search.GwebSearcher.Search(TextBox1.Text, 10)

        Dim str As String = “”

        RichTextBox1.Text = “”

        For Each r As Google.API.Search.IWebResult In results

            ‘Fetching Title

            str = str & “Title :” & r.Title & vbCrLf

            ‘Fetching URL

            str = str & “URL :” & r.Url & vbCrLf

            ‘Fetching VisibleUrl

            str = str & “VisibleUrl :” & r.VisibleUrl & vbCrLf

            ‘Fetching Content

            str = str & “Content :” & r.Content & vbCrLf

            str = str & “=========================================================================================” & vbCrLf

        Next

        RichTextBox1.Text = str

    End Sub

End Class

 

 

6)      Now Enter the text to search on text box and click on Go. The result will look like –

Search Result

Blog at WordPress.com.