Translate

5 Object Oriented Programming Principles learned during the last 15 years

February 1st, 2012

It was about 15 years ago when I first created my first class.

I went from thinking of Object Oriented programming as this awesome paradigm I should know about, I remember having all these mental models of what objects where and learning all this new vocabulary that I really didn’t fully grasp until years later when I was designing and implementing bigger more complex systems.

At first I remember looking at objects as a collection of functions that kept a common reference to keep internal states and missing out on all the great features of inheritance and abstract classes. Looking at “interfaces” and using them like a recipe without really knowing their true power, I look at the past and I was so limited, hopefully I’ll look at this post 15 years from now and think the same.

There’s lots of books out there on best programming practices you should follow, on this short post I’ll share with you 6 principles that I follow that I promise you will make your code behave really well, less bug prone, simple and elegant when it comes to Object Oriented design. This advice will probably apply better if you code in a language like Java or C#, I’m not sure if you can say these principles will apply 100% to other OO languages (for instance those that allow multiple inheritance)

1. DRY principle.
This is one of the most preached ones, and I will also preach it. DO NOT REPEAT YOURSELF. Really, don’t. It ALWAYS comes to bite you in the ass if you have repeated code. Only repeat yourself if it’s too hard to not do so or if you’re absolutely sure that you won’t repeat yourself a third time, but I promise you that third time will come if it’s not for you for the next guy maintaining that code.

The obvious benefit of not repeating yourself is that you get to maintain code only in one place, the added benefit will be that your code will almost start being written by itself like a perfect equation as it grows. DRY code will be reusable, maintainable and the other principles I’ll talk about are related to the DRY principle in one way or another.

2. Keep the scope at the minimum, be as private as possible.

Keep your variables as close to your local scope as possible. This will save you many headaches, will protect you from bugs in logic because you did something to a variable in place where it shouldn’t have been in the first place, and it will keep things simpler.

This also tells you that your classes should expose as little as possible. Keep your variables local, if they can’t be local, try to keep them as private members, if they have to be used by extending classes keep them private, only use public when you really know it’s ok to make something public and that nobody is going to break the behavior of your object if they play with things at any point in time.

Keeping scope at a minimum can also prevent issues on longer lived objects like singletons that might be accessed by different objects. If you abuse the use of object properties you could have one object changing the internal state of the object while another tries to do something and you’ll get unexpected behavior, this tends to happen a lot in multithreaded environments where programmers are not careful and forget objects might be accessed by different threads/clients at the same time, tight scopes will protect you.

3. Be functional
Sometimes you’ll be tempted to be lazy and not pass parameters on a method and think that you’re so clever by changing an internal property of an object so that another function in the object will then get it. Big mistake buddy. This can be equivalent to that advice from functional programming languages where you’re told that using global variables are a bad idea.
Yes, we have object properties for several reasons, but if the logic of a method depends on the state of a variable, you might as well be explicit and pass it in the method.

This is in a way related to keeping scope at the minimum, and to some extend related to the advice some give of keeping your objects as immutable as possible.

4. Only abstract classes are worth extending.
If you have control over the class you are about to extend (if it’s in your codebase) before you extend that class take a look and make sure that it’s an abstract class. Abstract classes are MEANT to be extended, so you can be safe that you’re doing the right thing here.

If you’re extending a non abstract class, you should probably be composing it instead. If you don’t have access to the code of the class you are extending, things could not behave as expected when you extend, not all programmers are as thoughtful as you are.

5. Keep Object lifetime as short as possible.
This goes back to keeping scopes tight and trying to avoid singletons as much as possible (as convenient as they might be), in the case of java the JRE has a better time collecting garbage and will save you from memory leak headaches. Put those factories to work.

Feel free to disent and share your favorite principles, If you’ve coded long enough I’m sure you tend to think about these things too and I’d love to learn from your experiences.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Film makers, the solution to piracy is very easy, stop crying and get with the program

January 25th, 2012

I just watched a lot of film makers crying about how their movies are being “stolen” every day and seeing rogue websites monetizing on their movies.

