Luke – ITSVopfOLBC ?

Posted by Monty on January 6th, 2012

What on earth does that mean? ITSVopfOLBC ?

Well here’s a description:

I – Indexed
T – Tokenized
S – Stored
V – Term Vector
o – offset
p – position
f – Omit Term Frequency
O – Omit Norms
L – Lazy Loaded
B – Binary
C – Compressed

In case you ever wondered!

Counting number of records – Int or UInt?

Posted by Monty on December 14th, 2011

Lets say, you wanted a count for the number of records/customers/orders/Whatever else you have in your database/rdbms/nosql database/file system, how would you achieve that? 99% of the time, you would use something like this:


public int CountNumberOfRecords()
{
// Fancy code goes in here that does what it should do.
}

But, is using int the right thing to do? Int(32) has possible values from -2,147,483,648 to 2,147,483,648 – do you really need the negative? I don’t know about you, but I never have a negative number of rows in my database!

What you should be using is :


public uint CountNumberOfRecords()
{
// Fancy code goes in here that does what it should do.
}

What is the difference I hear you ask? Well a UInt (UInt16, 32, 64) is an Unsigned Integer – it does not contain a “bit” that can be used to signify that the number is a positive or a negative – the max value now is 4,294,967,295 – double!

The only issue with using a uint is that it is not CLS compliant – in other words, other languages that use the .net framework might not support UInt, so you might have to decorate your methods with [CLSCompliantAttribute(false)]

Que? No hablas

Posted by Monty on October 6th, 2010

So, let me get this straight.

You cannot delete this object because you will go and recreate it because of its associations?

Have you thought that maybe…

.. And I know this is a VERY crazy thought…

That I want to GET RID OF THIS RECORD and its Associations?

nHibernate sucks. It sucked 4 years ago when I used it, and it sucks bigtime now too.


public void ActuallyDeleteEmail(int emailId)
 {
 ISQLQuery sqlQuery = Session.CreateSQLQuery(string.Format('DELETE FROM [TABLE] WHERE ID={0}', emailId.ToString()));
 sqlQuery.ExecuteUpdate();
 }

OH! MY! GOD!

YOU CAN ACTUALLY DELETE THE RECORD? WHO KNEW?

Introducing ServerAdmin

Posted by Monty on September 1st, 2010

Its not quite ready for release right now, but basically, its a really lightweight web based management system for all the sites you have running on your box – so you no longer have to load up IIS Manager and dig through there, for simple quick things like adding a new site, recycling an app pool, or stopping a site.

Here are a few teasers:

Cognitive Bias, or Why you should get someone else to test your code

Posted by Monty on August 8th, 2010

This happens to be a frequent topic for discussion, where people either believe they shouldnt have to get someone else to test their code, or why they should get someone else, who isnt biased, to test their code.

I am a very strong believer in getting someone else to check my work. I am the one who written it, I know what its supposed to do, and I am biased in weather checking if it will work, or not. It is always better to get a second pair of eyes to check over something that you have written / implemented, just to make sure its working correctly. Im not the only one who likes to do this. Pharmacists HAVE to get someone else to check to see if they have dispensed the right prescription drug. Radiologists have to get a 2nd opinion to see if there is a fracture on a bone. In Air Traffic Control, there are computer systems, to make sure that two planes do not try to occupy the same space at the same time.

Take this phenomenon called Controlled Flight Into Terrain (CFIT). Its very interesting, and a bit gruesome. You take a perfectly good plane, fully working and fueled up, and you put a pilot into it, and he might fly it into the ground (or a mountain, or the sea, or an obstacle), in a controlled manor. According to Boeing, its one of the leading cause of airplane accidents and loss of life. This can be caused by a number of things, pilot disorentation, loss of situational awareness, minor problems manifesting themselfs.

Basically, the soft squishy human can be wrong. Two humans are less likely to be wrong than one, 3 more than 2, etc.

Get someone else to test your code. Seriously.

Comments considered harmful

Posted by Monty on May 10th, 2010

I often have this discussion with less learned members of staff, with regards to commenting code. The main argument is “Comment code so I can see what is going on!” and “If you don’t comment the code, I dont understand”. In the first instance, I recommend refactoring the code and rewriting it, so you do not NEED the comments, and usually (99.99%) with the second argument for commenting is, If you do not understand the code, should you really be touching it?

