That Code Corner Posts

Shcool
Fail Fast

Today I’m going to write about a principle I came across while watching some old Extra Credits videos. These guys are awesome, honestly. If you haven’t watched any of their things I highly recommend you go and check them out.

In the video they speak about a design principle called Fail Faster. Go watch the video. I’ll wait… You back? Okay so let me speak about the high level idea behind Fail Faster. It is basically to put something out there as fast as you can, even if it’s very raw and get some eyeballs on it. By doing this you will start picking up if and or where your design is flawed and figure out if you can correct course or if you need to restart (this doesn’t only apply to design, but can be applied to a lot of things in life).

Too frequently people hold onto an idea, a program or some code until they believe it to be perfect. Sometimes this is a a good approach, but most of the time it’s a really bad idea. The longer you work on an idea, the more time you invest the less likely you are too see it’s faults and the more it hurts if the idea fails. It’s a natural reaction. We all get attached to things we put a lot of effort into. So which would you rather do, work on an idea for months or even years to see it fail or figure out the issues with your idea at the start and work with both your eyes open? Seems silly to pick the first option when I put it that way, doesn’t it?

Then why is this so difficult to do? I mean it seems so logical. I believe it is rooted in who we go to for the advice. When an idea is raw, when it starts rattling in our heads most of us will think of going to people close to us, our family and friends or maybe trusted colleagues. However, these are also the people in our lives who we really really don’t want to come across as failures to and so we hesitate. Then there is another reason we don’t share, and it might seem a bit counter-intuitive but we don’t share because we are afraid that our idea will be a failure. Like I said it’s pretty counter-intuitive, but here is the catch, there are numerous studies which show that most people will delay confronting a fear (or perceived threat) in the present even if by avoiding the present fear the future fear is larger and more likely to occur. It’s just the way our brains are wired. So don’t beat yourself up if you find that you are afraid to share. It’s your brain trying to “protect” you.

So how did I get over my fear of failure? Well to be honest I haven’t gotten over my fear but as I’ve gotten older I worked out what is important to me and learnt where I can fail and where I can’t fail. I realised that failing at a project does not cause me to fail at what’s important to me. What I do consider a failure however is when I put a lot of time and effort into a project and then it fails because of things that could have been avoided. It also helps that I’m an optimist and I don’t hold idea sacred. I believe that ideas in their raw form are a dime a dozen and I also believe that for every idea I throw out I will be able to get two more, so I have started prioritising my time and not my ideas (which really are a dime a dozen).

This doesn’t mean that I am fearless however even when it comes to getting feedback for my ideas from people. The higher impact the idea could have on my life and the more I look up to the person I’m telling the idea to the more nervous I get. I have developed a few small cheats for this, sometimes I will try the idea out on a random forum, “What do you guys think of combining X and Y?”. It helps that I don’t see ideas as something sacred, you might not believe me but give it a shot, it’s amazing how many times I’ve gotten valuable advice from people speaking from experience. Sometimes it’s also just funny reading what the trolls have to say. However, I wouldn’t recommend it if you believe you have the next iPod on your hands. Another trick that I’ve picked up when I have a huge idea, is break the idea up into smaller segments, smaller cross sections of the entire idea and show different sections to different people. That way you can tailor each section and show it to people who can add the most value to those sections.

Online PvP Gaming
Online PvP Gaming

Practice, really practice. Practice at failing, don’t let it become a habit though. With each failure you learn valuable things. A lesson I’ve learnt from playing games, and especially on-line PvP style games is that failure is part of the process of getting better. It seems very silly to me now but I remember avoiding playing on-line games not because I didn’t think they were fun but because I was scared of losing to other people. Then one day I realised that if I want to be good at some games I will need to learn to deal with failure. The only way to get better is to practice, and the only way to practice is by playing and when you play you open yourself up to losing. It also helped that I started watching people stream games on-line and I watched famous professional players lose, wipe themselves off, get up and try again. Then I realised that they are no different from me. They only put extra hours into what they love. While I will never be a professional gamer, there are other things that I can put effort into, even if I end up failing a few times at the start. Keep in mind, with each failure you need too look at getting better. I can’t stress this point enough.