They keep talking about how they made DVD deals that make no sense no more because people are watching their movies for free the day after the release online on pirate websites. Why on earth are you putting the financial fate of your indie film solely on a format that’s dying? hint, the people that are pirating your content aren’t doing so on DVD, they’re getting it through the internet remember?

Then you go on to say the solution to your problem without paying attention to your own words “the pirate websites are making money on advertising and we can’t do anything about it!”

Are you listening to yourselves?

The solution is doing exactly what the pirate websites are doing Make your content free and monetize it on ads YOURSELF, use your film to advertise itself and merchandise around it GLOBALLY.

Here you have the cheapest most effective content distribution tools available in history at virtually no cost and you’re not using them. Sharing your film with a Creative Commons license gives others a clear signal to share the content for you, you don’t even have to be there to give people permission when you do it in advance.

When your movie is done, give it for free on YOUR website, on BitTorrent, on youtube, put ads on it and around it, and place links for people to buy your DVD, Blu-ray, T-Shirt, Special Edition Sets, Action Figures. etc.

Make your content available through Creative Commons licenses, allow for it to be remixed, let your cultural impact reach others in ways you didn’t imagine, a Creative Commons license forces the sharer or remixer to give you attribution and incredible things can happen to your film when it reaches audiences you never intended in ways that you never intended.

Sharing your content is FREE MARKETING that can open so many business opportunities for you. And all this time you’re stuck on selling tickets and DVDs, the most expensive 2 ways to distribute your films, the worst distribution channels controlled by box office and retail mafias that will take a big cut of the sales, when all this time you could be saving so much on marketing and distribution worldwide. It’s almost unbelievable that you’re still missing out on these opportunities and saying the film industry will die when it now has a worldwide audience instantly.

Put your movie on YouTube as well and apply for monetization, you’ll get millions of viewers WORLDWIDE there too and get a great CPM rate on video advertising.

If you make it hard for people to see your movie while others are making it easy you are pushing business away, no matter what technology or law they pass, if someone really wants to see your movie and it’s not easy for them to access legally (not in theaters, not in DVD, or they just can’t afford to pay you) they are going to get it so it might as well be you spreading the content, tracking all the views, and monetizing all those free views yourself.

If it works out for the pirates, it will work 10 times better for you because you are the source of the content, you will be ranked first on search engines, and you will sell copies and merchandise, guaranteed.

You’re the only people complaining about having too many customers. Stop thinking of a scarcity based business model, you have a product that can be copied endlessly, why not make use of that amazing feature?

Update: I just want to say that I’m super happy that all this SOPA/PIPA happened, it has raised awareness not only on problems but it’s made people look at those of us who have been working for years on solutions for content creators around free based models. In this post I’ve talked about film, but all my talk doesn’t come out of thin air, it comes from the experience we’ve had working day and night with musicians, here’s more if you are interested on what we achieved in 2011 with Creative Commons, BitTorrent and open minded content creators (don’t miss the statements of singer Kellee Maize, who has come time after time back to the bittorrent community)

Update 2: I also want to clarify, that I’m not saying “drop selling and go free 100%”, I’m saying do both. Giving content for free on the internet will not affect your sales at all, if you remember it’s already being copied for free, it’s just not being done under your supervision. The people that pays to see your movie on DVD or on the movie theater are used to that experience of those form factors and they will keep doing it because they’re used to it.

Have your content easily accesible online, have both free and paid versions of it online, convenience and availability is the best antidote for piracy. Once the content is free there’s no motivation for the pirates to do anything, it’s already free and you can even use the opportunity to grow a much larger community of fans around your content, even if they don’t pay for it, they’ll love you and spread the word about it.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Breakfast extra Protein, Vitamin and other supplements

January 22nd, 2012

From left to right:

Ginkgo Biloba (just in case it actually works)

Calcium + D3 600mg.

Centrum multivitamin

Glucosamine HCI with MSM 1500mg x 2 (For cartilage and collagen development, no more pain in my knees, or ankles when running, stuff is awesome)

