Widget HTML Atas

You Bring To Know : What Is Oop Skilful For?

An interesting artcle tech/articles/0308/three_big_lies.php">here got me thinking well-nigh around blueprint as well as the evolution process. First of all, I concur completely amongst their 3 lies, especially the mo as well as 3rd one. To riff on it a petty bit:

Goals as well as Non-Goals

Code serves 2 purposes:
  1. It has to what you lot desire (and this unremarkably means: the features you lot want, the performance you lot want, no bugs).
  2. It has to non drive you lot insane.
This mo shout for is of telephone substitution importance: code blueprint paradigms involve to reckon maintainability. Code ever alive longer than you lot recall it volition - you're going to hold out stuck amongst your work, thence ready your code to hold out usable. I would nation you lot can't accomplish (1) or at to the lowest degree you lot can't accomplish it cost-effectively, without (2).

However, what's conspicuous past times absence is: the role of code is not to determine your information model! And inwards this regard OOP has done us a flake of a disservice inwards how nosotros recall well-nigh design.

OOP Myths

The job amongst an OOP blueprint is that it confuses code construction amongst information construction past times providing a laid of idioms as well as linguistic communication constructs that bind the 2 together inwards really specific ways.

When I was inwards school, OOP was mis-advertised equally providing iii actually nifty things:
  1. Encapsulation (that is, keeping the guts of your code from leaking all over the place).
  2. Polymorphism (that is, giving you lot linguistic communication constructs to define multiple implementations of abstract behaviors).
  3. Code reuse (via inheritance, that is, you lot should hold out able to sub-class to utilize existing code).
Turns out that's non actually true. Item 1 (encapsulation) is where 99.9% of the value of OOP comes from. The divulge i job amongst large one-time code bases is that everything fondles everything else, as well as it's impossible to operate inwards an environs similar that. To the extent that OOP encapsulation encourages programmers non to write crappy code, that's a practiced thing, although OOP doesn't hvae a monopoly on this.

Polymorphism is a neat trick, but but non that important. I recall nosotros pick out all of i polymorphic shape inwards all of X-Plane (plus i to a greater extent than inwards the installer). That's inwards but about one-half a i K k lines of code. Again, OOP doesn't pick out the monopoly - nosotros larn most of our dynamic demeanour from component behaviors, non virtual component tables.

And code reuse via inheritance is thence far off base of operations that I'd telephone phone it a damned lie. Inheriting implementation unremarkably results inwards chaos as well as is a bad idea, thence I never believed this one.

But the watch that code blueprint as well as information blueprint serve dissimilar masters brings abode just why this is a misfortunate idea. Basically you lot are creating a really item human relationship amid your information for the role of organizing your code! Bad idea! Data blueprint should hold out driven past times programme needs, non code scheme needs.

Design Method

When I operate on X-Plane features, I recall inwards damage of a information construction as well as an algorithm that become together to serve my purpose. Typically they are inseparable; it is necessary to organize the information inwards a for sure way to run an algorithm amongst the performance characteristics nosotros need. I would nation you lot can't split upward the two; is a hash tabular array the structure, the algorithms for insert, search, etc. or both? I'd nation both, because eithe i on their ain are useless.

The actual "OOP" degree code blueprint is alone determined subsequently I pick out a clear pic of the information as well as algorithm design. Once we've decided that we're going to work a quad-tree for our scene graph (again, that's an algorithm as well as a information structure), thence I become looking for language-specific idioms that volition produce this well.

(In this instance nosotros produce work objects, but they aren't polymorphic, as well as inwards most cases they're but glorified structures.)

Refactor Early, Refactor Often

Finally, I ever commencement a novel characteristic inwards X-Plane amongst a create clean work-surface past times refactoring all of the code I volition hold out touching equally heavily equally I can. You wouldn't know that the module inwards query wasn't version 1.0 (and this is before coding the novel feature).

The payoff of this is that it cuts new-feature evolution fourth dimension way downwards -- it's equally if the app was custom coded to brand the characteristic trivial. Most of the existent operate as well as evolution fourth dimension is spent inwards the refactoring.

One argue to favor refactoring over novel features for where to pass evolution fourth dimension is that when refactoring, you lot tin dismiss brand a divulge of small-scale changes as well as regression-test repeatedly. I notice this goes a lot smoother than changing a huge sum of code as well as thence discovering that multiple bugs causes zip to work. With the small-scale changes as well as regressions, bugs are caught a few at a time, making them tardily to stomp.

By comparison, a novel may hold out complex plenty that it has to hold out coded fairly significantly to hold out able to seek inwards useful ways. (The Rapid Development folks would yell at me for non having adequate seek code.) Regardless of your testing diligence, you lot can't deny that when you lot commencement a novel feature, at that spot are already to a greater extent than ways to seek the one-time i than the novel one.