473,725 Members | 2,032 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Objects, objects, so many objects! ;-)

Hi all,

I have a very general but quite significant question about objects.

My question is, when should I create them? I know thats a crap question so
let me explain a bit further.

Lets take an example of user management against a database. Things I might
like to do include:

- Creating a new user
- Changing a users phone number
- Deleting a user
- Getting a users age
- Updating a users password

The thing with all this is not one of those operations require the
instantiation of a User object. You could do it through static methods.

The thing is, you the sort of operations above must make up 80% or so of all
operations on users. Likewise, operations similar to these ones effect most
business entities, be it a Bug or a Role or a Car object.

So my question is - is it really the case, that a good proportion of the
time, you dont need to instantiate an object at all to achieve your aims, or
am I using a poor approach to design my applications?

Many thanks to anyone who can share some advice.

Kindest Regards

tce
Jul 21 '05 #1
11 1790
Hi tce,

While creating a new User would be performed via a static method, you would
still be instantiating a new User object as the return value of that method.
Here I make the assumption that you could have multiple users, making an
instance necessary. Another operation that would require a new User object
could be Update. For example, when persisting changes, it is quite
reasonable to retrieve the changed values, which could possibly not be held
in the original object.

Joe
--
http://www.csharp-station.com

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> wrote in
message news:en******** ******@TK2MSFTN GP11.phx.gbl...
- Creating a new user
- Changing a users phone number
- Deleting a user
- Getting a users age
- Updating a users password

The thing with all this is not one of those operations require the
instantiation of a User object. You could do it through static methods.

Jul 21 '05 #2
None of us really _needs_ objects. As you pointed out, we could write
everything using static methods and static properties. That's what I
used to do 10 years ago. It was called structured programming, and I
used C.

I'm not being sarcastic: people programmed in non-OO languages for
decades. The modern equivalent, in an OO language, is to make
everything static.

What your question boils down to, essentially, is: what are the
advantages of object oriented programming over structured programming,
and when should you apply object technology versus structured
technology?

The difficulties with using structured programming as you described in
your example are as follows.

o WIthout an object to stand guard over your user's phone number, how
can you be sure that the phone number is being set only by the static
method and isn't being diddled by your caller? If you're using a
"private" static string to store the phone number, and only allowing
access through a public static method, then in essence you've created a
singleton and only allow there to be one user in memory at a time. If
you want multiple users in memory at once, then somebody, somewhere,
has to remember their phone numbers. Without objects you rely on the
application program to promise not to muck with the phone numbers
except by calling your methods, because without objects you can't
encapsulate state.

o Without an object you can't easily encapsulate business rules. What
if whenever a user's phone number is changed you have to also make
calls to update the Address Book in Exchange? Does the application have
to remember to do this? Objects make this sort of thing much easier:
the application can just say user.PhoneNumbe r = "..."; and the object
verifies the new number and looks after informing Address Book that it
has changed.

However, objects aren't good for everything. I remarked elsewhere that
some of the worst designs I've seen were the result of newbie
programmers trying to make everything an object. Objects are really
good for low-level stuff that you can put a name to, like "user" or
"purchase order". Once you get up to a sufficiently high level, like
the top level of an application, the usefulness of calling such big,
complex things "objects" starts to fade rapidly.

Jul 21 '05 #3
tce,

I use static methods and members fairly infrequently -- and most of those
are general purpose helper or utility functions that require no instance
data other than whatever is passed in via arguments.

It may help to think of it this way: a class represents a a real-world thing
or concept (or an abstraction of one) including its data (fields /
properties) and behaviors (methods). It's just a way to organize data and
operations on that data into a coherent package. In general, most "things"
a program deals with have data that varies from one "instance" to another.
You will create objects from these classes. If you have methods that are
appropriate for more than one class then you may have organized the code
incorrectly -- or you may actually have a general purpose utility function
that can be a static method of a utility class that may never be
instantiated.

