Improving The Code I Write

Most of the reason I write articles are so that I have a place to put all the cool things i find regarding coding in one place.  Something has recently been coming up that has captured my attention: “Code Smells”.  By Code smells I mean the ability to look or evaluate any piece of code that has been written and assess its value based on a number of indicators. 

The question that started me on this path is: “How many developers look at ways of improving the way they write code?”.  I have been writing recently to try and learn and refresh my knowledge on technologies as way to improve the code I write.  BUT this brings up a big question, does knowing a lot of technologies make you a better coder?  I would assume that the more you know makes you more valuable since the company you work for would not need to train you, but does this make you a better coder?  I don’t think so because you could still write messy code that is difficult to maintain. 

So in my quest to better the code I write, I have been looking at ways of improving and making sure that code I am writing does not have the following smells:

Duplicate Code

One of the best indicators for me is code duplication.  I am sure that people add duplicated code into an application for many reasons: not enough time to extract the logic into a common method library, ease of copy and paste etc.  The reason I put this at the top is that it when you come to maintain an application that has a lot of code duplication or duplicated logic it show on the amount of time it takes to do simple changes.  Many times changes that have been made to a system will not have been updated in all the duplicated methods a lead to easily avoidable bugs being introduced.  The Principle DRY: Don’t Repeat Yourself is a good way of making sure when you are coding to try and look at the bigger picture.  It may take a little longer when you are initially writing the code, but I promise it will save time in the long run.

The God Object

This is something that can creep into any application.  My guess is that the coder is trying to make it simple and keep related logic in one place or the team is being lazy when adding to existing factories/objects.  The problem with this is that you end up with a method that does EVERYTHING and again becomes a nightmare to maintain.  I have seen coders propose frameworks that would be guilty of this, that down the line cause maintenance teams endless hassles.

Overly Long methods

This is closely tied to the god method I mentioned above.  It refers to methods with too much logic and there are hundreds of lines long.  Obviously when another developer comes along they struggle to understand what the purpose of the method is and how everything works.  A solution to this would be to try and break up the method into components that help make it more manageable.

Large chunks of commented out code

I haven’t seen too many complaints regarding this but it i cant stand it when I see it.  What is the point of leaving code commented out for a considerable time.  I obviously don’t mind code commented out if it is causing a problem or will be reinstated after a short period.  But large chunks of code commented out for large periods of time makes no sense.  Most projects I hope are in some kind of source control.  If that is the case the developer could go back find the code if it is needed again and correctly labelled / branched?

Global Variables

Global variables can be bad for many reasons,  I will relate the issues I have had with them.  The reason they show bad practice is that they can be accessed and changed from any method.  They can be changed anywhere not to mention shadowed and increase the likelihood of bugs.  Please don’t misunderstand me,  I have no problem with properties on web forms.  The issue I am talking about would be variable that is used by multiple methods i.e. a Data Set that get set and reset in different places.

Throwing an exception in a catch block

I have never really understood this practice.  If you need to make sure that you are closing streams or there is code that needs to run you can and a finally clause.  The code I am talking about is:

        try
        {
            throw new Exception("Test");
        }
        catch (Exception exception)
        {
            throw exception;
        }
        finally
        {
            //Code that needs to run
        }

 

Re-throwing the exception destroys the stack trace information and causes headaches when trying to debug the error.

If you need to log the error you could use the following to re-throw the error without destroying the stack trace:

        try
        {
            throw new Exception("Test");
        }
        catch (Exception exception)
        {
            Logger.LogError(exception)
            throw;  //Preserves the stack trace
        }
        finally
        {
            //Code
        }

 

Non Descriptive Variable Names

This has to be my pet hate.  In a few projects I have looked at the naming convention seems to be the use of single char variable names as in int a; string b;.  This isnt minified JavaScript, this was working c# code.  There must be some reason people do  it, maybe they want people to not meddle with their code or make it hard to understand.  Personally I stay away from code that does this simply because making any changes could cause errors.  Not to mention that trying to remember what variable is what or used for what can be difficult.

I was going to add Non descriptive Method names as in two or three letter method names but I think it could all go under the same category since it is really the same thing.  I think it goes without saying that while reading through code that references the method ssd which gets back the variable w and goes on to use it in a method named Adw does really go a long way in making the code easy to understand.

In closing the reason I am bringing up these points is that I want to have pride in the work that I do.  I do see it as a work of art.  Not only are we creating things, but they are useful (hopefully) to people that need to get their job done. 

I am always keen on suggestions and ways of keeping my code clean and practices that aid this, so if you have suggestions please reply to this post.

