Learning OOP - is this logical?

Hello…

Been a guest here, a poster here, I asked tones of questions, got double-tones of answers, I believe I helped few, too.
I even bought a book from you guys, cannot say what it was but thank you, Rudy :smile:

I have been coding all sorts of websites, I finally got to be specialized in creating CMSs, each one technically so specified to do this or that, so no templates were used.

Now.
Been working for this company where OOP is a must. The boss knew I wasn’t comfortable with the idea of OOP, so he gave me the chance to learn it, wihch I will be eternally thankful for - no questions asked.

Last weekend I had this job where my task was to change few simple things. Simple, huh!
It took me straight THREE hours to determine what was wrong. Why?

Well, there was config, which then called this index, then ‘THIS’ called that, ‘THAT’ called this, ‘THIS’ called that…

I am calm, still.

I am being told the OOP works like a human brains, so it is so easy to adapt to it, cus it’s logical.
Uhm… Iam… rGH… Gin…
Now I am LOST.

Explain to me WHERE the above flow of info is LOGICAL?!?! and WHERE?!!?!
HOW the hell IS THAT LOGICAL?

Logic is here:
I go to Wallmart. I go to isle 4, where the books on ‘why you should use OOP’ are. Done.
My brain works like that.

OOP:
I go to San Fransico, then I go to A Walmart, then I go to the Walmart I live next to, I go through another eleven functions and HEY!! ‘why you should use OOP’ is really here.
My brain SHOULD work like this.

CAN YOU PLEASE TELL ME why OOP does make sense?!?!

1 Like

Because it greatly reduces the amount of code you need to work with at the one time bybreaking it up into a large number of smaller independent pieces.

Instead of having a million lines of code that are possibly going to be affected by a given change you at most only need to deal with a few hundred lines of code and usually a small enough piece of code that it can be printed on a single page.

Usually it results in needing to write a lot less code in total as well - say 1,000,000 lines of code instead of 1,000,000,000 lines.

Only when you are dealing with very small programs (say less than 10,000 lines) does OOP sometimes appear to involve a lot more code - at least until you get to the next project and find you can reuse 90% of the code from the previous one instead of having to write the whole thing over again.

I just like to add that OOP lends itself to being very portable to other projects. Take for instance a login/registration system, once you get it written for one website it’s very easily to use it for another website, with minimal changes to the code and differentCSS styling. Once you get a bunch of scripts written in OOP (Specially if you use namespaces) it’s almost as if you have your own framework, if not better in some regards for you know what makes the script(s) work from the inside-out. Now, before anyone me bashes me about Frameworks, I’m not saying Frameworks are bad; all I’m saying is learning OOP is a little different way of tackling a project (Specially if you want to learn OOP),

1 Like

The idea of OOP is that you should think in human ways rather than computer ways, so you think about how your system and objects interact with each other, instead of how the program executes code. You start off by modeling your objects based on real-life entities, and you only start to write application code after you have completed the initial OO analysis and design. It’s a mindset that not everyone gets it, but once you are comfortable with it, you wont want to write procedural code any longer.

I think your boss intends to build a large or at least medium scale application, and this is where OOP’s benefits come into play. With OOP, you build libraries of classes first and write your application later, there is an inherent cost and delay upfront, but it makes future development a lot better. With procedural programming, you wont get very far in a medium to large sized application, as development cost quickly grows much quicker and sometimes even exponentially. With OOP, its easier to add new features, maintain old code, and to test/debug your application.

You may take a look at the below image to understand the rationale to use OOP, when your application starts to grow larger and more popular:

Are you sure the OOP code you are looking at and should alter is also well written OOP code?

Scott

1 Like

I was thinking exactly this, Scott. I’ve been looking at a lot of very badly written OOP code recently. We’ve spent days deleting 10x more code than we wrote because nobody could follow it! When we were finished it was considerably easier to read.

