60 points · by javinpaul · 13y ago · 57 comments
"if you use a hardcoded value more than one time consider making it public final constant"
This should obviously be: "if you use a hardcoded value, make it a public final constant"And:
"if you have block of code in more than two place consider making it a separate method."
This should be: "if you have a block of code, consider making it a separate method"Also, half of these principles imply the other ones.
edit: This is meant seriously btw, if you're giving advice to novice programmers, the instructions should be clear. If you say 'consider making a separate method' what should the programmer consider? If the programmer is new to this he or she won't know what considerations to make either.
This goes for all principles by the way, they're all fine and dandy but they're also trade offs. Every time you abstract something, you risk obfuscating the code for the uninitiated.
As arethuza suggests, Keep It Simple Stupid is also a solid design principle, and it contradicts a whole lot of the other principles.
Adding e.g. a symbolic constant is a bet. You win if during the lifetime of the system, you discover that the constant must be changed, and the gain is the time saved by changing it quickly and with few errors. If you never have to change the constant, you lose the time spent on introducing the constant and the time spent on looking up the constant while reading the code.
Most of the rules here are long change (or whatever the financial people call betting on something), except for KISS.
I don't think these are rules of thumb that you should take for granted without criticism, especially when paraphrased from their original (well researched) source.
And I especially don't think you should take from them what you like. They are principles you should know because they are known to lead to well designed software, you should leave none of them alone.
The person you responded to mentioned novice programmers. Teaching novice programmers with the attitude of "use as much of this as you like/want/understand" is a pretty poor technique that will not give good results.
If you separate the block out into their own methods, they reader will have a much easier time verifying that they are decoupled.
I believe that inclusion solves the issue you've described. Obviously you don't need every 5 lines of code encapsulated into a method, but preventing code duplication should be paramount.
A lot of people seem to thing that code duplication is the only reason to refactor code out into it's own function. While that's certainly a good reason, there are many others. For me, being able to test the code easily is a major one. The smaller the function (specifically the less branching and the less it does), the easier it is to test.
Obviously, it's a balancing act.
The worst effect of this kind of doing things is the image of programming that it shows to beginners. Programming well has nothing to do with that, really.
I mean, of course you shouldn't try and deliberately flaunt any good guidelines, but the measure of your code is ultimately not these.
The guidelines set out here allow you to deliver that value without having to rip up your product for every minor change.
However to turn things a bit more constructive, let me know what languages you see as less of a mess if you have the time. It doesn't matter whether we agree and I'm not looking for a fight, just your opinion and reasoning. I might well agree.
Or at least avoiding "general-purpose tool-building factory factory" factories:
http://discuss.joelonsoftware.com/default.asp?joel.3.219431....
There's low harm in duplication of code. When something changes you don't have to refactor your class structures. You just change the code for the thing it affects. And if you have to make multiple edits it's often pretty obvious what you need to do.
(Data and configuration are different, it's more important for these to have a canonical home.)
"Classes, methods or functions should be Open for extension (new functionality) and Closed for modification. This is another beautiful object oriented design principle which prevents some-one from changing already tried and tested code."
No, no, no. Let someone modify whatever the hell they want. If they screw it up, that's on them. Quit it with this childish belief that you have to control people.
My girlfriend came up with a nice metaphor trying to follow along. The open/closed principle applies to natural language as an interface. It is important language can be extended to convey new meaning, but also important that it is closed for modification so we retain the ability to understand eachother.
If changing a common component or library breaks my code, they may be at fault, but it's still my feature that breaks. The open-closed principle is about sandboxing modifications to the scenarios that need those modifications. It's not about babysitting.
Without it, when you make a change you need to consider every consumer or risk breaking their case. Or, you get these nightmarish components that have "modes" (if I'm included in this context, act like this, if I'm included in that context, act differently).
Also see: http://stackoverflow.com/a/90915/343699
It's trickier to use, because blindly applying it can lead to immense classes, but I find it a useful thing to keep in mind.
class Foo < ActiveRecord::Base
belongs_to :greeter
delegate :hello, :to => :greeter
endThe book "Effective Java" (from 2001) still has to one the "must read" for any Java programmer that didn't read it.
In it Joshua Bloch has always been very clear, from the start, about all the deeply ingrained ugly Java warts that Java had from the start (and still has). Like for example the complete and total utter impossibility to respect the equals() and hashcode() contracts in the face of inheritance.
Took a long time to set in but now quite some Java programmers are beginning to accept that having equals() and hashcode() at the top of an inheritable OO hierarchy may not have been the greatest thing since sliced bread.
The second book to read is Goets and al.'s "Java Concurrency in Practice" (one of the best looking programming book cover ever, which never hurts an incredibly high-quality content).
But the real lesson is that: Java is too complicated and has way too many warts to ever produce beautiful code. The JVM is not all bad but "Java the language" is really fugly.
Switch to a language that has a saner way to deal with concurrency (Say Clojure's STM or Go) and you can throw most of the pages in these books to the trash.
But is that a problem with Java or is it a problem with ANY OO language?