There is the 3rd style of commenting I sometimes come across, and that is the Newbie Comment. Typically, the code has been lifted from a coding website, and someone has left the comments in there, because they either dont know how to use their text editor, or they dont understand the code they are coping and pasting. Both of which, to me at least, indicate that the person should not be writing code and should spend more time understanding the framework and how things work in general, like the following:

//create file object
 FileInfo fi = new FileInfo(filePath);
 //read the file into a stream reader
 StreamReader sr = fi.OpenText();
 //store the contents in a string
 string emailBody = sr.ReadToEnd();
 sr.Close();

The most common comments I see are totally useless, and simply repeat what the line below will do, for example i++; //increment i. How is that comment at all useful? Its not, it just tells you what the line is doing, which the line itself should tell you. Its duplication of code, you are repeating yourself, which is violation of the DRY principle:

Don’t Repeat Yourself

From the wonderful book of The Progmatic Programmer. The comment adds nothing to the overall readability of the code, so it shouldnt be there, it is simply extra clutter to make things more difficult to read and maintain.

Making code easier to maintain, read

Posted by Monty on May 7th, 2010

Often, when working with someone else’s code, it works, but it isnt easy to maintain and read. Basically its ugly, and sometimes its like an ugly bag of snakes. Take this example:

Note: This code has been changed to protect the guilty.

[HandleError]
 public ActionResult LogOn(string username, string password, bool rememberMe)
 {
 string failureText = _websiteService.Login(username, password, rememberMe);
 if (!string.IsNullOrEmpty(failureText))
 {
 TempData["LoginFailure"] = failureText;
 return RedirectToAction("CheckoutAddress");
 }
 else
 Response.Redirect(Url.Action("CheckoutAddress", "Orders"));

 return View();
 }

Now, to me, this rings alarm bells, for multiple reasons. First off, you have failureText which claims to login, but actually gets you if it failed or not. Next thing you have is the “If there is no failure text, then it has worked”. Thirdly, you have the defunct return view, even though it shouldnt really be there.

How should this code be structured I hear you cry? Well, something like this:

[HandleError]
public ActionResult LogOn(string username, string password, bool rememberMe)
{
 if (AreCredentialsValid(username, password) && IsUserAllowedToLogin(username))
 {
 SetAuthenticationCookie(username,password);
 return RedirectToAction("CheckoutAddress");
 }
 else
 {
 ShowInvalidCredentialsPanel();
 return RedirectToAction("CheckoutAddress");
 }
}

Much more easier to read, much easier to maintain and modify, and no having to look for magic strings and just assuming someone can login because there wasnt an error.

Creating a CI server using CC.Net Part 1

Posted by Monty on February 24th, 2010

For the CycleMania project, I previously used TeamCity, but I think it is more geared towards building static projects and running tests rather than deployment, so this time around im going to be using CruiseControl.NET

Note – I am doing this and writing this at the same time :)

To get started, you need to install the following:

  • Subversion for windows, command line, from Collab.NET
  • NAnt, I have version 0.85 installed, and put the path into the PATH environment variable, I am NOT using 0.86beta1 because of this error:
NAnt 0.86 (Build 0.86.2898.0; beta1; 12/8/2007)
Copyright (C) 2001-2007 Gerry Shaw

http://nant.sourceforge.net

BUILD FAILED

Failed to initialize the 'Microsoft .NET Framework 2.0' (net-2.0) target framework.

 Property evaluation failed.
Expression: ${path::combine(sdkInstallRoot, 'bin')}
 ^^^^^^^^^^^^^^

 Property 'sdkInstallRoot' has not been set.

For more information regarding the cause of the build failure, run the build again in debug mode.

Try 'nant -help' for more information

Even though we do NOT need the .net 2.0 SDK! Very poor show indeed.

  • CruiseControl.Net – you probably want to set the admin password to something else from the config file loctated in the webdashboard directory (inside where you installed cc.net), the file is dashboard.config

Once you have the above monkies installed, you want to do a checkout of the CycleMania source code to a directory, like so:


