RSS

Levenshtein Distance

a string metric for measuring the difference between two sequences. Informally, the Levenshtein distance between two words is the minimum number of single-character edits (i.e. insertions, deletions or substitutions) required to change one word into the other.

Source: Wikipedia: Levenshtein Distance

Why does this matter?

Levenshtein distance is a form of approximate string matching. In real life application, Levenshtein distance can be used in spell checking, correction in OCR system (Optical Character Recognition), record linkage, auto suggest words in smart phone and many more.

 
Leave a comment

Posted by on July 29, 2014 in General

 

Tags: ,

Differences Between Session.Clear and Session.Abandon?

Session.Clear()

  • Doesn’t trigger Session_End in Global.asax.
  • Doesn’t change Session ID.
  • Will remove all keys and objects in stored in the session.

Session.Abandon()

  • Trigger Session_End event Global.asax.
  • Session ID will be disposed, a new one will be assigned.
  • Will remove all keys and objects in stored in the session.
 
Leave a comment

Posted by on July 28, 2014 in General

 

Tags: , ,

ASP.Net MVC 4 Bundles Ignore List

.min JavaScript Files

It all started when I tried to bundle a .min JavaScript file in my ASP.Net MVC 4 application. MVC just completely ignore any .min files I have in BundleConfig.cs.

Obviously, it doesn’t work. OK, it’s not so obvious when it happened to me.

Ignore List

Turn out, ASP.Net MVC 4 Bundles has ignore list. You guess it, the .min extension is included in the ignore list. To override the ignore list:

public static void SetIgnorePatterns(IgnoreList ignoreList)
{
    if (ignoreList == null)
        throw new ArgumentNullException("ignoreList");

    ignoreList.Ignore("*.intellisense.js");
    ignoreList.Ignore("*-vsdoc.js");
    ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
    //ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
    ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
}

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.IgnoreList.Clear();
    SetIgnorePatterns(bundles.IgnoreList);

    // Continue with your code ...
}

That’s it, the .min JavaScript file will now be included in bundling.

Alternatively, you can also rename the JavaScript to remove its .min extension to be included in bundling process. This approach doesn’t require modification of bundle’s Ignore List.

While we are on the subject, it’s important to note that Bundle and Minification are different process. ASP.Net site explains it as:

Bundling

Bundling is a new feature in ASP.NET 4.5 that makes it easy to combine or bundle multiple files into a single file. You can create CSS, JavaScript and other bundles. Fewer files means fewer HTTP requests and that can improve first page load  performance.

Minification

Minification performs a variety of different code optimizations to scripts or css, such as removing unnecessary white space and comments and shortening variable names to one character. Consider the following JavaScript function.

Source: MVC 4 Bundling and Minification

 
Leave a comment

Posted by on April 18, 2014 in General

 

Tags: ,

What is Prototype?

In JavaScript, objects also have one additional attribute: a pointer to another object. We call this pointer the object’s prototype.

Prototype allows you to create inheritance in JavaScript objects.

var man = Object.create(null);
defineProperty(man, 'sex', "male");

var yehuda = Object.create(man);
defineProperty(yehuda, 'firstName', "Yehuda");
defineProperty(yehuda, 'lastName', "Katz");

yehuda.sex       // "male"
yehuda.firstName // "Yehuda"
yehuda.lastName  // "Katz"

Object.getPrototypeOf(yehuda) // returns the man object

Read more: Yehuda Katz’s Understanding Prototype in JavaScript

 
Leave a comment

Posted by on March 21, 2014 in General

 

Tags: ,

What is CLR?

The Common Language Runtime (CLR) is the virtual machine component of Microsoft’s .NET framework and is responsible for managing the execution of .NET programs.

A language like C# requires the .NET Common Language Runtime (CLR) to execute. Essentially, as an application that is written in C# executes, the CLR is managing memory, performing garbage collection, handling exceptions, and providing many more services that you, as a developer, don’t have to write code for. The C# compiler produces Intermediate Language (IL) , rather than machine language, and the CLR understands IL. When the CLR sees the IL, it Just-In-Time (JIT) compiles it, method by method, into compiled machine code in memory and executes it. The CLR manages the code as it executes.

Read more: Common Language Runtime – Wikipedia

 
Leave a comment

Posted by on March 21, 2014 in General

 

Tags: ,

ASP.NET Web API and OData’s Verbose JSON

OData v3 and Verbose JSON

Whenever you work with OData in ASP.NET Web API, the return result will be JSON format. It’s noteworthy to know that by default Web API will return new OData JSON format specified in OData version 3. For older OData, the JSON format is different. It has more ‘unnecessary’ placeholders. This old OData JSON is also referred as ‘Verbose JSON’. In short, there are two types of OData JSON:

  • OData v3 has new JSON format
  • Verbose JSON is used in older OData versions.

ASP.NET Web API (.Net 4.5) and Verbose JSON

As mentioned before, by default Web API will return new OData JSON.

To change this behavior, you can request Web API to return old OData JSON format by specifying ‘odata=verbose’ in Accept header. Below is the sample of jQuery Ajax call specifying verbose JSON:

$.ajax({
    url: "/api/Products",
    dataType: "json",
    accepts: { json: "application/json;odata=verbose" }
}).done(function() {
    // do work ...
});

How it Looks Like?

For the curious:

Verbose JSON

{
    "d" : {
        "results": [
            {
                "__metadata": {
                    "uri": "http://demos/Orders(10248)",
                    "type": "SampleModel.Order"
                },
                "ProductId": 10248,
                "Name": "Vgnon"
            }
        ],
        "__count": "1"
    }
}

OData v3 (and above) JSON

{
    "odata.metadata":"http://localhost:43111/api/$metadata#Products",
    "odata.count":"1",
    "value":
    [
        {
            "ProductId":2,
            "CreatedDate":"2013-06-10T23:53:26",
            "CreatedBy":0,"Name":"Camera"
        }
    ]
}
 
Leave a comment

Posted by on October 7, 2013 in General

 

Tags: , ,

Compare Two URL Strings

URL String Compare

What is the best approach to compare two URLs and determine if they are the same or not?

For example:
http://stack247.wordpress.com and stack247.wordpress.com are the same URL.
https://stack247.wordpress.com and stack247.wordpress.com are the same URL.
http://stack247.wordpress.com/tag/interview-question/ and stack247.wordpress.com/tag are NOT the same URL.

 
Leave a comment

Posted by on July 31, 2013 in General

 

Tags: , , ,

 
Follow

Get every new post delivered to your Inbox.

Join 38 other followers

%d bloggers like this: