How do I make the transition from procedural to OOP?

I spent some time when I first started replacing ALTER GOTO statements with procedural code in CoBOL. The code I was updating had been written before CoBOL properly supported procedural coding and so every “function” call consisted of an alter of the goto at the end of the “function” to point back to the statement after the goto that started the “function” call. Of course then there was the code where the alter had been misused. Code using simple goto statements was easy in comparison. Nothing like reaching a goto statement in the code that didn’t specify where it would take you and having to try to track back through the code to try to find the last statement that changed where the goto points.

There are a whole range of different programming paradigms each of which serves a different purpose. Any code you write will be simpler if you use a language that supports the appropriate paradigm. Using the appropriate one is what distinguishes a professional - where procedural is appropriate then that is what a professional would use.

I have worked with procedural, reporting, rules based, classical OOP and prototyal OOP languages and all have their advantages and disadvantages depending on what you are trying to do. For example back in the early 1980s I was producing reports using RPG2 that often had less than 5% of the amount of code that producing the same report in CoBOL would have needed - simply because for producing reports a reporting language is more appropriate than the alternatives.

Learning the first language for a new paradigm is much harder than learning a new language that uses a paradigm you are already familiar with. When I learnt classical OOP (using C++) I got hold of a library and created an application program to do something where I knew exactly what the program needed to do (I wrote a complete cashbook program). Because the library already contained classes to do a lot of the processing it was almost impossible to write procedural code that could perform the necessary interactions and far easier to work out how to create the classes that would link things together to perform the needed processing.

One benefit that a language that supports two different paradigms (such as PHP) has is that you don’t have to finish learning the new paradigm before being able to use it. If you have existing procedural PHP then you can implement a few classes in it really easily without having to switch across all at once. For example you can convert existing mysqli calls to use OOP on a call by call basis.

mysqli_prepare($db, ...

becomes

$db->prepare(...

and your code is one command closer to being converted to use OOP.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.