One of the things I wish I’d learned earlier was DDD (Domain Driven Design) as it helps you understand your object boundaries, to properly encapsulate etc.

Hi there,

thank you, guys, for your inputs, that was quite a nice reading and I enjoyed that a lot.

“Usually it results in needing to write a lot less code in total as well”
“Take for instance a login/registration system”

But I have been doing just that while programming the site structurally! I had written bunch of useful ( well, that depends on a point of view, of course ) functions, I share some of 'em via my portfolio. I never write a code from the scratch, reinventing the wheel - the solid background, structural, is there, waiting to be harnessed spreading joy amongst the owners and viewers :slight_smile:

“The idea of OOP is that you should think in human ways rather than computer ways”
The most biggest problem of mine is to switch from structural to OOP way of coding - way of thinking. Sir, you don’t imply I think like a computer, cuz that is certainly not a case! :smiley: I am not a techy person at all, and never have been.

“you build libraries of classes first and write your application later,”
Been kind of doing this - not writing classes but rather bunch of functions not grouped into a class but still working together.

"Are you sure the OOP code you are looking at and should alter is also well written OOP code? "
I am not able to respond to that. I don’t know whether the code sucks or is great. With this particular job I think that was sort of based on Smarty. I was able to finish this job, I’ll know what to look for in the future when similar changes come up.


In another company, few months back, I worked on Symfony.
I HATED this framework, still do, with a passion, the work on it was ridiculous, clearing this damned cache every ten seconds ( waiting twenty to be cleared ). I was completely broken by this frame, I don’t think I will EVER get back to it.

The main framework I have been working on lately is Codeigniter. I rather get the idea, yet sometimes I feel like I was hit with a hammer, cuz I don’t get any words that are screaming at me from the monitor screen. I fail at simple things.

With structural programming - thing like that never happens.

Thank you very much for this little ‘kinda’ chat. I appreciate your time!

Take care, all of you Responders, and Sitepointers
Greg

You might be able to share 50% of your code by reusing functions from prior projects using structural programming. You should be able to share 95% of your objects when using OOP.

Also with sharing the functions you can’y just look at the one function when amending it to handle additional functionality - you would need to look at all the other functions that access the same data and everything that calls those functions. With objects you can change the internal functionality without needing to look at any of the code that calls it as long as the interface remains the same.

The big difference between unctions and objects is that objects encapsulate ALL of the code that relates to handling everything related to whatever the object represents and so the code it contains is completely independent of all other code whereas a function is still dependent on the other code that interacts with the same elements as it does.

I believe here lies my problem - I cannot grasp the idea of objects, even though my brains is supposed to understand it with no questions asked :smile:
I guess I need to code something small, and learn the stuff step by step. SIGH…

Thank you very much for clarifying bunch of things.
Cheers,
Greg Bialowas

That’s about the only way you can learn it properly.

Unfortunately with a small project you will not actually see the benefits straight away. That will come when you can reuse some of the classes you created for this project for a completely different project and so save on a lot of work because you will know that the code you are reusing works.

This is how I think of objects. Objects are what the classes build, when the program runs. That is as simple as I can explain them. The objects themselves are also easy to understand actually and the rules there are for the building of objects is also relatively easy to understand, although the process may seem a bit complicated at first. “What the heck is a constructor for?” - for instance.

I believe it is the abstractions in OO programming, which often cause the confusion for beginners. And at the same time, it is the ability to be abstract that makes OOP so powerful. Once you get the abstraction part, OOP is pretty cool.

As for your bout with Symfony’s cache. I have to mention it again, but maybe the source code wasn’t the best.

Symfony is certainly one of the most professional frameworks you will find out there.

Scott

I don’t believe that objects in programming are easier to understand than functions. They were created to bear some resemblance to real life objects but in practice objects in programming refer to very abstract concepts that have little to do with the outside world but a lot to do with programming concepts and abstraction layers - and you need to get to know them to be able to understand the objects. But it’s worth the effort, nevertheless.

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