473,842 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"All public methods should be virtual" - yes or no / pros & cons

I'm on a team building some class libraries to be used by many other
projects.

Some members of our team insist that "All public methods should be virtual"
just in case "anything needs to be changed". This is very much against my
instincts. Can anyone offer some solid design guidelines for me?

Thanks in advance....
Nov 15 '05
175 8932
di********@disc ussion.microsof t.com <di********@dis cussion.microso ft.com> wrote:
2004? When was C++ spec last updated ?
2003.
[...]


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 15 '05 #91
di********@disc ussion.microsof t.com <di********@dis cussion.microso ft.com> wrote:
And what language do you think will be used most in the CLI world and where
the jobs are :D C#
Ugh, that again.
For a sterter, what language do you think the
foundations of the CLI are written in?
[...]


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 15 '05 #92
Uhh what language do you think I code with C# ?
"Hendrik Schober" <Sp******@gmx.d e> wrote in message
news:eM******** ******@TK2MSFTN GP11.phx.gbl...
di********@disc ussion.microsof t.com <di********@dis cussion.microso ft.com>

wrote:
And what language do you think will be used most in the CLI world and where the jobs are :D C#


Ugh, that again.
For a sterter, what language do you think the
foundations of the CLI are written in?
[...]


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Nov 15 '05 #93
Martin Maat [EBL] wrote:
"Bronek Kozicki" <br**@rubikon.p l> wrote in message
news:rq******** *************** ******@40tude.n et...
[still crossposting and asking for crosspost]
Nah, let them read it here.
On Wed, 28 Jan 2004 00:21:47 +0100, Martin Maat [EBL] wrote:
I am not saying that polymorphism and events are equivalent, I am
saying that the example provided by Carl is a good example of
when you should not use polymorphism if you do have a more
natural alternative like events

Could you please provide example code in C#, with the same (or
better ;) ) functionality as Carl's example ? It would be
interesting to compare idiom your are proposing with the one
described by Carl.


Okay, we used a number of terms in the past posts ranging from
polymophism to events and delegates. What the purpose of Carl's
example boils down to is to that of a callback.


This is not true. The flow-control pattern that exists in my example is
similar to that of a callback. The purpose, however, is completely
different. The example you provided is one of a "client" hooking into the
behavior of an object. While a derived class is a "client" of it's base, it
is a very special kind of client. The idiom that I illustrated shows how a
base class (think interface, but in abstract terms, not C# terms) can be
constructed to ensure that ALL classes that implement that interface satisfy
the invariants (guarantees) that the interface designer intended. Delegates
cannot provide this guarantee, since there's no way to REQUIRE that a
derived class (or client) actually provide a delegate to one of it's methods
and hook it into the correct event of the base class.
In C# "delegates" are
used to implement both events and callbacks. One could argue that
events basically are callbacks, let's not get into that right now.


Delegates (and events) in C# are precisely (and solely) a systematic
mechanism for creating and invoking callbacks. Nothing more. That's not to
say they're not useful - they are quite useful. But they're not a mechanism
one would use to implement design by contract.

-cd
Nov 15 '05 #94
> > Well, they didn't have these fancy editors back then that enabled the
programmer to collaps the implemantation did they? So seperating interface and implementation was just practical [...]
If I hadn't been taught it before, the first time I came across an app

that had a couple 100kLOC, with many people designing and implementing
parts of it simultaniously, I know what separation of interface and
implementation is good for. IMHO that's why so many languages
have it.


No, don't mean a thing. What you are refering to is having separate
(implementation ) code files for different parts of a system. That is a good
thing. Implementation and interface though, beit for a single class or for
any code library, are logically one and the same. Seperating them physically
does not bring any gain as far as maintainability is concerned. You will
always lock both when working on either. Working on just the interface
without touching the implementation is pointless and if you are changing the
implementation you don't want anyone else to mess with the interface. If
anyone did want to mess with the interface he would most certainly want to
mess with the implementation as well.

Mind that the idea of .c and .h files stems from the pre-OO days. People
mainly extended that to .cpp and .h because they were used to working that
way. For class definitions there is no point in doing so as far as
maintainability is concerned..

With C# it is not only because there is no longer a practical need for
separation that everything is in one file now, it is a design decision based
on the notion that a class definition is a self-contained unit. Think of the
problems that would arise for the namespace system if it were different.

Martin.
Nov 15 '05 #95
> > No one is blaming anyone or anything. We just feel it is a good idea to
avoid language constructs that are easily misunderstood when designing a
language. [...]


Define "easily". Oh, and for whom? I have been teaching programming and
IMO every language is too hard to grasp for someone, and easily used
by others.


As a teacher of programming languages you will acknowledge that in C it is
relatively easy to write code that does something different than what you
intended yet compiles without any problem. In Pascal it is a lot harder to
do that, even in Basic it is a lot harder to do that. Just a few examples:

if (a = 3) { /* do whatever */ } // I mean "if a equals 3, do whatever"
but instead a is assigned the value 3 which evaluates to true.
MyFunction; // I want to execute the function but by forgetting the
parenthesis a function pointer is evaluated and dropped into a void instead.
Case fall-though is another nice one.

It is for good reason that in championships for writting the most obfuscated
one-liner the C language rules :-).