Fail Faster will make you better at whatever it is you do. Just keep in mind that you need to learn from your failures, don’t let them define who you are. Sometimes a failure can stay in your mind for a long time if you let it. It can grow and become bigger than it originally was. Don’t let your failures grow on you, don’t let failure become a habit, become stronger after each failure, smarter, faster. You are not your failures. Figure out what works for you and run with it.

-R

Sets
Image courtesy of wikipedia

I have interviewed a lot of developers, for various reasons my managers and team leads often ask me to help with the technical portion of interviews. So I have been exposed to a lot of developers from different backgrounds, one of my favourite interview questions are around the Java Collections Framework.

My purpose for asking questions about this framework is to see how seriously the developers take their Java. Anyone who has done more than just trivial Java development has worked with something from the collections framework. In fact I would be happy to say that if you haven’t seen or worked with the collections framework you haven’t developed any enterprise level code in Java. Collections is in my opinion one of the areas of Java where you have to be an expert.

Keep in mind when I speak about Java Collections my intentions are not to catch people out on small technical details, like Set is just an interface and you cannot create a new “Set” object. Some people enjoy those types of questions but honestly I find very little value in tricking people into trying this:


Set s = new Set();

One more thing, I know that Sets and List are not the only Collection types but I try to keep my blog post focused, so for now I am only going to speak about Sets and Lists. I might do a future blog on Maps (which don’t inherit from Collections) Queue and Deque (now that I think of it I might also one day do a blog on Polymorphism in Collections).

So here are the CORE things that I think you need to know about when it comes to Sets and Lists.

1) Sets:

Sets are awesome, really. Don’t believe me? Well let me explain. Sets store elements which is exactly what you would expect from a set, but they do so without any guarantees of the order of the elements (I am speaking about insertion order here). The also do not allow any duplicates. There are three types of Sets you will use.

1.1) HashSet

Perhaps the most basic of all sets the HashSet is awesome when you want to store some objects quickly and don’t care about the order, the HashSet never guarantees element order. Also HashSets are not thread safe, unless you are only reading.

1.2) TreeSet

A TreeSet stores it’s elements ordered in ascending order OR based on a comparator that you set up during initialization. There are a lot of instances where order is important, order here is used in the context of sort order that you set up in your comparator or in ascending order. Not to be confused with insertion order.

1.3) LinkedHashSet

Remember when I said that Set’s don’t care about insertion order? Well, technically that’s not true, a LinkedHashSet does actually maintain insertion order. So why not just use a ArrayList you might ask yourself. Well it’s because LinkedHashSets do not allow duplicates, and ArrayLists do.

2) Lists

Lists, differ from sets in two critical aspects, and it is also these two reasons that decide if you should use a Set or a List. Those two things are (a) Lists guarantee the (insertion) order of the elements, this means that the way you put the elements in is the way you will find them if you look for them. (b) Lists allow for duplicate elements.

2.1) ArrayList

This is the Collection I see most frequently in production code, it is very robust and can be used for almost anything. The only thing that gets to me is when people use this and then write separate methods to remove duplicates. Overall this Collection is an All Star when it comes to Java Collections and deserves a lot of respect. One thing to keep in mind, this guy is NOT thread safe.

2.2) Vector

When you get right down to it the biggest difference between Vector and ArrayList is the fact that Vector is thread safe. There are slight differences in the way they grow when new elements get added, but the general rule of thumb here is use ArrayList unless you really really need your ArrayList to be thread safe, then just use Vector.

2.3) LinkedList

This is probably the least understood collection, and it’s a pity because given the right conditions it can really shine. Basically you will always use the ArrayList unless, you don’t need to access your list in random locations (so if you are just constantly reading from the top LinkedList way better than ArrayList) AND if you need to do more insertion/deletion instead of reading a LinkedList is better because your LinkedList never needs to resizes. Which is something your ArrayList might need to do.

Conclusion

And there you have it, the big debate on when to use Lists and when to use Sets has been settled. If you have a group of elements that can contain duplicates then you should use a List, if insertion order is important the you use Lists (you would be surprised how often order is NOT important). If those two are not true then please use Sets.

-R

P.S. One of the reasons I love Sets is because they don’t allow duplicates so it is very cool to use them to make a list of elements unique and to identify which elements are duplicates. I might show how it’s done one day, but for now here is a hint: the add method for Sets returns a boolean.

 

