Tuesday, 5 August 2014

Learn A Pattern A Week With Pie - Builder Pattern

Welcome back to our series on design patterns.

This week our focus will be on the Builder pattern, a creational pattern which provides ways to construct a complex object one step at a time.

So let's say you've a class called Student with a constructor which accepts 3 parameters; firstname, lastname and department. Obviously, creating an object of our hypothetical Student class is a no brainer. Just pull out your editor and fondle some few keys to get it done as shown below:

Student s = new Student("Edward","Pie","Computer Science");

The above snippet is easy enough to be memorised by a day old chick, isn't it?

What if you'd a constructor which accepts 10,15,20, or more parameters? Would you still go by the strategy above? Yes or No? Be honest!

Wait babe, don't flush in 10,15,20 or more arguments into your constructor. That's gluttony and you know that's unprofessional. And now you're wondering, "what else can I do?" Uuhhhhm! Ok lemme create a parameterless constructor and throw in a bunch of setter methods to set properties on the object.
Really? Mr. holy sinner, there's a better way (ask your parents).

Aaaah! I caught you there, now you're scratching your head and you're saying "what the heck am I to do?" Tadaaaaah! The Builder Pattern says, "hey! I am every heck you've to do"

The Builder Pattern

This is a pattern for constructing complex objects in a step-by-step approach using simple objects. In this pattern, a Builder class which is independent of other objects builds the final object in succession of steps.
This pattern falls under the creational pattern in the GoF's grouping of design patterns.

Ti's Time For Hands-On

Let's have a practice lab session and try out one of the simple ways of implementing the builder pattern.
We'll continue working with our hypothetical Student class, this time it accepts 6 parameters instead of 3. Six parameters aren't too much to be supplied as arguments to a constructor but we're sticking to 6 parameters just so it'll be easier to understand. You'll be able implement a Builder for a much complex object after understanding this.

The 6 parameters for our hypothetical Student class are firstname,lastname,department,cwa,level, and hallOfResidence.

See a simple implementation of the above scenario below:

I admit you've to add extra code to implement the builder pattern for such a simple scenario but the focus is on being able to construct complex objects in a step-by-step approach. With the builder pattern, you're able to chain a series of setter methods calls with each call taking the object under construction a step further.

Consider a typical real world situation where you take your car to the car repairs shop, the fixing of your car's problems may start with the welder welding broken pieces together, the auto-electrician fixing electrical problems, the mechanic fixing your engine and finally the sprayer giving your car a new look. You'll agree with me that the movement of your car from one expert to the other takes it closer to your final goal, fixing your car. This is how the builder pattern works. It helps you to construct complex objects in a step-by-step manner.

There are other ways of implementing a builder but for the sake of understanding I chose a very simple approach.

Your comments, suggestions, questions and criticisms are welcomed here or on Facebook.

Thanks for learning with me. See you in the next post!

Remember to have fun!!!

Sunday, 3 August 2014

My Highly Opinionated Thoughts On Learning Programming

This is gonna be a rather long and highly opinionated post on mastering programming so I advice you grab a cup of coffee and sip slowly as you read with an open mind. Like I said, the points aren't laboratory certified ways of mastering programming 1, because I'm not a master programmer yet (at least per the people I keep as mentors) and 2, because what worked for me may not work for you. Pass what you read here through your own filters, assimilate what resonates with your style and shove the remaining shit out of your brain.

Choosing A Programming Language

I'll urge you to love to be a polyglot (be able to program in multiple programming languages). Choose at least 1 mainstream programming language eg. Java or C# .NET or PHP and some other non-mainstream programming language eg Python, Ruby, R etc. Your location determines what's mainstream and non-mainstream. In Ghana, Java & C# are mainstream. Also, master different languages for the different paradigms especially OOP and functional programming. If the only tool you've is a hammer, every problem appears to you as a nail. It easier to find a not-so-well-paying job with your skills in the mainstream. Conversely, it's difficult to find a job with your skills in the non-mainstream but remember to say thanks to Pie when you hit that high paying job which needs your skills badly.

Master Syntax & Semantics

The best way to learn any programming language is to master the syntax and semantics of the language. Don't sit all night cramming crap you don't understand into your head. Until you learn to understand, you can't really build good stuff with your language of choice. Don't rush it. You aren't in any race. Slow it down, learn it well, understand it and use it well. Learn to share your knowledge freely once acquired. Don't fool yourself by thinking it's an advantage if only you can do it. You know why? Simply, you don't know shit. People know more than you do yet they are humble and love to share. The more you teach, the more you understand.

Learn & Apply Design Patterns