Martin.
Nov 15 '05 #96
Martin Maat [EBL] <du***@somewher e.nl> wrote:
Well, they didn't have these fancy editors back then that enabled the
programmer to collaps the implemantation did they? So seperating interface
and implementation was just practical [...]
If I hadn't been taught it before, the first time I came across an app that
had a couple 100kLOC, with many people designing and implementing
parts of it simultaniously, I know what separation of interface and
implementation is good for. IMHO that's why so many languages
have it.


No, don't mean a thing. What you are refering to is having separate
(implementation ) code files for different parts of a system.


No. What I am talking about it having the
interface separated from its implementation.
That is a good
thing. Implementation and interface though, beit for a single class or for
any code library, are logically one and the same. Seperating them physically
does not bring any gain as far as maintainability is concerned.
It does. It makes clients independand of
the implementation. They only depend on
the interface.
You will
always lock both when working on either.
I rarely ever change a header.
(I'd be dead within a week if I did change
interfaces half as often as I change their
implementations . People would be queueíng
at the table tennis room while their
machines are locked with the compiler
running and had a _lot_ of time to consider
what bad things to do to me. :o> )
Working on just the interface
without touching the implementation is pointless and if you are changing the
implementation you don't want anyone else to mess with the interface. If
anyone did want to mess with the interface he would most certainly want to
mess with the implementation as well.
Althoguh I think it is possible, it is
uncommon to lock files using CVS. I have
never done it and haven't heard from anyone
doing it here.
Mind that the idea of .c and .h files stems from the pre-OO days. People
mainly extended that to .cpp and .h because they were used to working that
way. For class definitions there is no point in doing so as far as
maintainability is concerned..
You're kidding, aren't you?
Suppose I have a class 'X', used in just
about every part of a big project. Now I
found a bug in 'X::f()' and need to fix
that. Why would I need to lock/change the
interface? 'X' was designed a long time
ago, its interface had a lot of time to
mature -- it is stable. I I change its
interface just so, everyone on the project
will be prevented from working for an hour
or two, just so. I wouldn't dream of doing
this without discussing the changes first.
All I need to do is fix 'X::f()'.
With C# it is not only because there is no longer a practical need for
separation that everything is in one file now, it is a design decision based
on the notion that a class definition is a self-contained unit. Think of the
problems that would arise for the namespace system if it were different.
I don't know C# and I don't know its
namespace system. What I know is this: If
I cause everybody here to recompile just
because I fix some implementation, I'd
be in serious trouble.
Martin.

Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 15 '05 #97
Martin Maat [EBL] <du***@somewher e.nl> wrote:
No one is blaming anyone or anything. We just feel it is a good idea to
avoid language constructs that are easily misunderstood when designing a
language. [...]
Define "easily". Oh, and for whom? I have been teaching programming and
IMO every language is too hard to grasp for someone, and easily used
by others.


As a teacher of programming languages you will acknowledge that in C it is
relatively easy to write code that does something different than what you
intended yet compiles without any problem. In Pascal it is a lot harder to
do that, even in Basic it is a lot harder to do that. [...]