Note: This article was originally posted on my old blog (http://thatcodeplace.blogspot.com). That blog will still be up but I will no longer be updating it.
Image courtesy of digitalart /FreeDigitalPhotos.net
Image courtesy of digitalart /FreeDigitalPhotos.net

I often wonder how certain people can be so good at what they do. When I look at sports stars or successful writers I often think to myself what separates those people from me? What i takes them to the next level? I believe that there are numerous factors that contribute to the success of those people, but I think one pattern that I have seen is easy to implement. Good habits.

When watching someone famous give an interview the number one piece of advice you will hear from them is “never give up”. That is true, but giving up or not giving up is a habit. You can develop a habit of completing things and seeing things through to the end, just like you can develop a habit of giving up and always taking the easy way out.

Spiral Effects of Habit
Habits create their own inertia, that’s why it’s difficult to give up smoking if you’ve been doing it for years. The good news is that the same applies for good habits. Want to get into the habit of doing something productive, like say creative writing? You should just start writing, don’t worry about being good or bad, just get into the habit of writing once a day for twenty minutes. Now the general rule of thumb on the internet is that if you do something for 21 to 30 days you will create a habit, I’ve found from personal experience that it sometimes takes a bit longer. Especially if it’s something you are not naturally good at. The trick is to keep at it, eventually the habit will create it’s own inertia and it will become easier and easier to stick to your writing.

However you should be careful because the same is true for negative habits, like the ones I’ve mentioned before, once you start taking the easy way out it becomes easier to do next time and before you know it you are coasting.

One day at a time
I remember reading something that has stuck with me for years, unfortunately I can’t find it again but the story goes something like this (I’m going to paraphrase liberally since I can’t find the original story):
“I was extremely overweight, I decided to do something about it. So I got a personal trainer and asked her to help me lose weight, things were going well but one day I was running up some stairs with my trainer and then it hit me. I am going to have to do this every day for the rest of my life. And suddenly my energy drained out of me and I stopped running, I was out of breath and told my trainer that I could not do this, there was no way I could do this for the rest of my life, the tears were coming to my eyes. My trainer looked at me and said, -Don’t worry about the rest of your life, that will take care of itself. Worry about the next step, and once you’ve taken that one worry about the one after that. Before you know it you will be done and you will feel great-

It’s always stuck with me, even though I can’t remember the source. Sometimes I get overwhelmed by the idea of starting something new. My stress levels go up and I start worrying about things that may or may not happen, I worry about failure, I worry about making a fool out of myself, what will people think of me?

But the above story is a great reminder that I should focus on the next step. If I want to learn a new habit I won’t do it by stressing about failure. You learn new habits by doing things in the present, not by thinking about things in the future. Take the next step, and then the one after that…

Find something that works for you
There are a few habits that everyone can benefit from, exercise more, eat healthy be a good person. The good thing about those habits is that they will allow you to build on them and create more good habits, it’s a lot easier to learn to write when your mind is fresh from some early morning exercise. In fact I believe that the above three habits will allow you to build a solid foundation for building more good habits on them. They are essential in my very humble opinion 🙂

One thing that a lot of people get wrong is they believe that they can create a habit using sheer willpower. I’m sorry to tell you this, but you probably can’t. Willpower belongs to the pre-frontal cortex, and this piece of your brain is the one that burns the most energy and it the most susceptible to fatigue. And guess what, if you are relying purely on willpower you will get tired one day. Then that chocolate that you’ve been successfully ignoring for the past two weeks will be consumed.

So find a system that can remind you, or help you when your willpower drops. I found using HabitRPG works for me. It combines things I love the most getting better at things, a list like structure that allows me to tick things off and levelling up!

Find your system, hack at it and make it work for you.

Conclusion:
Self improvement is something that I am really passionate about, I expect that I will write about this and similar topics again in the future. The point I was trying to get across is that to become really good at something you need to Cultivate Good Habits and Prune Bad Habits. If you do this even for just a month you will look back at your month and stare in amazement at the big difference in your life. And the lives of the loved ones around you.

One final word of warning, don’t try to break all your bad habits all at once. Rather attack them one at a time as you defeat one bad habit the others start losing their grip on you. Divide and conquer.

-R