Question:
WHY do people use class(es)?
1970-01-01 00:00:00 UTC
WHY do people use class(es)?
Six answers:
Daniel B
2012-02-25 14:28:58 UTC
Great response from McFate. Here are a few things I will add. The big advantage to classes versus function + global variables is that as an application gets larger and more complex the classes can make things much easier to maintain. The class allows you to hide the way the code works and the data is represented internally from the rest of the application which makes it much safer to change that code without impacting other parts of your program. If I have a variable that is private to a class I can be sure only code in that class can effect it, but with a global any part of the program could change the value. When you get into complex programs you will also find the object oriented design patters can make it much easier to solve tricky problems in your applications. For example if I have a set of functions that need to work slighting differently in different situations I could write the functions with a bunch of switch/case statements to handle the differences. But in an object oriented program I could use the strategy pattern to encapsulate the differences in their own classes just making the program much easier to maintain and extend.



Well written classes are also a lot more re-usable then function with global variables. If my class self contained I can just copy it into another program and re-use it.



Your SQL example seems to go beyond just a question of classes. Even if you decide to do procedural programming there is a major advantage to consolidating certain tasks into a single place in your code. For example if I want to read data from a specific table in my database I write a function that does this and returns the results. I will then always call this function from other parts of my code instead of going direct to mySql. This way if I have to change something about how that table is accessed, for example if the table name changes, I only have to make the change in one place. It also allows be to easily add additional functionality in the future. Let's say I decided that this table is used so often that I want to cache the result in memory. I could add this caching capability to my function (or class) without having to touch other code in my application.
?
2012-02-25 14:26:56 UTC
this may change your mind



http://blog.fedecarg.com/2008/05/29/30-useful-php-classes-and-components/



http://coding.smashingmagazine.com/2009/01/20/50-extremely-useful-php-tools/



every big php project uses php unit and php debug... as your projects bigger, you would need to use classes or programmers to help you code



TO USE OR NOT TO USE CLASSES?????



it comes down to whether you trust the coder or not!!



php classes are like python modules... I can create a social networking site in python using django in 2 to 3 weeks, if i code everything by hand... or i can do it in half an hour using pinax which includes modules for social networking websites



pinaxproject.com



what do i choose?



I test the modules, if they're good i use them, if not i don't



Using a class to connect to mysql in a small project is stupid, i agree, because it takes more time to create the class than to write the codes but are you against DRY and MVC? I don't think so :)
2012-02-25 14:12:04 UTC
This is true about classes which you didn't write. It takes time to make it work. Now I realized that packaged classes Never helped me. The most successful thing is to have the real math equations and procedures and you code it by yourself.



I deal with java. I like classes.

There is nothing more natural than dealing with an object. It is a problem solving technique to make modeling the real world more easier. Instead of solving the problem in steps you create object and let them solve themselves. Shielding yourself from the details makes you solve more complicated tasks better.



Classes are not the important thing, it is OBJECT which make the magic, there is nothing more natural than treating your problem as a set of objects. You can't have objects without classes.



I am not a programmer. I had a classical engineering education and before programing I dealt for years with solving pages and pages of problems in steps way. When yo do it in an object fashion you can do more complex tasks easier. The funny thing is that people with classical engineering education can't comprehend the object way in solving the world. I guess object oriented programming are almost close to solving real world problems by building a minimized model of it. Or modeling the system in an analog computer. Probably this is the reason why OOP programs and analog computers were simple in design.



Steps way doesn't make sense. When you deal with objects you can do more complicated tasks.





So I wrote a huge scientific programing java and it is imposable to handle something that complicated without separating the tasks in classes. Dealing with objects. It is impossible to deal with this huge complexity without encapsulating them in objects.
flood
2012-02-25 13:26:09 UTC
for a few seconds I thought "what the hells this guy on about" then I saw you where talking about mysql not C++ and realized I cant help :(
2012-02-25 13:26:05 UTC
I love classes. They may be misused at times, but I cannot imagine making any large system without them. I may not have been programming for 40 years... I'm only half that old, but I find object oriented programming and classes to be awesome.



Edit: I may have misunderstood your question.

Edit 2: You've never found a useful class?

Like any class? I've used so many useful classes I can't even count them. Math classes (hell if I'm implementing sin, cos, etc), List classes, all sorts of classes.
McFate
2012-02-25 05:30:58 UTC
It's possible to write badly designed code in any framework, but abuse of a framework is not a justification for not using the framework at all. In my own 30+ years of programming, I've found OO to be as big an improvement over its predecessors, as block-structured languages were over FORTRAN and BASIC. Is OO going to make your life significantly easier for a two-line MySQL access? Of course not, but that's a toy example. What happens when there are hundreds of tables and thousands of queries? Then a framework like Hibernate looks a lot better than the Perl/PHP mess we'd otherwise end up with.



You've never found a "class" from someone else worth anything? I use hundreds or thousands of classes written by others -- libraries like SAX, DOM, JAXB, Xerces -- on a daily basis. Sure, there are badly designed classes written by newbies here on YA!, but so what? They're just learning. As someone much wiser than I once said: "Good judgment comes from experience; experience comes from bad judgment." My coworkers produce classes I have no trouble using. Maybe you need to find a place where your peers are as good at programming as you are?



Classes are a great way for organizing code. Polymorphism/inheritance is a great way for reusing code -- reducing complexity, enhancing readability, and lowering maintenance cost. I agree that these great tools do no good in the hands of people who don't use them properly, but I think we disagree over how common it is to find usable work by others.



Let me give an example from a fairly major project which I've worked on recently: a loan servicing system. It has three main classes of receivables: curtailments, monthly amounts due, and fees. All of these inherit from a common Receivable class. The behavior of ALL types of receivables is in the superclass (e.g., when a receivable gets paid, record a Transaction for the payment being allocated). It is overridden in bits and pieces in the subclasses (e.g., fees don't pay down principal, curtailments change the loan's schedule going forward, etc.). The behavior hung on the Fee class itself isn't every last detail common to all receivables, it's just the parts that apply only to Fees.



In a non-OO language like C, I'd have to either have tons of duplicated code, or functions full of if-statements (if it's a fee, do this, otherwise do that). The former is expensive to maintain, the latter is difficult to read. Neither of those non-OO approaches scales well to the real production system which has more than 30 fee classes underneath the generic Fee. The organization of that code in an OO structure is much more straightforward, and it does scale to the full-sized problem. OO gives constructs that map well to the way that people naturally think about the world (hierarchies of types: thing -> vehicle -> car -> ...; instances of types: my Prius, your Prius, ...).



I still write lots of Perl scripts, and occasional bits of PHP. My Perl projects tend to be small, though, and I find Perl OO kind of weak and clunky, so the benefits don't justify the overhead -- just as with your PHP MySQL example. But for big real-world projects, OO confers significant advantages, which are critical to the efficiency of development and holding down the cost of maintaining the solution going forward. On top of that: if I weren't able to make use of large class libraries written by others, I'd be a lot less productive because I'd have to keep reinventing the wheel.



@M


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...