You don't know of design patterns? Meeehn, you're so not current. A simple Google search will land you on some awesome sites where you can learn and apply design patterns. It will help you avoid writing spaghetti codes and also shun voodoo programming. Don't spend hours wondering how to write some piece of code. Some older dude faced similar issues, fixed and documented the procedure just to make life easier for your lazy ass. Sorry, you ain't lazy, you wouldn't have read this far. lol!

Read & Write Lots of Codes

Until you read code written by others you may think you're the best programmer ever created. Code writing is easy, code reading isn't. Every developer thinks the other guy who wrote the code he's been called to maintain is an idiot. Why? Simply because you've bad code reading habit or maybe the guy was indeed an idiot, a typical text book example of an idiot. The pun and jokes not withstanding, take these points serious. You learnt how to write code, you've to learn how to read code as well. Join some open source projects, get insulted and intimidated by the experienced dudes. They won't forgive you for posting stupid code, you'll get hurt but you'll dare not repeat yourself.

Invest In Your Skillset & Toolset

Make learning a life-long process. Buy books if you've the means, watch video tutorials. Never enter tomorrow being the same person you are today. Wake up every morning knowing you're a better person. Buy good computers. Use your snail-paced PC for a doormat. Save and buy some fast computer, at least fast enough to launch your IDE before you blink 10 times. Know what you don't know and never be satisfied with what you know. If you don't know, and you don't know that you don't know then you're a fool.

Have A Mentor, Be A Mentor, Change Mentors & Don't Be Changed As A Mentor

Have A Mentor
Whether you like it or not, you aren't the best programmer. Every other programmer, even the greenhorns know something about programming that you don't know. That you've programmed for 20 years doesn't make you the best programmer around. Stop crowning yourself in your utopian world. Shove your pride up your ass, step down, get someone who's better than you, humble yourself and learn from this guy.  Choose a mentor and learn from him.
Be A Mentor
The best way to learn is to teach. You'll hardly forget what you've taught over and over. Don't be stingy with your knowledge. The truth is, you don't know jack. Others know more than you do yet they freely share. If someone sat on this knowledge you wouldn't get to know of it. Count the number of books around you, the number of videos freely available, the write-ups, the blog post etc. How greedy and stingy can you be. Can you lock up the whole internet into your pocket? You see, there's no sense in holding onto some knowledge as though it's your personal property. Share! Share!! Share!!!
Change Mentors
Once you're able to communicate with your mentors at par, you are colleagues. At this point, a wiser thing to do is to choose you mentor's mentor as your new mentor or choose someone better than your mentor as a mentor. Whether you like it or yes, there's always someone up the the ladder. Hating is diabolic and unhealthy. Humility and curiosity is key to the acquisition of knowledge. Acknowledge the fact that your aren't the best and you won't be the best but you can be among the best. Change your mentors frequently. It's a measure of how fast you're growing.
Don't Be Changed As A Mentor
If your mentee  chooses another mentor it means he's got to your level and you aren't growing. Keep growing your knowledge. Learn as though you're gonna live forever on earth. Find great joy in increasing your technical know-how on a day-to-day basis.

Don't Be Complacent

In the land of blind men, the one-eyed man is the King. In Ghana, anything above a freaking HelloWorld is worthy of an award. That there are few techies around doesn't mean everything should be over hyped. A full grown hairy man creates a damn app which notifies you when you get a letter at the post office and it get featured in our contentless media. You build static websites and get carried on shoulders by incompetent people holding highly influential positions. Hey, village programmer, Ghana or African isn't the only place on earth. There are better programmers elsewhere who are worth looking up to. Truly, there are equally good local programmers who are worth mimicking. Have big dreams. It's not about an app which tells you how much damn calories you've burnt. Who cares if your burnt the hell out of yourself. It's only about solving the very simple problems in society. Guys, stop acting as though you're at silicon valley. Silicon valley ain't no joke. Here's silicon savannah, embrace it. Know where you are, know where you wanna go to and you'll know how to get there. Kill your pride because your pathetic degree isn't even a match for someone's primary education. You were taught Computer Science by some lecturers who haven't even written code to parse a file. Your final year project was a useless blah blah blah management system which is sitting in dust by now.
I'm telling you the hard truth you've never heard and you're probably thinking "who the hell does this Pie dude think he is". Sorry man, I'm an honest and proud African who wants Africans to be known for the good things we do and not the copy-cat wanna-bees we've been for decades.

Have big dreams, dream about working for intelligent tech companies across the world. Work with some of the smartest people around. Learn from them! It's not only about some half-assed software. It's not about engaging in mental masturbations. Thinking you're the best. Stop painting Africa black with your mediocre bullshit inventions.

I don't think of my self as the best programmer, I only aim to be among the best in the world and that's what I'm constantly working on becoming.

Get real!!!