kick it on DotNetKicks.com

A Guide to implementing NHibernate with MySql

My company has recently decided to update the Data Access of our application and I have started looking into ways to design and structure the new Business Logic and data access.  Currently we are looking at using NHibernate, so this is my guide to setting up NHibernate.  I have attached a working solution to this post.  Please feel free to download it and have a look:  Project Link.

Basically NHibernate is a way of managing information from an application to a relational database.  It is part of Hibernate.Core developed for the .Net platform.  The way NHibernate works is by mapping the database to the objects with the use of XML files.

The first thing to set up is the Config file.  This either goes in the web.config or the app.config:

Config

The config section handles the connection string.  This can be set programmatically using the configuration properties when setting up the session.  Things to be aware of in the config file are the mapping assembly which will tie up the database with the business objects that are going to be used in the application.  The config file also defines the type of database driver class to use.  This allows the developer to change the database type to be almost any database in use today.  I have used MySql for this example but NHibernate supports a large number commercially available databases. An important note here is that the MySql.Data.dll library should be in the bin folder (with the NHibernate libraries). Lastly as I have mentioned is the connection string.  This is the same format as any ASP.NET application.

The following Code would be useful if you don’t want to set up the connection in the config file or only know it at run time:

NhibernateProperties

The next step is defining the business object.  The is a fairly self explanatory process and could be automated using code generation tools unless you have a lot of patience.  The idea here is to match the fields in the table(s) with the properties you want to be available in the object.  I generally suggest using a partial classes.  The reason for this is that if there is any class logic you want to include in the object and you need to re generate the class for any reason, you won’t lose the changes you have made.

Once all the objects have been created, the xml mapping files will need to be created.  The format of the xml document should be as follows:

HbmFile

The mapping links the Database table(s) to the object.  It does this by specifying the assembly to use as well as the class.  Obviously then the properties are matched with the columns.  You can have multiple tables in an object, so you are not restrained to one class per table.

Once the mappings have been done the last step is to create the data access object.  Using NHibernate here is really simple and show the incredible flexibility and the power of NHibernate.

To access the database, a session object will need to be created.  This creates a session for use with the database.  It will be stored in the application cache or context if the application is a Web application.

The rest is really easy.  To save and object all you do is call the save method on the session object and pass the object.  The same applies for the delete and update.  The loading of data is incredibly flexible and by default is lazy loaded.  The easiest method of loading data would be getting the object by id, or using the criteria object in NHibernate or using the inbuilt query language.  Examples of this are:

SelectMethods

So using NHibernate shows that you are able to add a flexible simple data access solution to you project.  I am amazed by the ability to be able to change or add to the database without many detrimental effects on the project.  NHibernate makes coding simple and effective.  I would love to hear comments from people who have used NHibernate in projects, and how it went.

kick it on DotNetKicks.com

Unit testing

Almost anyone working in development these days will say that unit tests are an integral part of any application development.  I think  the reality of a lot of application development does not have unit testing.  It would be interesting to find out how many projects have unit testing and what the test coverage is.

The point of this post is not to complain about the lack of unit testing but to look at the process of creating quality unit test for a project.

The first thing to note is that what most people think is a unit test, is actually an integration test.  The best way to explain it would be give the definition of both a unit test and an integration test. 

A unit test: is a method that calls another method and tests the outcome of the method against a set of assumptions.

An integration test: is a test that tests a set of dependant modules.

It becomes obvious when you look at the definitions that when people try and write unit tests they inadvertently create integration tests.

The obvious benefit of unit testing is a reduction of bugs.  You might say that it would be obvious but it should be mentioned since it can show bugs with the push of a button or a build environment.  This allows teams to make sure that enhancements or changes will not break existing functionality.

It also makes code easier to understand.  The tests themselves give the programmers the understanding of what the methods should be doing.

There are a number of good guidelines to writing unit tests on the internet.  Here is some of the wisdom of the internet:

Make sure that the test is not inadvertently testing more than one method.  This includes loading data from the database, using the file system in any way.

Make sure that the tests can be run easily without any config file.

A clever way of making sure you are creating unit tests and not integration tests can be achieved by using Stubs.

Stubs are a replacement for a dependency that the method that needs to run.  To be able to use stubs, the code must be written in such a way that different providers can be used as dependencies.  By this i mean if the code was checking a folder for a file, the dependency might be replaced by a stub that provides the code with a way of interacting with a fake file system so that the unit test remains a unit test.

