Thursday, June 20, 2013

Nullabe Guid?

I saw this today:
    if (myObject.ResourceValue != null)
    {
        otherData.Value = myObject.ResourceValue.ToString();
    }

and wondered if it is possible to get to this situation. So I tried it out myself and found that you will never have a null, non-nullable guid. See below:
namespace ScratchConsole
{
    public class Haha
    {
        private Guid _value;

        public Guid Null { get { return _value; } }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            Guid? test = null;
            //Guid data = test.Value; // throws an exception.
            //Guid data2 = null; // won't compile
            Guid data3;
            //Console.WriteLine(data3.ToString()); // won't compile, unassigned variable.

            Haha ha = new Haha();

            Console.WriteLine(ha.Null.ToString()); // outputs "00000000-0000-0000-0000-000000000000"
        }
    }
}

It seems that instead you should always check for an empty guid. Like so:
    if (myObject.ResourceValue != Guid.Empty)
    {
        otherData.Value = myObject.ResourceValue.ToString();
    }

Sunday, April 28, 2013

Generating TinyUrl Like Id's

I was looking into generating id's for a personal site that I'm doing for fun. The first thing I did was Google around a bit to see what other people are doing. There were some pretty good ideas out there, most of them taking a url and hashing it to a Base64 representation.

That was fine and dandy, but the methods they were using were long and hideous. I'm a huge fan of short and simple approaches because odds are, it's already been done by Microsoft. Since everyone was talking about hashing I figured I would see what this was about in my search result. How to Hash Passswords

It was still a little too complicated. I knew there had to be something simpler. Then I realized that on the Path object you can generate a temporary file name. It's pretty short. I tried it out and it was exactly what I was looking for! I just had to chop off the portion I wanted, since I wanted to restrict it to 7-8 characters.


Friday, April 19, 2013

Small Code

Here's a snippet from a post I took offline.

Small Code
"Functions should do one thing. They should do it well. They should do it only."

I feel like this is an overused term. It doesn't help that I also feel cursed by what my professor has taught me, "Try to keep your methods down to three to five lines." No matter how many times a fellow developer will compliment how nice my code is, they will argue to the death that you can't possibly have those kind of restrictions.