473,804 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hypothetical: All code in classes but main()

This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather than
in namespace scope (obviously classes themselves are an exception to this),
and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?

BTW, yes, I /do/ favor this approach.
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #1
47 1807

"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:gJ******** ************@sp eakeasy.net...
This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?

BTW, yes, I /do/ favor this approach.


wow, finally I meet somebody who likes (c++) classes like me :). I am "with
you" ... I never really liked that c-style programming, if you can do the
same thing with classes, but this just my opinion and taste - I am not
saying, that I have the truth in this issue. Maybe its like that somebody
likes tea, but somebody else might hate tea but like coffee :).

I dont see eny limitations in "normal programming" - I would defenitely
choose class route if possible . I was jus thinking, that some very critical
fast code might need pure c-code because of efficiency. For example in chess
game programming you calculate 2 000 000 moves per second, so classes might
not be suitable in all programming because they might be a little bit too
slow ... but to be honest with you, I am not sure of this ... Does somebody
know if this is true or not?
Jul 22 '05 #2

"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:gJ******** ************@sp eakeasy.net...
This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?

BTW, yes, I /do/ favor this approach.


wow, finally I meet somebody who likes (c++) classes like me :). I am "with
you" ... I never really liked that c-style programming, if you can do the
same thing with classes, but this just my opinion and taste - I am not
saying, that I have the truth in this issue. Maybe its like that somebody
likes tea, but somebody else might hate tea but like coffee :).

I dont see eny limitations in "normal programming" - I would defenitely
choose class route if possible . I was jus thinking, that some very critical
fast code might need pure c-code because of efficiency. For example in chess
game programming you calculate 2 000 000 moves per second, so classes might
not be suitable in all programming because they might be a little bit too
slow ... but to be honest with you, I am not sure of this ... Does somebody
know if this is true or not?
Jul 22 '05 #3
Steven T. Hatton wrote:
This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather than
in namespace scope (obviously classes themselves are an exception to this),
and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?

BTW, yes, I /do/ favor this approach.


I almost always have a single object to represent the whole application,
and use main() only as an entry point. That said, forcing _everything_
into classes seems to disallow use of some of my favorite C++ features
and programming styles. For example, many operator overloads belong
outside the class.

template< typename T >
int add_one( T const& t )
{
return 1 + t; // If T is a UDT, where is operator + defined?
}
Jul 22 '05 #4
Steven T. Hatton wrote:
This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather than
in namespace scope (obviously classes themselves are an exception to this),
and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?

BTW, yes, I /do/ favor this approach.


I almost always have a single object to represent the whole application,
and use main() only as an entry point. That said, forcing _everything_
into classes seems to disallow use of some of my favorite C++ features
and programming styles. For example, many operator overloads belong
outside the class.

template< typename T >
int add_one( T const& t )
{
return 1 + t; // If T is a UDT, where is operator + defined?
}
Jul 22 '05 #5

I normally don't jump into hypothetical discussions like this, but parts
of this just rankled me.

Juha Kettunen wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:gJ******** ************@sp eakeasy.net...
[snip]
If you were forced at gunpoint to put all your code in classes, rather


than
in namespace scope (obviously classes themselves are an exception to


this),
and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?


Personally, I can see no practical functional limitations or benefits.

[snip]
I dont see eny limitations in "normal programming" - I would defenitely
choose class route if possible . I was jus thinking, that some very critical
fast code might need pure c-code because of efficiency. For example in chess
game programming you calculate 2 000 000 moves per second, so classes might
not be suitable in all programming because they might be a little bit too
slow ... but to be honest with you, I am not sure of this ... Does somebody
know if this is true or not?


Where this whole thing starts to spiral out of control is here. "C-code"
by itself is no slower or faster than the equivalent "C++-code" (ie,
class-based code). If you wanted to, you could write "slow" or "fast"
code in either. In fact, writing the theoretical "fastest" program is
possible in C and C++ - your compiler and your algorithm are what
determine how "fast" your code will run.