The last thing I would like to mention is Test driven design.  Test driven design works by writing a test to solve a particular problem.  The developer would write the test case that would fail since there is no code, then write the code to make the test pass.  By writing tests first the project should have a high code coverage.  Not to mention the fact that by writing the test, the developer focuses on the task at hand which is only to make the test pass.

This is by no means a comprehensive article on unit testing but I hope that it helps people on their way to 100% test coverage.

kick it on DotNetKicks.com

Linq To Data Sets

I have been slowly moving the system I am working on from old legacy DataSets and DataTables to adding a bit of linq.  Here is a quick and simple guide to using Linq with DataTables:

Firstly you need to make sure that the project is using Framework 3.5 and has the following references:

References

The next part is really easy.  You would use linq as you normally would:

var variable = from datarow in dataSetVariable.Tables[0].AsEnumerable()
where (datarow.Field<int>(”Id”) == id)
select new
{
id = datarow.Field<int>(”Id”),
property = datarow.Field<int>(”property”)
};

then you can iterate through the list as you would normally:

foreach (var  item in variable)
{
string property = item.property;

}

Hope this is usefull.

kick it on DotNetKicks.com

MySql Revelations

I am used to MS Sql when writing web based applications but recently I have started working for a company that uses MySql.  This has been fairly similar in most cases but there have been some differences that I have battled to find answers to online so I thought I would put them here and hopefully they will help someone else (like me) who is newish to MySql.

 

The first issue was the declaration of variables.  It seems that MySql doesn’t require the declaration of variables (Although I think they then become global which is probably a bad thing if you are reusing the variables,  I will look into this). 

So in Ms Sql, DECLARE @Var int

In MySql you dont need the declare, you can just set it Directly: SET @Var = ‘somevalue’;

 

Once I was over the first hurdle I came up against the setting the variable in a select statement.  I was trying the following (which didnt set the variable)

SET @Var = 1;

SELECT @Var = 5;

SELECT @Var;

The result was 1.  The solution I found was the following operator: :=

So then the follow worked for me:

SET @Var = 1;

SELECT @Var := 5;

SELECT @Var;

 

I hope that this helps.  If you have more revelations for a MySql starter then please let me know and I will post them here to help all the new guys.

kick it on DotNetKicks.com

Linq to Xml

I have recently used linq to Xml on the application that I am using and found it really easy to use so I thought I would put together a quick how to guide:

The history to this is that we have upgraded to use Visual Studio 2008 from 2005.

The first hurdle I had was to make sure that the project was using the .Net 3.5 framework.  This can be done by right clicking on the project and selecting the framework:

Framework

The second issue I had was to make sure the project had references to the required libraries:

Usings

The rest is really easy, The follow is how I read the xml document:

work

I noticed that the var statement had the property statement on it from the select.  Which makes it easier if you have multiple properties on Xml elements i.e if the xml was:

<Statement>

<Table>Person</Table>

<Sql>SELECT Name FROM Person</Sql>

</Statement>

So the object then should have the properties “Sql” and “Table” if they are selected.

Hope this is helpful.

kick it on DotNetKicks.com

Interesting Read

I have updated the links section of the site to include a site that I have been reading lately.  Check it out:

http://www.netbookexpert.co.uk

Caching in .NET

The reason for this blog is that we are starting to look at ways we can speed up the application we use at work.  One of the ideas we came up with is Caching.

So I thought I would do some research into the types of caching in NET and how it is used:

Output caching:

Caching of a page.  The inner working of this is caching the final rendering out of the page so that the server does not have to run through the page life cycle to generate the page again.

There are options to vary this cache using querystring parameters.  Using the vary by param option will allow you to cache different versions of the page depending on params as well as only one version.

Example:             <%@ OutputCache Duration=”60″ VaryByParam=”None” %>

This will cache the page for 60 minutes and will not vary the cache based on any query string parameters

The vary by param options can be used in the following ways:

//This will cache different versions of the page when the following parameters are in the querystring: Id and SourceId

<%@ OutputCache Duration=”60″ VaryByParam=”Id;SourceId” %>

//This will vary the cache for all variations of the querystring

<%@ OutputCache Duration=”60″ VaryByParam=”*” %>

Caching:

This allows the user to load items into the cache and it will be accessable from any page on the site.  The using of this is simple:

//Caching the variable valueToCache

Cache(”CacheKey”, valueToCache);

//Or

Cache["CacheKey"] = valueToCache;

//Loading the cached Variable

object valueToCache = Cache["CacheKey"];

Caching with dependencies:

This is similar to the previous explanation of caching except there are exceptions on how the data is cached and when it is valid.  So this can be used to cache a file until it changes so that when the file is changed it will renew the cache.  The other main option is to invalidate the cache after certain time period.

