General Tips

Think Outside the Box

Entrepreneur on Fire Podcast Logo
I was listening to episode 682 of the Entrepreneur on Fire Podcast, where John Lee Dumas was interviewing Shane Snow. Shane has written a book called Smartcuts: How Hackers, Innovators, and Icons Accelerate Success. An abbreviated description from Amazon reads:

Entrepreneur and journalist Shane Snow analyzes the lives of people and companies that do incredible things in implausibly short time… One way or another, they do it like computer hackers. They employ what psychologists call “lateral thinking” to rethink convention and break “rules” that aren’t rules.

Shane Snow
During the show, Shane gave a small story to illustrate the general idea of the kind of lateral thinking that he is espousing.

Pretend that you are driving a car on a rainy night and you’re kind of in the middle of nowhere and windshield wipers are going like crazy and you see on the side of the road three people that are standing out there miserable in the rain. You slow down and the first one you notice is a friend of yours that saved your life one time. Next to him is a little old lady with a cane who definitely can’t make it back to town on her own. And next to them is the man or woman of your dreams and this is your only shot to ever meet them. You only have one seat in your car (meaning a car with a total of two seats), who do you pick up?

John Lee Dumas suggested that he would pick up the little old lady. That is just the “military man” in him. Shane’s point was that lateral thinking “hacks” the question and finds the answer by thinking outside the box. In Shane’s solution, you give the seat to the old lady, give your keys to your friend, and stay with the love of your life.

I love that illustration. Yes, it is contrived, but it is a perfect word picture of how creative problem solvers think. The problem that most people have with the problem is that they make assumptions. In this case, you’ve assumed that you must stay with your car and that you must only save one of the three people in distress. Lateral thinking throws out those assumptions and looks for a way for “everyone to win”. In real life, 100% satisfaction certainly isn’t always possible, but you will get a lot closer by throwing out assumptions and “hacking the question”.

Great reminder for the day.

Podcasts

Podcast Episode 22 – In Defense of Stored Procedures

Gavel - Labeled for CC Reuse, From http://upload.wikimedia.org/wikipedia/commons/b/b2/CourtGavel.JPGEpisode 22 finds me talking about Stored Procedures. Stored Procedures have gotten a bad rap not just recently, but for over a decade. Stored Procedure apologists don’t always help the matter and misinformation abounds. I take a look at stored procedures and why it isn’t the worst thing in the world if you use them.

I haven’t forgotten my last podcast about religious arguments and I try to take an honest look at when stored procedures aren’t terrible, when ORMs are best, and when in-line parameterized SQL might be your best option. Too often, I feel that developers are scared of the database because they don’t know it or only have a limited view of what it could be for. In this episode, I look at it from all angles.

Links Mentioned in this Show:
Rob’s Original Post
Frans Bouma’s Reply
Rob’s Rebuttal
Jeff Atwood’s Post
Ayende’s Compromise
Zip Locate
Gooey

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

Code Tips

C# MemberwiseClone

Clone Trooper - Creative Commons License from https://c2.staticflickr.com/4/3484/3796528975_e8c7faaed2_z.jpg?zz=1

If you’ve ever needed to make a copy of an object in C#, you might have come across the MemberwiseClone() method. This can save you a lot of hassle of left-hand/right-hand property matching, but it does come with some “gotchas” that might not be readily apparent if you haven’t carefully read the documentation.

Let’s start with a class that looks like this. Notice that it has a ShallowCopy method that just returns a MemberwiseClone of the object.

public class Person
    {
        public string Name { get; set; }
        public Person Boss { get; set; }

        public Person ShallowCopy()
        {
            return (Person)this.MemberwiseClone();
        }
    }

Now, let’s create some objects and make a shallow copy of the worker and see if the objects are the same:

Person bigBoss = new Person() { Name = "Tony", Boss = null };
Person boss = new Person() { Name = "Silvio", Boss = bigBoss };
Person worker = new Person() { Name = "Paulie", Boss = boss };

Person cloneWorker = worker.ShallowCopy();
Console.WriteLine("Are the workers the same? {0}", worker.Equals(cloneWorker));
Console.WriteLine("Are the bosses the same? {0}", worker.Boss.Equals(cloneWorker.Boss));

This gives us the following results:

Are the workers the same? False
Are the bosses the same? True

“So what?” you might ask. Well, what that means is that if I change a property on the Boss of either object, it automatically changes in the other object (since only the reference was copied, and that reference points to the same memory address in both cases).

worker.Boss.Name = "Chuck";
Console.WriteLine("Worker's Boss' Name: {0}.", worker.Boss.Name);
Console.WriteLine("Clone Worker's Boss' Name: {0}.", cloneWorker.Boss.Name);

This gives us the output:

Worker's Boss' Name: Chuck.
Clone Worker's Boss' Name: Chuck.

It is possible that you may be okay with this happening. However, if you don’t want this to happen, you can implement a DeepCopy on your object instead. In the DeepCopy, you take a MemberwiseClone and then you call for a deep copy on every reference type in the object. In our example, I’d add this to our Person class:

public Person DeepCopy()
{
    Person copy = (Person)this.MemberwiseClone();

    if (this.Boss != null)
    {
        copy.Boss = this.Boss.DeepCopy();
    }

    return copy;
}

Now, when I check on the objects, they aren’t the same. And, when we change the property on the boss, it doesn’t automatically change in both places because they are completely separate objects.

Are the workers the same? False
Are the bosses the same? False

Worker's Boss' Name: Chuck.
Clone Worker's Boss' Name: Silvio.

That’s all there is to it. I have wrapped up the code for this blog post into a small console project and put it on GitHub. If you have any questions, let me know in the comments.

Podcasts

Podcast Episode 21 – Enough with the Religious Bigotry

Larry Wall Bigotry Quote

In Episode 21, I spend a little time on the “significance” of episode 21 before getting into the main topic. Our topic this time is about people who worship the language that they are using like a religion. Far too often, I’ve encountered the attitude from developers that people who aren’t using what they are using are luddites, heretics, and not worth their time. In my opinion, that has no place in today’s development landscape and I explain why.

Links Mentioned in this Show:

You can also subscribe to the podcast at any of these places:
iTunes Link RSS Feed

Thanks to all the people who listen, and a special thanks to those who have rated me. I really appreciate it.

The episodes have been archived. Click Here to see the archive page.

Personal

My First Guest Post – a Brief Bio of John W Mauchly

A little over two weeks ago, Matt Groves, my first podcast guest, asked for some volunteers to contribute to his excellent brief bio series.

I jumped at the chance to contribute. I find the history of our industry fascinating and while Matt had already covered one of my favorites, I was more than happy to grab the next name in the list and set out to work.

I read a lot of information and I learned a lot in the process. In the end, I had a lot of trouble even coming close to living up to the Brief label. My subject had done a lot to move computing forward. In the end, I was able to cut enough and still maintain a narrative that made sense and kept the importance of Mr. Mauchly. So, if you would, please go over to Matt’s blog and check out my guest post Brief Bio: John W. Mauchly.