If you have some data that would be the same for *any* instance of a class,
then make it a static field of that class. If you have some action that
would be the same for *any* instance of a class, make it a static method of
that class. Otherwise, yes, create objects, and don't worry about having a
lot of them -- quantity is not so much the issue as coherent organization.
Remember that the object code associated with an object is only loaded once
no matter how many instances you have. Only the instance data requires
per-instance memory storage. Most objects don't have that much instance
data, so you really aren't going to eat up all your RAM in a hurry.
Assuming you use basic common sense in your designs up front, worry about
having too many object instances only if it becomes a real-world resource or
performance issue.

--Bob

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> wrote in
message news:en******** ******@TK2MSFTN GP11.phx.gbl...
Hi all,

I have a very general but quite significant question about objects.

My question is, when should I create them? I know thats a crap question so
let me explain a bit further.

Lets take an example of user management against a database. Things I might
like to do include:

- Creating a new user
- Changing a users phone number
- Deleting a user
- Getting a users age
- Updating a users password

The thing with all this is not one of those operations require the
instantiation of a User object. You could do it through static methods.

The thing is, you the sort of operations above must make up 80% or so of
all operations on users. Likewise, operations similar to these ones effect
most business entities, be it a Bug or a Role or a Car object.

So my question is - is it really the case, that a good proportion of the
time, you dont need to instantiate an object at all to achieve your aims,
or am I using a poor approach to design my applications?

Many thanks to anyone who can share some advice.

Kindest Regards

tce

Jul 21 '05 #4
Hello TCE,

I just want to add one thing to the discussion.

It is simple to imagine objects to be the computerized version of a "thing"
with methods being the "verbs" and properties being the "noun attributes,"
and some objects work OK that way. However, this view is limited and
flawed. It will quickly lead to a dead-end where the value of OO
programming is unclear, while the cost is very visible.

I would suggest that you pick up a slim book by Alan Shalloway and James
Trott called "Design Patterns Explained"
http://www.amazon.com/exec/obidos/tg...l/-/0321247140

This is an easy read.
Once you are done, you will have a MUCH better idea of how to use OO
methods, when to create an object, and what objects are really good for.

And... you will know when, and how, to create them.

--- Nick

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> wrote in
message news:en******** ******@TK2MSFTN GP11.phx.gbl...
Hi all,

I have a very general but quite significant question about objects.

My question is, when should I create them? I know thats a crap question so
let me explain a bit further.

Lets take an example of user management against a database. Things I might
like to do include:

- Creating a new user
- Changing a users phone number
- Deleting a user
- Getting a users age
- Updating a users password

The thing with all this is not one of those operations require the
instantiation of a User object. You could do it through static methods.

The thing is, you the sort of operations above must make up 80% or so of all operations on users. Likewise, operations similar to these ones effect most business entities, be it a Bug or a Role or a Car object.

So my question is - is it really the case, that a good proportion of the
time, you dont need to instantiate an object at all to achieve your aims, or am I using a poor approach to design my applications?

Many thanks to anyone who can share some advice.

Kindest Regards

tce

Jul 21 '05 #5
Hi everyone,

Thanks for you're advice.

Well, everyone seems quite entrenched in the idea that making objects to do
stuff is good. Which is as I expected.

I still don't see WHY I would make an object to do stuff when there is no
real need. Someone mentioned that it's to hold business rules - but static
methods can hold these rules just as easily as an instance.

I'm not at all worried about the performance cost of making objects, its
just in a great many situations (and in a great many sample applications)
there doesnt seem to be much call for making an object to perfrom an action.

In the applications that I'm playing with at the moment, the only time I
actually create individual objects is when dealing with collections of
entities - where making an in memory representation has real benefits. For
other things, like making an update to a database, there just doesnt seem
much point. Bearing in mind that I'm making web applications here - a lot of
the time I don't want in memory collections of lots of objects - I just
don't seem to need them.