For the record, I make games, and I need "efficient" code to move big
chunks of data around very quickly. I use classes with impunity - the
things I usually watch for are polymorphism and virtual functions, and
hidden code (destructors and constructors that aren't immediately
obvious). And even these I use in some cases.

But being told to put all your code into classes is as illogical to me
as being told to use only words and phrases from Shakepeaean iambic
pentameter as variable names. Or, more practically but no less obtuse,
hungarian notation. Does it help anything? Not really. Does it hurt
anything? Not really. And if it does either it does them in equal
measure. So why insist on it?

I mean, compare the elegance of this:

int max(int a, int b) { return (a > b) ? a : b; }

int larger = max(1, 2);

To this:

class Math
{
public:
static int max(int a, int b) { return (a > b) ? a : b; }
};

int larger = Math::max(1, 2);

Only Java programmers could find the latter more legible.

Both can run just as fast (if you inline the former; the latter is
intrinsically inlined), so it's not a speed issue. Namespace Math::max
provides (essentially) the same amount of collision protection and
encapsulation as class Math::max, so it's not a maintennance issue.

What it is, is a design issue. What you're telling me is that there are
such things as "Math" objects. In order to find the max of two numbers,
I have to go get a "Math" and use the "Math"'s "max" operation. Or, in
the static case, that "Math"'s have a behaviour or mechanism that is not
specific to any one "Math", but is nonetheless "Math" object-specific
behaviour.

On the other hand, the free function tells me that "max" is an operation
in category (ie, namespace) "Math". In other words, "max" is a "Math"
operation.

Doesn't the latter just make more sense?

So in conclusion, what the hell are the benefits of such arbitrary
constraints? Write your program so that it makes logical sense and
models the problem it is trying to solve as closely as possible. If the
language has the tools, and if they don't lead to bad design and
maintennance problems, use them.