Right. But first, we are talking C++ here
(stronger type checking, 'const', less use
for the preprocessor etc.). Second, while
it might still be relatively easy to write
some obfuscated code in C++, we were not
talking about "some code" here, but about
private virtual functions and whether they
are a language construct that is easily
misunderstood.
(Incidently, I taught the use of such just
last week. Nobody found them specifically
strange, nobody asked any question about
them, and in their homework they'll just
use it.)
[...]
It is for good reason that in championships for writting the most obfuscated
one-liner the C language rules :-).
I think with the operator overloading,
implicit conversions and all that stuff,
C++ would be a lot easier to write
obfuscated code if you wanted to.
Martin.


Schobi

--
Sp******@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
Nov 15 '05 #98
"Martin Maat [EBL]" <du***@somewher e.nl> wrote in message
news:10******** *****@corp.supe rnews.com...
No one is blaming anyone or anything. We just feel it is a good idea to avoid language constructs that are easily misunderstood when designing a language. [...]
Define "easily". Oh, and for whom? I have been teaching programming and IMO every language is too hard to grasp for someone, and easily used
by others.


As a teacher of programming languages you will acknowledge that in C it is
relatively easy to write code that does something different than what you
intended yet compiles without any problem. In Pascal it is a lot harder to
do that, even in Basic it is a lot harder to do that. Just a few examples:

if (a = 3) { /* do whatever */ } // I mean "if a equals 3, do whatever"
but instead a is assigned the value 3 which evaluates to true.
MyFunction; // I want to execute the function but by forgetting the
parenthesis a function pointer is evaluated and dropped into a void

instead. Case fall-though is another nice one.
A quality compiler will probably produce a warning that the expression
always evaluates to true (if a is a numeric type). Hopefully ending the
warning with 'Is this what you intended?' ;-). But as the quality of the
compilers increase, so should the quality of warnings. And since warnings
can be errors that is a good thing.

But:
int g(int x);
// ...
int x=3, a;
if( a = g(x) ) { ... }

Would quality compilers warn about that? Suppose you intended it?

Tom.
It is for good reason that in championships for writting the most obfuscated one-liner the C language rules :-).

Martin.

Nov 15 '05 #99
"Martin Maat [EBL]" <du***@somewher e.nl> wrote in message
news:10******** *****@corp.supe rnews.com...
No one is blaming anyone or anything. We just feel it is a good idea to avoid language constructs that are easily misunderstood when designing a language. [...]
Define "easily". Oh, and for whom? I have been teaching programming and IMO every language is too hard to grasp for someone, and easily used
by others.


As a teacher of programming languages you will acknowledge that in C it is
relatively easy to write code that does something different than what you
intended yet compiles without any problem. In Pascal it is a lot harder to
do that, even in Basic it is a lot harder to do that. Just a few examples:

if (a = 3) { /* do whatever */ } // I mean "if a equals 3, do whatever"
but instead a is assigned the value 3 which evaluates to true.
MyFunction; // I want to execute the function but by forgetting the
parenthesis a function pointer is evaluated and dropped into a void

instead. Case fall-though is another nice one.
A quality compiler will probably produce a warning that the expression
always evaluates to true (if a is a numeric type). Hopefully ending the
warning with 'Is this what you intended?' ;-). But as the quality of the
compilers increase, so should the quality of warnings. And since warnings
can be errors that is a good thing.

But:
int g(int x);
// ...
int x=3, a;
if( a = g(x) ) { ... }

Would quality compilers warn about that? Suppose you intended it?

Tom.
It is for good reason that in championships for writting the most obfuscated one-liner the C language rules :-).

Martin.

Nov 15 '05 #100

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

Similar topics

164
8069
by: Ken Brady | last post by:
I'm on a team building some class libraries to be used by many other projects. Some members of our team insist that "All public methods should be virtual" just in case "anything needs to be changed". This is very much against my instincts. Can anyone offer some solid design guidelines for me? Thanks in advance....
0
9870
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
9715
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
10610
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...
1
10671
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10310
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
9452
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
7035
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();...
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4088
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.