Whey Protein (24 grams of proteins, 1 gram of fat, 3 grams of carbs)

H20 300ml.

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License by Gubatron http://gubatron.com

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

#IWasThinking S01E04 Making our own “Secret” Society

January 10th, 2012

Thanks for watching! Unlike Uslar Pietri’s “Invisible Friends” on YouTube you don’t have to be invisible! Send a video response or leave a comment below.

On this episode
Wish I was part of a secret society, a great unconditional support network.

Let’s form our own, only smart folks looking to change the world for the better should be in. Thoughts about free energy from the earth’s magnetic field (what would’ve Tesla done if he had wikipedia). Thanks to the 60 to 90 people who watch, I’m not talking to an empty room, yay.

Come to think about it, some religions are just that (powerful brotherhoods in the open), they just have a mythical pretext to come together.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Photography/Photoshop homage to music video “First of the year” #skrillex

January 5th, 2012

First of the year
Miami, FL
December 2011

Here’s my homage to Skrillex’s “First of the Year” music video, one I think will be remembered forever as a cult music video for Dubstep as it started crossing the mainstream frontier.

The model is my daughter who started joking with her fingers right after she saw the music video, which rang a bell in my head, and here we are.

In case you have not seen the music video yet, I’ll save you the Googling
First Of The Year (Equinox) – Skrillex [OFFICIAL]

This picture is Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Javascript: Capitalize Text Like In This Title

January 5th, 2012

On this one, we’ll show of the dynamic nature of javascript and we’re going to make all strings have a new method called “capitalize()”.

So you can do stuff like this:

console.log("this text should look nicer now".capitalize());
>> This Text Should Look Nicer Now

Here’s the magic code you’ll need to add somewhere on your javascript files and all strings will have a new capitalize() method.

String.prototype.capitalize = function(){
	   return this.replace( /(^|\s)([a-z])/g , function(m,p1,p2){ return p1+p2.toUpperCase(); } );
};
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

javascript: Format a number to display thousands US style (#,###,###,###)

January 5th, 2012
/**
 * Format a number to display thousands like in the US -> 1000000 => 1,000,000
 * @param number
 * @returns
 */
function formatThousands(number) {
	return Math.max(0, number).toFixed(0).replace(/(?=(?:\d{3})+$)(?!^)/g, ',');
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

javascript: Get N random elements from a List

January 5th, 2012
/**
 * Walks linearly through the list to find an element.
 * returns true if it's found.
 */
function elementIn(collection, element) {
	for (var i=0; i < collection.length; i++) {
		if (collection[i]==element) {
			return true;
		}
	}
	return false;
}

/**
 * Returns a new list of n random elements taken out of myList.
 */
function getNElementsAtRandom(myList, n) {
	var toGo = n;
	var result = [];
	var indexesUsed = [];

	while (toGo > 0) {
		index=-1;

		do {
			index = Math.floor(Math.random()*(myList.length));
			console.log(index);
		} while (elementIn(indexesUsed, index));

		indexesUsed.push(index);
		result.push(myList[index]);
		toGo--;
	}

	return result;
}
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Why philosophy is important (the movie)

December 31st, 2011

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

#IWasThinking S01E03: Why philosophy and questioning our reality is important

December 31st, 2011

We should also be looking for the next leap in philosophy trends, we need new forms of interpreting reality to have a leap in technological progress. (just the way the introduction of “science” (scientific method, testability) propelled us to the world in which we are now)

Learning and living philosophy is REALLY important. Just as we evolve in every other knowledge field, we must evolve as philosophers. When some philosophers decided that reality had to be questioned (tested) we came up with the scientific method, which brought about more innovation in the last 200 years than in thousands before it. Just because we started questioning our reality differently than before (like shit was made by god and stuff, we learned stars where massive balls of hydrogen and helium collapsing into heavier elements which make up most of what you and I are made of.

What will the next breakthrough in philosophy bring to the aid of our creativity?

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004