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)]

Error 2 when starting Windows Event Log

Posted by Monty on May 3rd, 2011

… On Windows 7

If you have the above error (Cannot find the file specified) when trying to start the windows event log, then have no fear, for I may have the solution. If you have tried everything else online, then try this fix.

Open up regedit, and delete the following key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Parameters

Basically, a hotfix (not sure which) malforms that registry key, rendering the event log useless.

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:

This is NOT the way you write code

Posted by Monty on August 24th, 2010

Seriously. If I see ANYONE under my employment writing code like this, without a VERY GOOD REASON, I will look at getting rid of you for gross negligance, and possibly criminal acts, under the geneva convention.

    public static DataTable GetAll(String where)
    {

        // get a configured DbCommand object
        DbCommand comm = GenericDataAccess.CreateCommand();

        //define command type
        comm.CommandType = CommandType.StoredProcedure;

        // set the stored procedure name
        comm.CommandText = "GetAll";

        //Create new parameter
        DbParameter param = comm.CreateParameter();
        param.ParameterName = "@tableName";
        param.Value = _tableName;
        param.DbType = DbType.String;
        //Add param to command object
        comm.Parameters.Add(param);

        //Create new parameter
        param = comm.CreateParameter();
        param.ParameterName = "@where";
        param.Value = where;
        param.DbType = DbType.String;
        //Add param to command object
        comm.Parameters.Add(param);

        DataTable dt = new DataTable();
        dt = GenericDataAccess.ExecuteSelectCommand(comm);

        return dt;

    }

If your WHAT THE FUCK meter isnt going off already, wait till you see GetAll:

ALTER PROCEDURE [dbo].[GetAll]
	@tableName VARCHAR(50),
	@allRecords BIT = NULL,
	@where NVARCHAR(1024) = NULL
AS
BEGIN
	SET NOCOUNT ON;

	DECLARE @sql VARCHAR(200)

	IF @where IS NULL
	BEGIN
		-- Insert statements for procedure here
		IF @allRecords = 1
		BEGIN
			SET @sql = 'SELECT * FROM ' + @tableName
		END
		ELSE
		BEGIN
			SET @sql = 'SELECT * FROM ' + @tableName + ' WHERE disable = 0'
		END
	END
	ELSE
	BEGIN
		SET @sql = 'SELECT * FROM ' + @tableName + ' ' + @where
	END

	EXEC (@sql)
END

Seriously. DEAR GOD. HOW THE HELL DID YOU GET A JOB?

public static DataTable GetAll(String where)
{

// get a configured DbCommand object
DbCommand comm = GenericDataAccess.CreateCommand();

//define command type
comm.CommandType = CommandType.StoredProcedure;

// set the stored procedure name
comm.CommandText = "GetAll";

//Create new parameter
DbParameter param = comm.CreateParameter();
param.ParameterName = "@tableName";
param.Value = _tableName;
param.DbType = DbType.String;
//Add param to command object
comm.Parameters.Add(param);

//Create new parameter
param = comm.CreateParameter();
param.ParameterName = "@where";
param.Value = where;
param.DbType = DbType.String;
//Add param to command object
comm.Parameters.Add(param);

DataTable dt = new DataTable();
dt = GenericDataAccess.ExecuteSelectCommand(comm);

return dt;

}

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.

Helicopter on the M25

Posted by Monty on March 18th, 2010

Well, no one seems to believe me, so here are the pics.


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