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


Posts Tagged ‘oop’

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.

More pizza squabbles

Saturday, May 2nd, 2009

The other day I ruminated on whether going for the smallest slice of pizza initially will result in your getting more in the long run than going for the biggest first, and concluded that I should try and write a computer model of it, which I have now done (bear in mind the page is a bit slow to load), to a degree.

The graphs below, plotting number of pizza slices vertically (increasing as you go down) and number of people horizontally (increasing to the right), and average over 100 trials per slices/people combination, show that there is a small area, when the number of people is roughly equal to half the number of pizza slices, and the number of people is not too big, where taking the smallest available slice on the first go pays off. But an even better strategy under these circumstances is to take the smallest of the N largest slices, where N is the number of people.

Graphs of pizza slice taking strategies

Graphs of pizza slice taking strategies

There are lots of alterations I’d like to make to the model, as it’s not quite a true reflection of reality.

  1. Add the ability to have people eating at different rates.
  2. At present the sizes of the slices are chosen randomly (and then normalised to make sure the total size is equal to the number of slices). However, the distribution in real life pizza slice sizes is far from random; they will probably have roughly a normal distribution, with very few extra big slices and very few extra small ones. Because the size of one slice is not independent of the sizes of other slices, they are likely to follow a more complex distribution, but it’s probably beyond me to work out what it is.
  3. The colours are nowhere near contrasting enough. I’ve done a bit of a fiddle on the greens to bring them out a bit more, but it’s a bit of a cheat that probably won’t always work. There are regions in the graph where one strategy is consistently slightly better than the other, but it doesn’t show. A high contrast version (below) does show this info (and in fact also showing that most of the time you’re better off wtha smaller slice first strategy if more than two slices per person), but I think it’s important to show the subtleties too.
    pizza-slices-high-contrast
  4. The slow loading is an issue.

For the record, now that I know how OOP works in PHP I used my new skills to write the model, and it was most useful. Much easier to keep track of what values you’ve written to where.

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!