Thanks for your comments so far

Kindest Regards

tce
Jul 21 '05 #6
read the book that I recommended... you will go from asking the question to
answering it fairly quickly.

This is a leap of faith to get here, but once you see the value of OO
design, you will wonder how you ever lived without it.

--- Nick

"thechaosengine " <sh856531@micro softs_free_emai l_service.com> wrote in
message news:ua******** ******@tk2msftn gp13.phx.gbl...
Hi everyone,

Thanks for you're advice.

Well, everyone seems quite entrenched in the idea that making objects to do stuff is good. Which is as I expected.

I still don't see WHY I would make an object to do stuff when there is no
real need. Someone mentioned that it's to hold business rules - but static
methods can hold these rules just as easily as an instance.

I'm not at all worried about the performance cost of making objects, its
just in a great many situations (and in a great many sample applications)
there doesnt seem to be much call for making an object to perfrom an action.
In the applications that I'm playing with at the moment, the only time I
actually create individual objects is when dealing with collections of
entities - where making an in memory representation has real benefits. For
other things, like making an update to a database, there just doesnt seem
much point. Bearing in mind that I'm making web applications here - a lot of the time I don't want in memory collections of lots of objects - I just
don't seem to need them.

Thanks for your comments so far

Kindest Regards

tce

Jul 21 '05 #7
Hi Nick,

Thanks for the suggestion. I'll take a look.

tce
Jul 21 '05 #8
The value of object oriented programming is not immediately evident
while you're writing the code. If it were, the industry would have
invented it in the 70's, not the 80's. You see the value only later,
when you try to modify your software in reponse to changing
requirements.

After all is said and done, objects are little more than a way to
organize your code so that things that belong together stay together,
and so that you can make hard-and-fast statements about what you can
safely change during maintenance and what you can't, and by "what you
can't" I mean that changes require you to do two hours' (or more) worth
of code research to make sure that the change won't break anything.

The real, real-world value of objects is that they allow you to make
more guarantees about how your code works, and package up things that
"belong together" in nice little bundles... so that two months or two
years from now when you (or someone else) comes to make changes to the
code, you can easily find where to make the change and you can make the
change secure in the knowledge that you're not breaking something
somewhere else.

This was what was "wrong" with procedural programming: it was not that
it wasn't expressive enough. C can express anything that C# can
express. The problem was that the language didn't help you organize
your code so that it was easy to understand and change. Before I
learned C++ and C#, I was writing "objects" in C. Why? Because
experience had taught me that this created more maintainable code than
did the usual procedural style of programming. When C++ came along, and
then C#, I was happy that the language finally helped me do what I had
been doing without any help for 10 years.

So, in the end all I can say is that if you finish writing your program
and say, "I don't see what the big deal about objects is," then you're
making a premature evaluation. You need to write your code then come
back and change it again, and again, and again. Only then will you
start to see that it's all about organizing your software, and it's far
easier to change software in which everything to do with a customer is
there, in the Customer object, than it is to change software in which
customer data is manipulated all over the place.

In the end, it's just an organizational convenience that, like all
other changes in the way we organize software, forces you to think
about problems in different ways.

As well, O-O programming long ago passed the
"flash-in-the-pan-marketing-fad" phase of its existence... and it's
still around and used widely. That tells me that it has great value.
Programmers are not idiots. They use what works, and what doesn't work
eventually dies. O-O addresses some important maintenance and
programming issues, otherwise we would have chucked it ten years ago.

Jul 21 '05 #9
Hi Bruce,

This is a very good description. I wish that I could have said it as well.

Thanks,
--- Nick

"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ c13g2000cwb.goo glegroups.com.. .
The value of object oriented programming is not immediately evident
while you're writing the code. If it were, the industry would have
invented it in the 70's, not the 80's. You see the value only later,
when you try to modify your software in reponse to changing
requirements.

