Tuesday, July 21, 2009

Coding Guidelines

We are lucky to have a client that is tech savvy. And most of the times our code is being reviewed by their tech architects.  Most of the times we get reviewed our code on a frequent time intervals. On one side it is good, on the other side it leaves our developers with some morel loss. Adding to the fuel, is me. I insist my developers to follow certain standards within their code. It is time to post the general points that I emphasize.


1)    Use long names for variables
2)    Initialize variables at the time of declaration
3)    Use .Equals instead of ==
4)    Use territory operator ?:
5)    Use ?? when expecting the null reference
6)    Use .Length() comparison  to validate string with values
7)    Use StringBulder instead of strings concatenation
8)    Use StringBuilder replace instead of string (Ref : http://dotnetperls.com/replace-string-use)
9)    Use single line assignments for common values
10)    Avoid try catch as much possible
11)    Organize Usings and remove & sort the references
12)    Use white spaces within every expression
13)    Separate user methods from system generated methods. Use of Region
14)    Give Author details on every page
15)    Maintain the history of modification
16) Write the code in as small reusable methods as possible.
17) Never assing objects to null when they are within the loop
18) If you can use the GC.KeepAlive, make the best use of it
19) Try to separate the Finalze method into a different class and instantiate in the caller class that implements the IDisposable interface
20) Make use of WeakReference to avoid the multiple connections / reads from static content
21) Use int.TryParse, instead of Convert.ToInt or int.Parse
22) Never Ever Trust the User Input, do use Encoding where ever necessary

There are much more, this list would never end. But these are general guidelines that are in my mind at any given time. And different approaches can be identified depending on the situation that the code is in.

Copy binary table data from diff servers

Today, before we start an internal demo of our project, we realized that, on a particular table there is no data in the QA server. We have every thing ready, but no the sample data. We just can’t create an insert script from the table and execute the script there, because, there is a column which is binary type. When we generate the script, the binary data type is not able to populate. Then we are struck with a big question of how to transfer the data between servers.

Let me explain you much in detail about our infra structure.

We have a dev DB server for our internal development hosted on 192.168.2.10 <<local ip address; for our convenience >>

We have a test bed for our application and IIS installed on it, let’s say this is 192.168.3.10, which is altogether a different network. And this IIS is connecting to a TestDB Server (let’s say it is hosted on 192.168.3.20), where we migrate our DB scripts to create the database / tables / stored procedure / user defined functions  / blah .. blah .. what not.

Now, this IIS server(192.168.3.10) is connecting to the TestDB(192.168.3.20) on secured connectivity. Our network folks implemented a rule that the x.x.2.x series can’t talk to x.x.3.x series, but not the vice versa. [[This is the trick / loop hole here]] Now that we need to populate the binary data from a table, and we don’t have access.

What all the dev’s do in the begin is to a bing on how to access the remote server to copy data from table. We are not exceptional to this, and we did all kinds of searches. We also googled for the same, unfortunately we end up either creating the LinkedServers. As we couldn’t connect to the remote TestDB server from our DevEnvironment, we couldn’t establish the connectivity. And the our application DBs are not accepting to create a LinkedServer from TestDB server.

after doing lots of trails and found some easy way to populate the data from one server to another with out having to create a Linkage between the servers.

   13 Exec sp_configure 'show advanced options', 1

   14 

   15 Exec sp_configure 'Ad Hoc Distributed Queries', 1

   16 Reconfigure

   17 

   18 insert into UserProfile

   19 select * from OpenDataSource(

   20 'SQLNCLI',

   21 'Data Source=192.168.3.20;User ID=sa;Password=sa'

   22 ).GlobalWebContent.dbo.UserProfile

Pretty neat and simple solution, it took about approximately 4hrs to convince our SQL DBs to execute the above query.