RE: Intelligent Design
January 14, 2016 at 10:06 pm
(This post was last modified: January 14, 2016 at 10:08 pm by KevinM1.)
Esq, that's a great point about manufacturing. The same can be said for software. The entire point of Object Oriented Programming is to create reusable, plug-and-play objects that can be arranged in just about any configuration. The particulars are hidden away through abstraction, popularly through a mechanism called an interface. Rough pseudocode example (you may need to scroll through the code block to see the entire thing):
Because Circle and Rectangle implement IShape, they can be used interchangeably wherever an IShape is required. Even better, the calling code (doSomethingWithShapes in this case) doesn't care what kind of shape it gets. So long as it fulfills the promise provided by the interface that it will have a calculateArea() method, all is good.
And that's how high level software is written (as opposed to on-the-metal software like OSes, drivers, etc.). You write what you want to do as abstractly as possible with interfaces and other things known as abstract classes, dealing with concrete classes and objects only when necessary. And by encapsulating discrete functionality into classes, you basically create the software version of LEGOs that connect to each other through simple, abstracted APIs.
While there's a fair amount of upfront work (and creating objects tends to be more memory expensive than using primitive data structures like arrays or structs), the extensibility and reusability of writing code this way really shines in medium-to-large projects. Components can also be shared between projects very easily this way, often with no tweaking involved. Have a kick ass logger? Or email handler? Or anything else general purpose? Use it anywhere.
So, much like hardware has made a conscious effort to reduce the kinds of connection used (notice how USB essentially connects the world right now?) software has made a conscious effort to embrace sensible API structures.
Code:
interface IShape
{
float calculateArea();
}
class Circle implements IShape
{
float radius;
public function __construct(float radius)
{
this.radius = radius;
}
public function calculateArea()
{
return 3.14 * this.radius * this.radius;
}
}
class Rectangle implements IShape
{
float width;
float length;
public function __construct(width, length)
{
this.width = width;
this.length = length;
}
public function calculateArea()
{
return this.width * this.length;
}
}
function doSomethingWithShapes(IShape shape)
{
// blah blah
someResult = shape.calculateArea(); // is it a circle or rectangle?
return someResult;
}
Because Circle and Rectangle implement IShape, they can be used interchangeably wherever an IShape is required. Even better, the calling code (doSomethingWithShapes in this case) doesn't care what kind of shape it gets. So long as it fulfills the promise provided by the interface that it will have a calculateArea() method, all is good.
And that's how high level software is written (as opposed to on-the-metal software like OSes, drivers, etc.). You write what you want to do as abstractly as possible with interfaces and other things known as abstract classes, dealing with concrete classes and objects only when necessary. And by encapsulating discrete functionality into classes, you basically create the software version of LEGOs that connect to each other through simple, abstracted APIs.
While there's a fair amount of upfront work (and creating objects tends to be more memory expensive than using primitive data structures like arrays or structs), the extensibility and reusability of writing code this way really shines in medium-to-large projects. Components can also be shared between projects very easily this way, often with no tweaking involved. Have a kick ass logger? Or email handler? Or anything else general purpose? Use it anywhere.
So, much like hardware has made a conscious effort to reduce the kinds of connection used (notice how USB essentially connects the world right now?) software has made a conscious effort to embrace sensible API structures.
"I was thirsty for everything, but blood wasn't my style" - Live, "Voodoo Lady"