After all is said and done, objects are little more than a way to
organize your code so that things that belong together stay together,
and so that you can make hard-and-fast statements about what you can
safely change during maintenance and what you can't, and by "what you
can't" I mean that changes require you to do two hours' (or more) worth
of code research to make sure that the change won't break anything.

The real, real-world value of objects is that they allow you to make
more guarantees about how your code works, and package up things that
"belong together" in nice little bundles... so that two months or two
years from now when you (or someone else) comes to make changes to the
code, you can easily find where to make the change and you can make the
change secure in the knowledge that you're not breaking something
somewhere else.

This was what was "wrong" with procedural programming: it was not that
it wasn't expressive enough. C can express anything that C# can
express. The problem was that the language didn't help you organize
your code so that it was easy to understand and change. Before I
learned C++ and C#, I was writing "objects" in C. Why? Because
experience had taught me that this created more maintainable code than
did the usual procedural style of programming. When C++ came along, and
then C#, I was happy that the language finally helped me do what I had
been doing without any help for 10 years.

So, in the end all I can say is that if you finish writing your program
and say, "I don't see what the big deal about objects is," then you're
making a premature evaluation. You need to write your code then come
back and change it again, and again, and again. Only then will you
start to see that it's all about organizing your software, and it's far
easier to change software in which everything to do with a customer is
there, in the Customer object, than it is to change software in which
customer data is manipulated all over the place.

In the end, it's just an organizational convenience that, like all
other changes in the way we organize software, forces you to think
about problems in different ways.

As well, O-O programming long ago passed the
"flash-in-the-pan-marketing-fad" phase of its existence... and it's
still around and used widely. That tells me that it has great value.
Programmers are not idiots. They use what works, and what doesn't work
eventually dies. O-O addresses some important maintenance and
programming issues, otherwise we would have chucked it ten years ago.

Jul 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
8251
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list iterators and how they are behaving in situations like this. #include <iostream> #include <list> class Test; typedef std::list< Test* > Tlist;
9
1887
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects, but that's beside the point, for the most part. Here's the question: I want to be able to change the references (including deleting them). Is there any way to do that besides using pointers rather than references for the STL library? I'd also prefer to avoid using const_cast, if it is indeed...
6
2576
by: Alfonso Morra | last post by:
I have written the following code, to test the concept of storing objects in a vector. I encounter two run time errors: 1). myClass gets destructed when pushed onto the vector 2). Prog throws a "SEGV" when run (presumably - attempt to delete deleted memory. Please take a look and see if you can notice any mistakes I'm making. Basically, I want to store classes of my objects in a vector. I also have three further questions:
3
3028
by: ytrewq | last post by:
Should dynamic ("expando") properties be restricted to native and user-defined objects? Or should host objects - such as references to the browser or a plug-in or to the document and its elements - also allow them? Adding (and removing) object properties dynamically is an acceptable and common practice in JavaScript, and greatly adds to the power and character of the language. Essentially, an object in JavaScript can be considered to...
8
1861
by: Lüpher Cypher | last post by:
Hi, Suppose we have a hierarchical class structure that looks something like this: Object | +-- Main | +-- Object1
161
7844
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.. (After reading that
7
8222
by: Jo | last post by:
Hi, How can i differentiate between static and dynamic allocated objects? For example: void SomeFunction1() { CObject *objectp = new CObject; CObject object;
21
2210
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not necessarily the same... class base { int x;
27
2560
by: SasQ | last post by:
Hello. I wonder if literal constants are objects, or they're only "naked" values not contained in any object? I have read that literal constants may not to be allocated by the compiler. If the Standard is saying that "object is a region of storage", I deduce from that that literal constants aren't objects because they may not be alocated as regions of storage in the memory.
14
6019
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared static inside functions (i.e. local static objects) 5. objects declared at file scope.
0
8888
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9111
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8096
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4782
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.