Please visit my new campsite listing site ukcampingmap.co.uk


Posts Tagged ‘object oriented programming’

I think I get it

Thursday, August 27th, 2009

‘It’ being object oriented programming (OOP).

For a long time I got frustrated with reading tutorials telling you about inheritance and other useful features of using OOP’s class-based approach. The trouble is that in the example scenarios given (eg a ‘bird’ class which inherits from an ‘animal’ class) the set theory-like approach to writing code to represent things makes sense. But when it comes to building a website or something similar, and creating classes which do not display a nested structure, the analogy with set theory falls away and largely (at least in the websites I’ve worked on) you’re left with a convoluted structure which, at first glance, could just be done away with in favour of using normal variables and functions instead of properties and methods. I could see how perhaps the stronger structure might help organise code, and was gradually getting mor ecomfortable with the techniques, but still didn’t really get why using OOP was so important.

But now I think I do, courtesy of wikipedia, which has the best summary of the motivation behind OOP I’ve ever read:

The methodology focuses on data rather than processes, with programs composed of self-sufficient modules (objects) each containing all the information needed to manipulate its own data structure. This is in contrast to the existing modular programming which had been dominant for many years that focused on the function of a module, rather than specifically the data, but equally provided for code reuse, and self-sufficient reusable units of programming logic, enabling collaboration through the use of linked modules (subroutines). This more conventional approach, which still persists, tends to consider data and behavior separately.

After reading this it’s all become clear to me what the point is – creating a more concrete connection between a thing and what can be done to it. In a real world situation I would liken it to having the steering wheel in the car, always ready to be used to steer the car, as opposed to having a steering wheel which can be passed around and maybe tried on other things, eg a chicken, a whale, a front-bench tory… where the results would be unpredictable (apart from the tory, who would almost certainly find the experience arousing). And as long as you define the steering wheel at a high enough level (eg the ‘vehicle’ class has a steering wheel, which the ‘car’ class inherits) you don’t fall foul of code duplication either.

I have shot ourselves in the foot

Sunday, April 26th, 2009

A word of warning for anyone not remotely interested in programming, the rest of this post will be a long, hard, meaningless slog, though not without some comic payoff.

Object oriented programming (OOP) is a major concept to grasp if you want to go places as a programmer. My problem for the last year or so is that I have got to grips with the concept pretty well, but was still no closer to being able to do it.

OOP is a bit like set theory and group theory, which I’m more than familiar with as I studied maths for a good many years, and I could easily come to grips with the 4 basic tenets of OOP, but despite this I was still mystified as to how all these web-pages (in my case, though OOP reigns supreme in any big software project) know what to do with all these classes you’ve set up; how it knows where to find their definitions in particular was the bit that mystified me.

I know have the answer. After posting a question on a forum I discovered it’s not actually part of OOP theory at all, it’s just that programming languages that support OOP tend to have some way of enabling you to very concisely specify where the classes are to be found. The obvious answer I suppose.

Using the new search term “autoinclude” I was able to find a decent how-to tutorial. The tutorial is very well put together, save for some pretty on the cheap editing. Someone at a late stage before its publication must’ve thought it could do with coming across as more personable. A quick find and replace later, and all the “we”‘s have gone, replaced by “I”‘s. Which has left such beautiful turns of phrase as:

I have shot ourselves n the foot

I are using two external constants

I have a million products in my database, and I are not going to tell you about any of them!