If you really feel the need to apply arbitrary constraints, why not
write your code in morse code ( http://www.ioccc.org/1986/hague.c )?

mark

Jul 22 '05 #6

I normally don't jump into hypothetical discussions like this, but parts
of this just rankled me.

Juha Kettunen wrote:
"Steven T. Hatton" <su******@setid ava.kushan.aa> wrote in message
news:gJ******** ************@sp eakeasy.net...
[snip]
If you were forced at gunpoint to put all your code in classes, rather


than
in namespace scope (obviously classes themselves are an exception to


this),
and 'bootstrap' your program by instantiating a single application object
in main(), would that place any limitations on what you could accomplish
with your program? Are there any benefits to doing things that way (other
than survival)?


Personally, I can see no practical functional limitations or benefits.

[snip]
I dont see eny limitations in "normal programming" - I would defenitely
choose class route if possible . I was jus thinking, that some very critical
fast code might need pure c-code because of efficiency. For example in chess
game programming you calculate 2 000 000 moves per second, so classes might
not be suitable in all programming because they might be a little bit too
slow ... but to be honest with you, I am not sure of this ... Does somebody
know if this is true or not?


Where this whole thing starts to spiral out of control is here. "C-code"
by itself is no slower or faster than the equivalent "C++-code" (ie,
class-based code). If you wanted to, you could write "slow" or "fast"
code in either. In fact, writing the theoretical "fastest" program is
possible in C and C++ - your compiler and your algorithm are what
determine how "fast" your code will run.

For the record, I make games, and I need "efficient" code to move big
chunks of data around very quickly. I use classes with impunity - the
things I usually watch for are polymorphism and virtual functions, and
hidden code (destructors and constructors that aren't immediately
obvious). And even these I use in some cases.

But being told to put all your code into classes is as illogical to me
as being told to use only words and phrases from Shakepeaean iambic
pentameter as variable names. Or, more practically but no less obtuse,
hungarian notation. Does it help anything? Not really. Does it hurt
anything? Not really. And if it does either it does them in equal
measure. So why insist on it?

I mean, compare the elegance of this:

int max(int a, int b) { return (a > b) ? a : b; }

int larger = max(1, 2);

To this:

class Math
{
public:
static int max(int a, int b) { return (a > b) ? a : b; }
};

int larger = Math::max(1, 2);

Only Java programmers could find the latter more legible.

Both can run just as fast (if you inline the former; the latter is
intrinsically inlined), so it's not a speed issue. Namespace Math::max
provides (essentially) the same amount of collision protection and
encapsulation as class Math::max, so it's not a maintennance issue.

What it is, is a design issue. What you're telling me is that there are
such things as "Math" objects. In order to find the max of two numbers,
I have to go get a "Math" and use the "Math"'s "max" operation. Or, in
the static case, that "Math"'s have a behaviour or mechanism that is not
specific to any one "Math", but is nonetheless "Math" object-specific
behaviour.

On the other hand, the free function tells me that "max" is an operation
in category (ie, namespace) "Math". In other words, "max" is a "Math"
operation.

Doesn't the latter just make more sense?

So in conclusion, what the hell are the benefits of such arbitrary
constraints? Write your program so that it makes logical sense and
models the problem it is trying to solve as closely as possible. If the
language has the tools, and if they don't lead to bad design and
maintennance problems, use them.

If you really feel the need to apply arbitrary constraints, why not
write your code in morse code ( http://www.ioccc.org/1986/hague.c )?

mark

Jul 22 '05 #7
Jeff Schwab wrote:
Steven T. Hatton wrote:
This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather
than in namespace scope (obviously classes themselves are an exception to
this), and 'bootstrap' your program by instantiating a single application
object in main(), would that place any limitations on what you could
accomplish
with your program? Are there any benefits to doing things that way
(other than survival)?

BTW, yes, I /do/ favor this approach.


I almost always have a single object to represent the whole application,
and use main() only as an entry point. That said, forcing _everything_
into classes seems to disallow use of some of my favorite C++ features
and programming styles. For example, many operator overloads belong
outside the class.

template< typename T >
int add_one( T const& t )
{
return 1 + t; // If T is a UDT, where is operator + defined?
}


I believe there is actually a technical limitation met when binary operators
(ones that take two arguments) are forced to be class members. I need to
review the section, but I believe there are some symmetries that are lost.
IOW A@B works but B@A doesn't for cases such as '+' and '*'. Another
problem is the fact that functions which were external to a class now gain
access to member data and potentially become bound to it. This is the
argument Stroustrup gives for having helper functions at namespace scope.

I actually proposed another access specifier in addition to private,
protected, public and firend. I suggested /associated/ which would allow a
function to be a member, but it would only have access to public members of
its own class.
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #8
Jeff Schwab wrote:
Steven T. Hatton wrote:
This is a purely *hypothetical* question. That means, it's /pretend/,
CP. ;-)

If you were forced at gunpoint to put all your code in classes, rather
than in namespace scope (obviously classes themselves are an exception to
this), and 'bootstrap' your program by instantiating a single application
object in main(), would that place any limitations on what you could
accomplish
with your program? Are there any benefits to doing things that way
(other than survival)?

BTW, yes, I /do/ favor this approach.


I almost always have a single object to represent the whole application,
and use main() only as an entry point. That said, forcing _everything_
into classes seems to disallow use of some of my favorite C++ features
and programming styles. For example, many operator overloads belong
outside the class.

template< typename T >
int add_one( T const& t )
{
return 1 + t; // If T is a UDT, where is operator + defined?
}


I believe there is actually a technical limitation met when binary operators
(ones that take two arguments) are forced to be class members. I need to
review the section, but I believe there are some symmetries that are lost.
IOW A@B works but B@A doesn't for cases such as '+' and '*'. Another
problem is the fact that functions which were external to a class now gain
access to member data and potentially become bound to it. This is the
argument Stroustrup gives for having helper functions at namespace scope.

I actually proposed another access specifier in addition to private,
protected, public and firend. I suggested /associated/ which would allow a
function to be a member, but it would only have access to public members of
its own class.
--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #9
Mark A. Gibbs wrote:

I normally don't jump into hypothetical discussions like this, but parts
of this just rankled me.

Juha Kettunen wrote: [snip]
Personally, I can see no practical functional limitations or benefits.
I believe there actually are two. I already mentioned them in another post
in this thread.
Where this whole thing starts to spiral out of control is here. "C-code"
by itself is no slower or faster than the equivalent "C++-code" (ie,
class-based code). If you wanted to, you could write "slow" or "fast"
code in either. In fact, writing the theoretical "fastest" program is
possible in C and C++ - your compiler and your algorithm are what
determine how "fast" your code will run.
As you correctly pointed out below, virtual function calls have some
overhead beyond static calls. A static call to a member should be just as
fast as a global 'C'-style call, as far as I know.
But being told to put all your code into classes is as illogical to me
as being told to use only words and phrases from Shakepeaean iambic
pentameter as variable names. Or, more practically but no less obtuse,
hungarian notation. Does it help anything? Not really. Does it hurt
anything? Not really. And if it does either it does them in equal
measure. So why insist on it?
One argument would be for the sake of uniformity. But I guess that's just
the soldier in me talking.
int larger = Math::max(1, 2);

Only Java programmers could find the latter more legible.
There are advantages to having the static Math class in Java. It provides
for an easy point of reference. It's kind of like having a directory
structure and using it to organize your data. Personally, the following is
my idea of what a more complete set of mathematical operators looks like in
a programming language. But MMA is a specialized language:

http://documents.wolfram.com/v5/TheM...tax/A.2.7.html
Both can run just as fast (if you inline the former; the latter is
intrinsically inlined), so it's not a speed issue. Namespace Math::max
provides (essentially) the same amount of collision protection and
encapsulation as class Math::max, so it's not a maintennance issue.
Other than the fact that people tend to be sloppy with namespaces.
What it is, is a design issue. What you're telling me is that there are
such things as "Math" objects.
No. It's saying there are mathematical operators which can be classified as
such.
In order to find the max of two numbers,
I have to go get a "Math" and use the "Math"'s "max" operation. Or, in
the static case, that "Math"'s have a behaviour or mechanism that is not
specific to any one "Math", but is nonetheless "Math" object-specific
behaviour.
You don't instantiate Math. It is a utilities class.
On the other hand, the free function tells me that "max" is an operation
in category (ie, namespace) "Math". In other words, "max" is a "Math"
operation.
And how does this differ from Java's use of a class where you use a
namespace?
Doesn't the latter just make more sense?
It depends how it's managed. Java takes the functional operator approach to
mathematical syntax, whereas C++ allows for infix notation. I find that
far more significant in terms of how it 'feels' to use. An interesting
parallel to this is found in Misner, Thorne and Wheeler's _Gravitation_
where three notational forms are used in parallel through out the text:

http://www.reviewcentre.com/review57645.html
So in conclusion, what the hell are the benefits of such arbitrary
constraints? Write your program so that it makes logical sense and
models the problem it is trying to solve as closely as possible. If the
language has the tools, and if they don't lead to bad design and
maintennance problems, use them.


Actually, this was a hypothetical proposition. One reason for putting
everything in classes is that it usually makes refactoring easier (with the
possible exception of helper functions), and also makes reuse easier.

--
p->m == (*p).m == p[0].m
http://www.kdevelop.org
http://www.suse.com
http://www.mozilla.org
Jul 22 '05 #10

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

Similar topics

45
3632
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
0
9715
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
9595
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10099
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
9176
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...
0
6869
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
5536
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
5675
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3003
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.