svn co https://cyclemania.svn.codeplex.com/svn/trunk E:\SVN\CycleMania

Replace the E:\SVN\CycleMania with wherever your subversion repository lives.

For sake of argument, I have my build scripts living in E:\SVN\CycleMania-BuildScript, and the file is called CycleMania.build

Once you have done that, go into the build script, and type in nant, and you should get the following:

E:\SVN\CycleMania-BuildScript>nant -buildfile:CycleMania.build
NAnt 0.85 (Build 0.85.2478.0; release; 10/14/2006)
Copyright (C) 2001-2006 Gerry Shaw

http://nant.sourceforge.net

Buildfile: file:///E:/SVN/CycleMania-BuildScript/CycleMania.build
Target framework: Microsoft .NET Framework 2.0
Target(s) specified: build

build:

 [echo] Building Target
 [exec] Starting 'c:\Windows\Microsoft.NET\Framework64\v4.0.30128\MSBuild.exe ( E:\SVN\CycleMania\CycleMania.sln)' in 'E:\SVN\CycleMania-BuildScript'
 [exec] Microsoft (R) Build Engine Version 4.0.30128.1
 [exec] [Microsoft .NET Framework, Version 4.0.30128.1]
 [exec] Copyright (C) Microsoft Corporation 2007. All rights reserved.
 [exec] Build started 2/23/2010 3:47:11 PM.
 [exec] Project "E:\SVN\CycleMania\CycleMania.sln" on node 1 (default targets).
 [exec] ValidateSolutionConfiguration:
 [exec]   Building solution configuration "Debug|Mixed Platforms".
 [exec] Project "E:\SVN\CycleMania\CycleMania.sln" (1) is building "E:\SVN\CycleMania\Cyclemania.Web\Cyclemania.Web.csproj" (2) on node 1 (default targets).

 [exec] E:\SVN\CycleMania\Cyclemania.Web\Cyclemania.Web.csproj(649,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\VisualS
tudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exi
sts on disk.
 [exec] Done Building Project "E:\SVN\CycleMania\Cyclemania.Web\Cyclemania.Web.csproj" (default targets) -- FAILED.
 [exec] Done Building Project "E:\SVN\CycleMania\CycleMania.sln" (default targets) -- FAILED.
 [exec] Build FAILED.
 [exec] "E:\SVN\CycleMania\CycleMania.sln" (default target) (1) ->
 [exec] "E:\SVN\CycleMania\Cyclemania.Web\Cyclemania.Web.csproj" (default target) (2) ->
 [exec]   E:\SVN\CycleMania\Cyclemania.Web\Cyclemania.Web.csproj(649,3): error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft\Visua
lStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
 [exec]     0 Warning(s)
 [exec]     1 Error(s)
 [exec] Time Elapsed 00:00:05.22

BUILD FAILED

E:\SVN\CycleMania-BuildScript\CycleMania.build(7,4):
External Program Failed: c:\Windows\Microsoft.NET\Framework64\v4.0.30128\MSBuild.exe (return code was 1)

Total time: 12.9 seconds.

HUH?!? What the hell happened there? Well in the infinate wisdom of Microsoft (which I have blogged about before), they did not decide to include the Microsoft.WebApplications.targets file anywhere to be found on the hdd, and you have to dig it out from the vs.net install, or you can find it from this handly link! Unzip this into your Program Files or Program Files (x86) if you are fancy and have a 64bit proc.

Once you have unzipped that lovely glorious file, and run the NAnt script again, you should see a better output, with LOADS of stuff hapenning, and hopefully at the end, these GLORIOUS WORDS:


BUILD SUCCEEDED

Total Time: 11.6 seconds

Note: Your time may vary.

In Part 2, I will describe how to set up your SQL 2008 instance up, user accounts (NOTE, I would do this properly as opposed to the hacky “lets get the site to work” way), and IIS…

Resharper and its “Convert to LINQ expression”

Posted by Monty on January 23rd, 2010

I guess im a bit old fashioned, but I like to write code like:

public static List<string> CovertLongListToString(IEnumerable<long> param)
 {
 List<String> returnList = new List<string>();

 foreach (long l in param)
 {
 returnList.Add(l.ToString());
 }

 return returnList;
 }

But then resharper 5.0 gives me this lovely option of “Convert to LINQ Expression”, and it turns into:


public static List<string> CovertLongListToString(IEnumerable<long> param)
 {
 return param.Select(l => l.ToString()).ToList();
 }

Thats just brilliant! I LOVE YOU RESHARPER!

Deployment process on CycleMania

Posted by Monty on January 2nd, 2010

As I have mentioned earlier, I am using a NAnt script to do the building for CycleMania, as I believe that its more geared towards what I need it to do than TeamCity. While TeamCity is a great product in itself, I believe that it either cannot do, or doesn’t make clear, how to copy over files from the build directory into another directory (this being the wwwroot of the website).

The reason why I dont want to point the wwwroot at the subversion root, is for security (.svn folders are browsable by default, and not parsed by asp.net), and because of the way that TeamCity works – when it attempts to do an update, it isnt clear on how to get it to revert all changes made (i.e. to the database), and the database file will be locked, with no way of making it available on the fly.

So, in jumps in NAnt. I know, I could use MSBuild Scripts, or powershell scripting, but I have been using NAnt for some time, and I know it rather well, and its free and runs on machines fairly easily.

A run down of the CycleMania NAnt Script

To start off with, we build the solution file. There is no point doing anything at all if the solution does not compile, so we run the following, After setting a few variables:


<echo message="Building Target"/>
<exec program="${MSBuild.Path}"  basedir="${BaseDir}"  verbose="true" >
<arg value="${SolutionPath}"  />
</exec>

For most of the tasks here, I have verbose set to true, because I want as much information as possible in the log file to see if everything worked, and if it didn’t, why not.

After this, it calls deploy, which has a dependency on clean which does the following:

<target name="clean">
 <exec program="${AppCmdPath}"  verbose="true">
 <arg value="STOP apppool  &quot;${ApplicationPool.Name}&quot;" />
 </exec>
 <delete verbose="true">
 <fileset  basedir="${DestinationDirectory}">
 <include name="*.*"  />
 <include name="**/*.*" />
 </fileset>
 </delete>

<exec program="${AppCmdPath}" verbose="true">
 <arg  value="START apppool &quot;%{ApplicationPool.Name}&quot;" />
 </exec>
</target>

The reason why we insist on shutting down and starting the application pool back up is because of MSSQL and IIS and their relationship. For some reason, if you simply recycle the application pool, it dosent stop the database that is bound to it (a user instance), so you cannot simply update the file in the App_Data folder, as I found out the hard way, which eventually led me down this path. It seems that if you want to be able to properly delete everything, you can kill the app pool, and it will unlock all the files. Because we are ussing IIS 7.5 on this box, we have to ues appcmd (google for it!), which is alot better than using iisreset, especially since I have other sites running on the box ;)

