Sunday, October 21, 2012

Fresh Ideas

At first I was really pissed off, but now I'm finding it amusing. It kind of makes me feel like the world is sane and I'm the crazy one. So now I cackle in hysteria! Not really but you get the point.

What I would like to talk about is what happens when people see an innovative idea. Annoyingly, no matter how good of an idea it is, they can't seem to handle it. So their first instinct is to say, "it's JUST xyz." Let me tell you, I hate that phrase with fiery passion.

Thursday, August 23, 2012

Dictionaries and Hashtables

I see Hashtable being used everywhere it seems. Hashtable is a early data structure in c#, this was pre-.net 2.0.

Ideally we should avoid using Hashtable where possible because thanks to generics, Dictionary, is more efficient. This is because Dictionary does not have to box and unbox the values. If you're not familiar with this you can reference this article. http://msdn.microsoft.com/en-us/libr...=vs.80%29.aspx Dictionary is also a type safe way of storing and retrieving data and therefore should be preferred so as to avoid experiencing run time errors.

There are actually multiple types of dictionaries available:
  1. ConcurrentDictionary - thread safe (can be safely accessed from several threads concurrently)
  2. HybridDictionary - optimized performance (for few items and also for many items)
  3. OrderedDictionary - values can be accessed via int index (by order in which items were added)
  4. SortedDictionary - items automatically sorted by key
  5. StringDictionary - strongly typed and optimized for strings

There are other data structures out there in c# that can be really effective. I highly recommend you read this article multiple times when you get the chance. It's a little dated (.Net 2.0) http://msdn.microsoft.com/en-us/libr...1%28v=vs.80%29 But it's really good and dives deep into how each of the core data structures work.

One of the engineers I work with pointed out something else that's pretty awesome. KeyedCollection, here's what he had to say:

The KeyedCollection uses a Dictionary internally. It is another nice way of managing keyed data.

http://msdn.microsoft.com/en-us/library/ms132438.aspx

http://stackoverflow.com/questions/7...eyedcollection

Monday, June 4, 2012

Meta-Syntax

Over the few years that I've been learning to write software, I find myself often struggling to think in terms of metadata. More accurately, meta-metadata. So it's no surprise that I have found myself struggling to understand Backus-Narf Form (BNF) syntax. I finally decided to sit down and take a closer look at it and I found it surprisingly easier to understand than I initially thought. Read on to see my findings.

Sunday, April 29, 2012

Padding Wrong for Ordered Lists

As I'm making some modifications to my blog, I couldn't help but noticed that ordered lists.
  1. example
  2. example
  3. example
Are messed up with most templates I picked. So I wrote some simple css to fix that issue, under the advanced section of the template manager. I put this in the "Add CSS" text box.

ol {
    padding-left: 40px;
    padding-top: 10px;
}

Just throwing this out there in case someone else was just as confused as me.

Wednesday, April 25, 2012

Serialization Helper Methods

I keep forgetting the classes/methods I need to Serialize/Deserialize objects to and from XML. So I decided to make a couple of helper methods.


/// <summary>
/// Writes the serialized form of the object, in xml format, to the file
/// location specified.
/// </summary>
/// <typeparam name="T">type to serialize from</typeparam>
/// <param name="toSerialize">data to serialize</param>
/// <param name="fileLocation">to serialize the data too</param>
public static void Serialize<T>(T toSerialize, string fileLocation)
{
    if (toSerialize == null)
        throw new ArgumentException("The data to serialize cannot be null.");

    if (string.IsNullOrEmpty(fileLocation))
        throw new ArgumentException("You must provide a file location to output the data to.");

    using (var textWriter = new StreamWriter(fileLocation))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", ""); //turn off namespaces
        serializer.Serialize(textWriter, toSerialize, ns);
    }
}

/// <summary>
/// Reads the data from the file location specified and creates an instance
/// of the object type. The object must be serializable.
/// </summary>
/// <typeparam name="T">type to deserialize the data too</typeparam>
/// <param name="fileLocation">data to deserialize from</param>
/// <returns>new object of type T with properties filled out</returns>
public static T Deserialize<T>(string fileLocation)
{
    if (string.IsNullOrEmpty(fileLocation))
        throw new ArgumentException("You must provide a file location to read the data from.");

    using (var reader = XmlReader.Create(fileLocation))
    {
        XmlSerializer serializer = new XmlSerializer(typeof(T));
        return (T)serializer.Deserialize(reader);
    }
}

They're pretty straight forward to use, but just for clarification.


public static void Main(string[] args)
{
    MyObject obj = Program.Deserialize<MyObject>(@"Files\SerializedData.xml");
    obj.AdjustValues();
    Console.WriteLine("Done!!!: {0} test: {1}", obj.Property1, obj.Property2);
    obj.AuditDirectoryName = "Test";
    Program.Serialize(obj, @"Files\SerializedData.xml");
    Console.ReadLine();
}

Short and Sweet

So today at work I had to come up with a method that would give me a listing of something from a collection. If the listing didn't exist, I wanted the listing to be initialized and automatically added to the list.

Let's look at my pathetic first attempt.


private Listing GetListing(List<Listing> cars, string name)
{
    Listing listing = null;
    IEnumerable<Listing> results = cars.Where(i => i.Name == name);

    //if map doesn't exist create it
    if (results.Count() == 0)
    {
        listing = new Listing()
        {
            Name = name,
            AdditionalData = new List<Data>()
        };

        cars.Add(listing);
     }
     else
     {
         //If we originally did this, it would throw an 
         //exception when there are no matches found. 
         //That's why this part is in the else block.
         listing = results.First();
     }

     return listing;
}

As you can see, I tried to get fancy and use Linq. I found, through testing, that the method .First() would throw an exception if there were no results. There were other methods, but for some reason I just couldn't think of the right method.

So then I threw my hands in the air and figured it'd be shorter to just write a method without Linq and that I was just over complicating it.