//Caching with a file dependency

Cache.Insert(”CacheKey”, valueToCache, New CacheDependency(Server.MapPath(”file.xml”)))

//Caching with a time dependancy of 1 minute

Cache.Insert(”CacheKey”, valueToCache, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero)

SQL Dependency Cache:

The SQL Dependency Caching works on the same principle as the previously discussed caching with dependencies.  The options available with caching are polling and notification.

The notification option is where the database will notify the application when the cache needs to be refreshed.  Full versions of SQL Server 2005 have notification.

The polling option is where the application polls the database to see whether the data needs to be refreshed.  The polling option requires more set up than the notification but if you are using SQL server express then this is a good option.

To set up the server, you will need to set up the database to use polling.  A good guide for this can be found here: http://www.asp.net/learn/data-access/tutorial-61-vb.aspx

AppDomain Cache:

The advantage of this cache, which is little – known to most .NET developers, is that any of your related components or classes in any satellite DLL to your process all have access to this                same Cache since they are all running under the same App Domain. You can store or retrieve virtually any object in AppDomain with the following sample code:

System.AppDomain.CurrentDomain.SetData(”mystuff”, myType)

myType =(myType) System.AppDomain.CurrentDomain.GetData(”mystuff”)

Session vs Caching:

I have included this last section due to the fact that some of the applications I have see use the session as a means of caching data for use later.  The point is to show the differences of using the session compared with the Cache.  Firstly the session is obviously only available for that session.  Another interesting point is that the cache does not serialize objects.

It simply stores references to them. The only overhead is the name lookup, which unless you store thousands of objects is a very quick operation. The session on the other hand serialises objects so there may be some overhead.

I hope this is a useful guide.  Let me know whether there is anything I have forgotten or should have included.

kick it on DotNetKicks.com

Visual Studio 2010 and .NET 4 Features

opening

I thought that since this has come out and a lot of people have been writing about it, I would jump on the band wagon and do a little write up on what I have read and seen so far.

Aside from the new look and opening screen there have been an number of improvements into this release.  This is not an exhaustive list of all the new feature, just the interesting ones that I found when I was looking for the new features:

  1. Jquery included visual studio.  I think most people know this, but still worth mentioning as jquery is awesome.
  2. Client-script loader.  This loads all the required scripts by a control and executing them in the correct order.  The loader also improves performance by loading the scripts in parallel.
  3. Smaller web.config files. web-config From what i have read all of the handlers and modules that are needed are automatically loaded without having to specify them in the file.  Personally I prefer smaller simpler files.  Make understanding what is going on a bit easier.
  4. Works side by side with previous versions the .Net framework and versions of visual studio.  I have installed it on my development machine and have not noticed any problems with current projects in .NET 2 and 3.5. Along side the ability to install the different versions, you can use the new interface to develop in any version on the framework.
  5. Gzipped session state.  When using the session in a web farm, there is an option to specify providers so that the state is the same across the the servers.  Now there is the option to compress the session state using built in gzip.
  6. Response.Redirect vs Response.RedirectPermanent.  The difference of these two redirecting methods is the status code that is sent back to the browser.  The Response.Redirect sends back 302 Temporary redirect.  Where as Response.RedirectPermanent sends back a 301 Moved permanently response which is good for search engines keeping track of the website.
  7. The new release has also included the option of keeping track with build servers.
  8. 2010 has added a really cool new feature called URL rerouting.  Essentially this feature enables the developer to reroute a request for a page (with parameters) ie www.url.com/page?parameter=value as www.url.com/page/value.
  9. A new charting control has been added to the tool box which enables developers to add .NET charts fairly easily to applications.  I have tried this but ran into a few problems.  It does seem to work in much the same way as other controls.  When I get it working I will update.

I really like new improvements and should make coding a lot more fun.

WCF web service and multiple host headers

I recently started using WCF web services to send data from the database to JavaScript.  The service seemed to work fine on the local development environment.  As soon as I deployed this to a website it stopped working.  I couldn’t figure out what the difference was, the environments were essentially the same, i.e. the 3.5 framework was installed, and the same version of IIS was installed.  Eventually I found that there is an issue with WCF services and multiple host headers.  By this I mean if you have to sites pointing to the same site i.e. www.makecodingeasy.com and www.makecodingeasy.co.uk pointing to the same site in IIS then the service seems to break.  As soon as I move the service to its own site it seemed to work.

The reason that I can find for this is that WCF does not support multiple host headers.  I guess for the moment the service will have to have its own site that the web sites reference.

If anyone knows a fix for this please let me know.