Friday, October 30, 2015

.NET Exceptions

This post may seem obvious, but is something that I never knew for sure until today.  When an exception is raised, a stack trace is included as a property of the exception that describes where the exception occurred.  The stack trace will always reveal the method where the exception originated.  Even if Function A calls Function B, and Function B raises an exception that is caught in Function A, Function B will be in the stack trace.  This small bit of knowledge can save a bunch of time, because if Function A is in the stack trace, you can rest assured that it didn't happen in Function B.

Wednesday, October 21, 2015

Git Line Endings

When editing a text file in Windows, the operating system inserts two characters: carriage return and a line feed (AKA CRLF).  In any other operating system, only the LF character is added.  Because some projects are developed collaboratively on different operating systems. Obviously, this poses problems.  Git offers different configurations to deal with this.  With one configuration, Git will automatically replace CRLF with LF upon a commit.  I was stuck with this configuration one day because I believe I mistakenly told it to set it up that way during installation.  You don't want this if you do all your developing in Windows.  To turn this off, use this command:

git config --global core.autocrlf false

Creating and Using a Git Ignore File

Most of the time, there are certain files and folders that you do not want to track in your Git repository.  You have to explicitly tell Git which ones to ignore.  You provide this information in a special file you create in the root of your repository.  It is simply a text file named .gitignore.  Notice that there is no filename, just an extension.  Windows will not let you create a file with no name, so you have to create the file using a command in Git Bash.  Run the following from your root folder:

echo /bin > .gitignore

This command will create a file named .gitignore and put the "/bin" text in it.  This is the start of a very basic git ignore file.  It tells Git to ignore a folder named bin and all files contained in it.  You can add individual files and folders on separate lines.  Just double click the file in Windows and edit in Notepad.

Wednesday, October 14, 2015

Regular Expression Primer

I feel like I've finally cracked the code on regular expressions.  This is my attempt to document the essentials of regular expressions using JavaScript.  A regular expression essentially describes a pattern of text.

Generally, you use a regular expression (regex) in one of two ways:

  1. Test to see whether a specified string matches a regex exactly.  This is mainly used for validating input.
  2. Extract all instances of the pattern from a larger string of text.  The main use here is to replace the instances of the pattern with something else.
The easiest way to perform the first use is to define the regular express on the fly and then call the test method like so:

/^[0-9]+$/.test(inputToTest)

The test method returns true or false, based on whether the input string was a match.  The forward slashes simply delimit the regular expression.  The ^ indicates the beginning of the string, and the $ indicates the end of the string.  [0-9] says to match any number, and the plus means any number 1 or more times.  So, only numbers with one or more digits will match the regex.

The second use often involves replacing the matching text.  You can use a regex as a parameter when calling the string.replace method.  Let's say you want to replace all numbers in a string with the letter x.  Here is how you do it:

var results = inputString.replace(/[0-9]+/g, "x");

What's the "g" for?

That's the global flag.  That says to replace every matching text in the string with "x".

Now, let's talk about parenthesis.  A parenthesis in a regex means you can extract just the part in the parentheses.  Let's say you want to find everything with curly braces, and replace it all with just the text inside the curly braces.

var results = inputString.replace(/\{([A-Za-z]+)\}/g, "$1");

The &1 refers to the text matched by the first parenthesis.  So we would replace "{test}" with just "test".  Notice that the backslash is used to escape the curly brace character.

Sometimes a parenthesis is just a parenthesis and doesn't have to be referred to later.  You can just use it to group stuff.

These are really dumb examples to keep things simple, but these techniques are very powerful.  The best reference I've found for dealing with regex is here.


Monday, October 5, 2015

Use a Comparer to sort a generic List

I use lists a lot and most of the time I can sort the data before I load it into a list.  However, occasionally, I find myself in a situation where I really want to sort the data after it has been put into a list.  You can't use LINQ to sort a list.  You have to use the Sort method of the list object, and this method takes an IComparer object as a parameter.

Let's say you have a simple domain object with two properties:

Public Class DomainObject
    Public Property Name As String
    Public Property StartDate as Date
End Class

Then, let's say you have a List that's been loaded up with a bunch of DomainObject's.  Here's the definition:

Dim MyList As New List(Of DomainObject)

First, let's sort by just name.  You first have to create a Comparer class.  The Comparer class is a bit of code that tells the Sort method how to sort.  (NOTE:  If you're list consists of ONE simple type, like string, you don't have to provide the Sort method with a Comparer class.)  Here's a Comparer class that sorts only by the name:

Private Class DomainObjectComparer
    Implements IComparer(Of DomainObject)

    Public Function Compare(x As DomainObject, y As DomainObject) As Integer Implements IComparer(Of DomainObject).Compare

        '-1 means y is greater (i.e. y is AFTER x)
        '0 means x and y are equal.
        '1 means x is greater (i.e. x is AFTER y)

        If x.Name > y.Name Then
            Return 1
        Else
            Return -1
        End If

    End Function

End Class

This function compares two individual DomainObjects.  If x's Name is greater than y's Name, then we return 1.  When we return a 1, we are saying that x is greater than y.  In other words, x will come AFTER y.  The inverse is true when we return -1.  And if we return 0, they are absolutely equal.

Now, all we have to do is instantiate our DomainObjectComparer class and feed the resulting object into the Sort method like this:

Dim MyComparer As New DomainObjectComparer
MyList.Sort(MyComparer)

Now, our list is sorted by the name property.

If you want to sort first by Name and then by StartDate descending, try this:

Private Class DomainObjectComparer
    Implements IComparer(Of DomainObject)

    Public Function Compare(x As DomainObject, y As DomainObject) As Integer Implements IComparer(Of DomainObject).Compare

        '-1 means y is greater (i.e. y is AFTER x)
        '0 means x and y are equal.
        '1 means x is greater (i.e. x is AFTER y)

        If x.Name > y.Name Then
            Return 1
        ElseIf x.Name = y.Name Then
            If CDate(x.StartDate) > CDate(y.StartDate) Then
                Return -1 'Notice here that it's -1 because we want descending order.
            Else
                Return 1
            End If
        ElseIf x.Name < y.Name Then
            Return -1
        End If

    End Function

End Class