Iain's IT Solutions Ltd.

Iain's IT Solutions Ltd Mobile Nav Menu

Object orientated coding V's procedural


I've been writing code for quite a few years now. VBA, VB.Net, C#, PHP & JavaScript. Hundreds if not thousands of lines of my code are run every day in my range of apps - but I almost never use classes. I know forms in MS Access are a type of class - but I mean classes I've written myself.

I've read plenty about OO & work with a guy who writes OO code, but despite this, I just can't get my head around it. I worry I'm missing some huge opportunity to write better code, with lots of reuse - but I don't think I write inefficient duplicated code at the moment...

I get the theory behind it - the person class with properties of height and age and methods of walk and sit or the car class with the property colour and the method steer - but I just can't seem to apply it to my code.

I have one class I wrote and use regularly. Written in Excel VBA it's an interface to another app. It's good as the other app changes occasionally and I only have to adjust for these changes in my class - in one place - but I could do the same thing if I used functions and routines in a module...

Only in the past few months have I started learning PHP & JavaScript. I'm worried I'm setting off down the wrong path by writing these in my familiar procedural style.

I think my code is clean, clever, efficient, well commented, uses good naming conventions and isn't confusing to follow - but of course I would say that. Perhaps what I need is to describe something I've built to an OO developer and get them to explain how it could have been written with classes - perhaps than I'll see the benefits. I'm not writing code about people or cars, so I find the analogy hard to apply. Perhaps a future blog will be "My object orientated epiphany" or "Object orientated - no point".

Iain Clark 22-Sep-2011
As an exciting update to this, I've recently written a class. I was writing some VBA behind MS Excel. The script iterates thru records on a worksheet, and for each, writes the data to an MS Word template. There are multiple templates, and as it moves down the records it decides which template to put the data into - so it's either opening the template, or adding to an already open template. What I did was create a class to open the template, create an instance of it, pass it some attributes then add it to a collection. As the code moves down the records, it iterates the collection of classes - if it finds the class it's looking for, it can make reference to the already open Word template and if not found, create a new instance of the class, opening the new template, add it to the collection, and write to it. I have to admit it's a very clean solution to the problem of finding open instances of MS Word...

No links allowed (to stop persistent spamming...)

Optional - enter if you want to be alerted when new comments are added