Question:
Why do we use classes? I don't get it...?
just "JR"
2011-04-21 09:56:47 UTC
I have 18 years experience in web development (and 40 in programming).
When "classes" came in, I thought them as an extention of the classical C "structure", but not much more.
In my 18 years of web development, I have used ONE class, ONCE: to allow me to use php to create PDF documents, but I had to extend the class, just to make what I wanted. In other words, I have to write class methods, which, in fact, was nothing else but "writing a simple function", and add it to a "library".
Nowadays, everyone uses classes, but very few KNOW what the class does! (And they come here and ask "why is my call to that class not working" - without showing us the class code!).
I often see people including HUUUUUGE classes in their code, just because they need ONE method, when that "method" can be written with only a few lines of code. The result is that they end-up with a script of 20Mb, while I end-up with 5kb, doing EXACTLY the same thing!
When they got a problem, they can't find the answer.
When I got a problem, I can pin-point it in seconds...
I COULD understand to use a class if you have a script that makes 100 calls to that class, but for ONE call?
My principle is: "If you have to use a piece of code twice, make it a function".
I guess that's why "classes" have been invented.
But, then, I would call them "libraries of functions"...

The only thing I can think of is the big drawback in file-sizes. In the 80's, the Autocad space shuttle, well detailed, required 400k of data-file. Now it needs 4Gb! The 3999600kb are for the UNUSED "classes"...
Please, don't tell me "they make development faster": that's bull...
Please, don't tell me "they make the execution faster": that's bull as well...
So, WHY do we bother with classes? I don't get it...
(I must be getting old) :-)
Seven answers:
?
2011-04-21 10:32:25 UTC
I'm working on an application for operators and players of an online game server. The application needs to be able to store information on multiple players on multiple systems.



I have 1 class that represents the individual server. It contains all of the information about the server, url, port, server specific settings. It also has an inherited keyed collection of players.



The player class contains all of the information on one specific player.



Each class has all of the methods needed by my program to work with the data it stores.



The server class has methods that handle the async requests between the application and the server, the methods to parse the data stream that is returned by the server into individual player information, data grids for displaying the information, etc.



Using this method gives me 2 advantages. Most obvious is that everything related to gathering/parsing data from 1 server is stored in one place. The server class.



The 2nd advantage is because everything in my program is stored in collections of strongly typed objects I can use LINQ (It's an SQL type language for working with in memory collections of data) to do various processing requirements for display purposes. Example: I want to see a list of players who are currently connected to the server. With LINQ I can say:



ElseIf cb_online_only = True Then

PlayerQuery = From characters In thisRealm.Realm_Characters _

Where characters.cd_Date_Rerolled = Nothing AndAlso characters.cd_Online = True _

Select characters _

Order By characters.cd_CurrExp Descending



PlayerQuery then becomes an enumerated copy of my player collection that I can either databind my data grid to or if I'm using an unbound datagrid I can use PlayerQuery to fill the datagrid.



As an old school coder myself it took some arm twisting for me to start using this type of code but I'm glad I did.



I agree completely that it is used many times when it does not need to be. There are times however when it is necessary to create a class that does only contain 1 method or a couple of properties.



An example of this would be with async communications. You have to create a class to hold the call back state data.
Robert
2011-04-21 10:20:35 UTC
There are languages now like Java and C# that are completely Object Oriented. I find OOD to be the easiest way to code software. I find it very easy to write modular code using objects, store them into packages and use them in any project. I've written many reusable models based on object oriented programming and I've been able to transfer those structures into many different projects. I've also learned complex API's that are all object oriented that may be complicated at first but once you learn them it becomes incredibly easy.



It sounds like some of those classes you're using are poorly designed. Which if that's the case then the fault is on the designer. I've written classes where each method is a few lines of code and you can easily find what's wrong with it. One project I was working on was written an old way using 10,000 lines of code to do one thing I switched it to OOD I reduced it to about 600. You're right though in that it doesn't make development faster if the structure is terrible and it certainly doesn't make execution faster, OOP is just my personal preference.
oops
2011-04-21 11:18:41 UTC
I of course won't argue that it makes execution faster, because it doesn't. Except in that [well written] object oriented code is much easier to maintain than procedural code, and well maintained code is likely to be faster than badly maintained code.



If you don't believe that object oriented programming can at least make development faster, then I'd be curious to see the projects you've worked on.



Of course, you never mentioned object oriented programming, but I can only assume that's what you mean when you are talking about classes. However, simply using classes does not make your code object oriented.



I guess I can understand your argument about file sizes. However, as a C++ programmer, that simply doesn't apply to me. I could include the entire standard library as well as boost in every single one of my source files, but if I don't use it, it won't affect my file size one iota.
2011-04-21 10:43:29 UTC
They DO make development faster, at the cost of bloated code.

They DO make execution faster, at the cost of bloated code.



Yes, I can write my own functions to do this or that but it will take me longer than making a call into a library where the same thing has already been done and optimized. Unfortunately to get that one optimized function I have to bring all it's cousins with it, thus bloated code. Personally I would locate the function I need in the class, examine it to see how it achieves efficiency (or doesn't, not all libraries are created equal) and then use that. But if I actually needed a lot of functions from that library it would be better to go ahead and #INCLUDE the library thus saving me a LOT of development time.



You have 40 years programming experience so you come from a different world than the current crop of developers (I've been in it 26 years myself). Coding has moved from an art-form (as you and I learned it) to an assembly-line.
?
2016-02-25 07:37:53 UTC
First Class.
Mr. crack ©
2011-04-22 23:55:04 UTC
Hense i am new to programming if you dont find my answer useful so pls dont insult me. You are right CLASSes are wastage of programming and it is very difficult to understand. If we can do something by 15 lines then it uses 10-20 classes and it is also wastage of time if some creative programmer like you make it easy and eliminate CLASS from field of programming and make pro field easy. Go ahead.
deonejuan
2011-04-21 10:34:23 UTC
It is not the class, it is the Object.



Really, if you ever did COBOL with the old, IBM 80-column punch card, same thing, except they had to print on all the cards: "DO NOT FOLD, SPINDLE or MUTILATE". With Objects no need to print that.



Computers were much more fun before Microsoft.


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