In this post, I explore the prudence of unification and conformity in code.
I subscribe to this philosophy: "There should only be one way to do anything, in code."
There's a few reasons for this. The first is that duplicate code is bad, mmkay? Having two ways of accomplishing the same thing is equivalent to duplicate code. They may not look the same. They may not act the same. But they perform the same task.
Building a framework is all about building the tools to quickly and efficiently create an application. You want to use the right tools for the job. Anyone who has tried to drop a transmission without a transmission jack can agree with me on this. If you use a standard jack, it is possible, but it is unstable, unwieldy, and takes a lot more work (more lines of code) than is necessary.
When pounding a post into the ground, you should not want to use a giant hammer. You can miss the post, you can hit yourself with the hammer, and it takes a lot of swings to hammer a post into the ground. You want a post pounder, which is a giant heavy metal sleeve that pounds posts into the ground.
That is why, with a great framework, there is only one way to do anything. Assuming your staying within the confines of a namespace, there is no hammer. There is no universal tool. There is a specialized tool for each thing you want to do. Want to pound in a nail? Here is a nail gun. Want to pound in a post? Here's a post pounder. You could use a hammer for each of those tasks, but a hammer, while suited well to the task, is not crafted specially for any particular thing.
Hammers are for people who don't exactly know what they will be doing.
To continue, want to bend a piece of metal? No. That is not a valid operation within the framework, because you aren't being specific enough with your requirements. You could use plyers, but we took away the plyers, because they are too general purpose. We have the Daimler (very large semi-truck building company) tool shed at our disposal, I am sure you can either find a tool that does the job exactly, or cast one for this very specific purpose.
An alarm that should be going off any time something seems frustrating in code is: Is this the right tool for the job? We should not have to fenaggle and coerce code to work, because our tools don't take up any space and are instantly available to us. One tool, one job, that's it. If it doesn't exist, build it and add it to the framework.
Wednesday, August 13, 2008
Wednesday, August 6, 2008
Loose coupling. Friend or foe?
Disclaimer: This is C#. I also have coded in java, but it was in a framework that was so well built that it was boring.
I recently ran into a situation where loose coupling is touted as a virtue of some code, but I am not personally seeing it.
I see it as a bit of a trade off. Quite a bit.
I measure the effectiveness of many patterns and design ideas in the overall effects they have on the code. This includes things like obscurity of the pattern, difficulty to learn/intuit the pattern, time it takes to refactor the pattern, etc. This can all be added up into a theoretical value called potential bugs.
Loose coupling is indeed nice when you are taking input from the rest of the world. I have currently run into a situation though where we use loose coupling to determine what services we are attaching ourselves to within the program.
You've probably seen this before. An application config with some fully qualified class names in it (good luck renaming those classes/namespaces without ripple effects), ConfigurationElementCollection, searching assemblies for classes, the good ol' Activator.CreateInstance.
This pattern is good when being used in a fashion that would benefit loose coupling, like a plugin, where you don't know what you are signing up for. But we, our team, controls both sides of the equation.
Argument against: The compiler is your friend. Refactoring tools are your friend. The easier (and faster) your code can be manipulated as a whole, the less bugs your going to going to see, and your code will become a lot more simple. When you control both sides of an equation, leave it out. This is unless you are using dynamic binding or something where loose coupling saves you complexity and numbers of lines of real code.
(Real code is french for code that can't be code generated, or isn't absurdly simple.)
Conclusion: Avoid Loose coupling, because it sabotages a lot of the tools at your disposal. Use when interacting with unknown parties, or dynamic binding to something. (Web services, DLLs, plugins, runtime code generation, etc)
Reasons:
The most boring code is the easiest to understand. (Readability)
Don't architect for something that isn't going to exist in the immediate future. (Simplicity)
Tools are your friends. Don't sabotage them by creating disjoint code if it is unnecessary. (Continuity)
The easier and faster you can refactor your code, the less bugs you are going to see. (Flexibility)
I recently ran into a situation where loose coupling is touted as a virtue of some code, but I am not personally seeing it.
I see it as a bit of a trade off. Quite a bit.
I measure the effectiveness of many patterns and design ideas in the overall effects they have on the code. This includes things like obscurity of the pattern, difficulty to learn/intuit the pattern, time it takes to refactor the pattern, etc. This can all be added up into a theoretical value called potential bugs.
Loose coupling is indeed nice when you are taking input from the rest of the world. I have currently run into a situation though where we use loose coupling to determine what services we are attaching ourselves to within the program.
You've probably seen this before. An application config with some fully qualified class names in it (good luck renaming those classes/namespaces without ripple effects), ConfigurationElementCollection, searching assemblies for classes, the good ol' Activator.CreateInstance.
This pattern is good when being used in a fashion that would benefit loose coupling, like a plugin, where you don't know what you are signing up for. But we, our team, controls both sides of the equation.
Argument against: The compiler is your friend. Refactoring tools are your friend. The easier (and faster) your code can be manipulated as a whole, the less bugs your going to going to see, and your code will become a lot more simple. When you control both sides of an equation, leave it out. This is unless you are using dynamic binding or something where loose coupling saves you complexity and numbers of lines of real code.
(Real code is french for code that can't be code generated, or isn't absurdly simple.)
Conclusion: Avoid Loose coupling, because it sabotages a lot of the tools at your disposal. Use when interacting with unknown parties, or dynamic binding to something. (Web services, DLLs, plugins, runtime code generation, etc)
Reasons:
The most boring code is the easiest to understand. (Readability)
Don't architect for something that isn't going to exist in the immediate future. (Simplicity)
Tools are your friends. Don't sabotage them by creating disjoint code if it is unnecessary. (Continuity)
The easier and faster you can refactor your code, the less bugs you are going to see. (Flexibility)
What is this?
This, more or less, is a place to record and discuss software engineering 'the correct way'. The majority of this is going to be me pursuing the correct way to code, but may contain thoughts on other aspects of a software engineer's life.
As always, this is basically from a hacker's perspective, and is meant to be informative with no guarantee of prudence, high mindedness, politeness, or anything of the like.
As always, this is basically from a hacker's perspective, and is meant to be informative with no guarantee of prudence, high mindedness, politeness, or anything of the like.
Subscribe to:
Posts (Atom)