One thing I have considered doing, but dont really have time for, is maybe copying over an app_offline file, so that if someone does hit the site while its being wiped, they can see a friendly error page as opposed to an empty directory listing, or something worse than that, but at the moment, that is low priority for me.

And now, deploy gets called:

<target name="deploy" depends="clean">
 <copy  todir="${DestinationDirectory}" verbose="true">
 <fileset  basedir="%{BaseDir}\Cyclemania.Web">
 <include name="*.*"  />
 <include name="**/*.*" />
 <exclude name="*.cs" />
 <exclude name="*.resx" />
 <exclude name="*.csproj" />
 <exclude  name="*.projdata" />
 <exclude name="*.sln" />
 <exclude name="*.csproj.user" />
 <exclude name="*.suo" />
 <exclude name="*.scc" />
 <exclude name="*.load" />
 <exclude  name="*.vssscc" />
 <exclude name="*.vspscc" />
 <exclude name="obj\**" />
 </fileset>
 </copy>
</target>

Notice, it will only copy files that are not on the exclude list – 99.9% those files arnt needed on a live system, but you can tweak it as required. Im looking to keep the wwwroot of the application as clean as possible, and I dont see much need to copy those files over.

And there you have it! With this script (and the bits that I have missed out), you should have a fully functioning build script that will automatically build it for you, wipe the target directory, and copy over the new files. And yes, it works.


Copyright © 2007-2010 Muntedhar Alhakim. All rights reserved.