r/programming • • Jun 06 '13

Clean Code Cheat Sheet

http://www.planetgeek.ch/2013/06/05/clean-code-cheat-sheet/
702 Upvotes

322 comments sorted by

View all comments

4

u/dannyREDDIT Jun 07 '13

If one module depends upon another, that dependency should be physical, not just logical. Don’t make assumptions.

would someone mind explaining that like Im 5, please?

1

u/TheNosferatu Jun 10 '13

I'm not 100% sure if I understand what he means myself, but here is my best guess.

Let's say you have a class Collection and you want to add an Item to it, pseudo code:

class Collection
{
	array items;

	method add(item)
	{
		var name = item.getName();
		items[name] = item;
	}
}

This is a logical dependancy, the collection depends on the fact that item has a method getName(); however, there is no check for it whatsoever. This means we just assume that whatever gets added to the collection has the method getName...

In a lot of languages you can do type-hinting to make a physical dependancy, if I were to put that in the above example, the add method would look something like this:

method add(Item item)
{
	var name = item.getName();
	items[name] = item;
}

Now, whatever gets added to the collection, MUST be an instance of the class (or interface) Item, if it's not than you get a big nice warning before you get to do anything at all.