Another C# critique

  • Thread starter christopher diggins
  • Start date
M

Magnus Lidbom

cody said:
Would you please let us know why exactly you think that const is useful in
C++ but not in C#? I personally don't see any difference that would affect
usefullness of const.


C++ makes a lot use of pointers (arrays, char*, pointers to objects) which
are not
selfcontained. everybody could alter a char* passed to a method, or even
delete[] it!
I could also pass the address to a local variable to a method as a pointer
and that
method could try to delete that pointer which will certainly crash your app.
I see big use for const with such types.

In C# you cannot delete something, additionally there is no such thing like
char* or
pointers (at least in safe mode)
Strings are immutable (immutability should be a property of the class, not
the object!).
Additionally since EVERY object is passed by reference (structs are very
rare in C#) you would have to precede almost every object variable/parameter
with a const if you
don't want to modify it. I don't want to write/maintain a such program!

You think it would mean more typing? Maintainability problems?
Let's compare the way you'd expose a piece of state, that must not be
modified, from your class in C# as is, and with const:

A commonly recommended workaround for not having const:

//Write a const interface, an interface exposing only members
//that don't modify an instance
public interface IConstState
{
int MyValue{get;}
}

//Have your state class implement the interface
public class State : IConstState
{
private int myValue;

public int MyValue
{
get{return myValue;}
set{myValue = value;}
}
}

//Have the containg class expose a property that returns the interface
public class MyClass
{
private State myState;

public IConstState MyState
{
get{return myState;}
}
}


With const:
1. The interface disappears
2. You write const four times:

//Correctly use const in your state class.
public class State
{
private int myValue;

public int MyValue
{
//Imaginary syntax saying that the value returned is const and the
//method is const(may be invoked on a const instance)
const get const{return myValue;}
set{myValue = value;}
}
}

//Have the containg class expose a property that returns a const instance.
public class MyClass
{
private State myState;

public State MyState
{
const get const{return myState;}
set{myState = value;}
}
}

So far it's not too big a difference. Const interfaces seem to work. With a
bit more typing, and a bit of obfuscation, but still. But now you find that
you need the ability to set MyState. What do you do? You can't use an
interface for assignment to your property. Properties cannot have different
types for get and set. Ok, so you need two properties, but that would be
very bad for legibility so you'll propably use two methods so you can keep
naming consistent:

public void SetState(State value)
{
myState=value;
}

public IConstState GetState()
{
return myState;
}

Ok, so far so good. It's sort of ugly, but hell it works. But now you
remember, darn I expose instances of MyClass that may not be modified. You
sigh deeply and code this:

public interface IConstMyClass
{
IConstState GetState();
}

and rework MyClass to have separate GetState and SetState methods.

What do you need to do if you have const to make this work? Nothing! The
const MyClass version already supports this. In all of this coding, if you
make a mistake in what you do with the interface workaround you end up with
instances being modified when they must not be. If you make the same mistake
with const your code will not compile.

So what have you?
1. Less code (35 lines agains 21 if you write it all out without the
comments using identical conding style):
2. Clearer code. (Consistent types that express intent.)
3. Safer code. (The compiler guarantees that you don't violate your
promises.)
4. Easier to maintain code(No need to maintain interfaces and very nearly
half the code disappears.)

Const is a bad thing? I don't think so.

/Magnus Lidbom
 
A

Andreas Huber

codymanix said:
You cannot inherit from an interface, but you can implement them.
An interface is just a contract, you do not inherit something from
them, neither methods nor fields. A lot of people confuse that
because the syntax is the same.

Having said that, I hope you understand now that I was never talking
about interfaces.

Well, given your original statement that was not clear at all. There are no
interfaces in C++ so one more often uses MI there...

Regards,

Andreas
 
A

Andreas Huber

cody wrote:
[snip]
C++ makes a lot use of pointers (arrays, char*, pointers to objects)
which are not
selfcontained.

Does a C# array qualify as not self-contained? If no, why not?

Moreover, people writing *modern* C++ rarely ever touch arrays, char* or
naked pointers directly. Instead, we use the standard containers, standard
strings, C++ references and smart pointers. *Despite this we use const
extensively.* I hope we can agree that the following data types in C++ and
C# are conceptually equivalent with regard to constness:
- C++ references and smart pointers vs. C# references
- The C++ standard containers (list, vector, deque, set, map, etc.) vs. C#
collections
everybody could alter a char* passed to a method, or
even delete[] it!

Well, in C# I can pass an array to a method which then happily modifies it,
can't I? Why is this situation different from the one you describe above?
I could also pass the address to a local variable to a method as a
pointer and that
method could try to delete that pointer which will certainly crash
your app. I see big use for const with such types.

Something being const won't stop you from deleting it so this argument is
moot.
In C# you cannot delete something, additionally there is no such
thing like char* or
pointers (at least in safe mode)
Strings are immutable (immutability should be a property of the
class, not the object!).

Why is that? Sometimes strings need to be modified and sometimes you don't
have to modify them. Both scenarios can easily and very expressively be
implemented in C++ by making the right data structures and/or functions
const. The compiler then checks for consistency. C#'s model of string
constness is therefore much less flexible than the one of C++. As I
mentioned before, the immutability model often leads to excessive copying.
Additionally since EVERY object is passed by reference (structs are
very rare in C#) you would have to precede almost every object
variable/parameter with a const if you
don't want to modify it.

Yes. A very effective way of documenting your intentions with passed
arguments...
I don't want to write/maintain a such
program!

Why?

Regards,

Andreas
 
C

cody

C++ makes a lot use of pointers (arrays, char*, pointers to objects)
Does a C# array qualify as not self-contained? If no, why not?


An array ios like all generic containers not selfcontained neither in C++
nor in C#.
There must be a way to protect such objects from mofification. either
through
wrappers or keywords.
But for all normal objects no const stuff is required imho.
 
C

cody

You think it would mean more typing? Maintainability problems?
Let's compare the way you'd expose a piece of state, that must not be
modified, from your class in C# as is, and with const:


IMHO it is nonsense to protect a selfcontained object from modification.
Please give me one example where you would actually need a object
that must be passed as const. please proof that modification to this object
would
bring your app in trouble.
Please choose not a generic collection (array,list,matrix) but a specific
real use object.
 
M

Magnus Lidbom

cody said:
IMHO it is nonsense to protect a selfcontained object from modification.

I'd really like to hear your definition of selfcontained and your argument
as to why this definition renders const obsolete. Noone will be happier than
I if you're right.

Meanwhile, in the world as I know it today:
Unless you can pass a const version around you must copy every single object
passed as an argument if you don't want it modified.

MyClass myObject = new MyClass ();
SomeClass.DoSomething(myObject );

After this call you don't know what state myObject is in anymore. If you
need to know that it's state is not modified you must pass a copy instead of
myObject. Which means a degradation of performance and more code since
MyClass must implement ICloneable. Most likely, either DoSomething shouldn't
change it's argument, or myObject shouldn't be passed if it musn't be
modified, but without const or a workaround you cannot verify that this is
the case. I believe my last post gave a convincing argument for const over
the the workarounds.
Please give me one example where you would actually need a object
that must be passed as const.

One common scenario with a truly compelling need for const I've encountered
often is when you need to expose application-wide state to subsystems.

Examples:

1. Any subsystem should be able to show a user which permissions he has for
an object. No subsystem but the security subsystem should be able to change
it. Exposing a const version of the data is a simple solution.
(Note that this is not intended to be safe against a malicious programer
only to protect against mistakes.)

2. Inside an Object Relational mapping framework objects containing
information about how classes and fields are mapped to tables and columns
are constantly passed around. None of this information may ever be changed
by the client or chaos ensues.

3. An application caches often read data instead of reading it from the
database. Content templates, security restriction etc is kept in the cache.
None of this may ever be changed by a client.

This is but a few real world examples of one common scenario that I
personally have dealt with.
please proof that modification to this object
would
bring your app in trouble.
What do you think would happen if any of the objects mentioned above were
changed?
Please choose not a generic collection (array,list,matrix) but a specific
real use object.
Those are not real use objects? On what grounds do you feel they should be
excluded from the discussion?

/Magnus Lidbom
 
R

Roy Fine

cody - see inline.

cody said:
Erm..Please tell me your definition of Flexibility. Iam not sure what you
meant.

Sure you do -- you are just standing firm, defending a bad statement (that's
OK, we all do it, you just got caught) C# runs in a controlled environment,
and to some extent, so does C++. It is a matter of fact that the C#
environment is more controlling, ergo, the C++ environment is less
controlling. Less controlling may be a better surrogate for flexibility. I
suspect that C# is not first and foremost a replacement for C++, but rather
an alternative or upgrade for classic VB (and I suspect that upgrade is not
the best word for the meaning that I had in mind).

Consider this construct --

/* **************************** */
bool someBool = false;
int ival = 220;
/* statement a*/
if((someBool == true) && (ival++ >= 100)){...}

/* statement b*/
if((someBool == true) & (ival++ >= 100)) {...}

/* **************************** */

While the major complaint with C++ may oft times be the uncontrolling and
unforgiving way pointers are handled (by programmers and by the language and
by the runtime), it is the arcane idiosyncrasies(note - one person's
idiosyncrasy is another's "must have feature") like above that lead to hard
to debug and even harder to maintain. While statement "a" and "b" in above
seem to generate the same results, it is the issue of the "compound test
short-circuit" and "expression side-effects" that get so many in trouble.
And while it is good syntax, it's quite bad form -- what if you really
wanted the value of ival to increment anfter the test if and only if
someBool is true? What if you didn't????

The if expression in statement "a" is seen as two statements(where the
second is evaluated only if necessary). The if expression in statement "b"
is seen as one statement.

To this end, C# is no better than C++ -- the above syntax is valid in MC++,
classic C++ (version 7 of the C++ compiler) and in C# -- and the compiler
warning messages are the same - NONE, and the results are the same....

This is but an simple example of why I thought your
definition/interpretation of Flexibility was uncalled for. The above is
clearly not stupid, the problem is certainly subtle, and the compiler is
quite content.

I would be hesitant to call this a design flaw of the language, rather more
inclined to see it as an unfortunate side effect. But there are many of
these that can ruin an over-hyped demo of a program that you have been
working months on and is the basis of not just the next salary raise, but
continue employment - and one gets better at this by understanding the
language.

regards
roy fine



I don't want to pile on here - but computing will likely change so
dramatically in the next 5 years, that what happens in 10 years will hardly
be recognized as computing by those that are doing it now...

Hm. Very hard to tell, but lots of stuff have to be backward compatible
and consider the very huge codebase written in C++.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
 
C

cody

Meanwhile, in the world as I know it today:
Unless you can pass a const version around you must copy every single object
passed as an argument if you don't want it modified.

MyClass myObject = new MyClass ();
SomeClass.DoSomething(myObject );

After this call you don't know what state myObject is in anymore.

Does that matter? Selfcontained object should never fear modification.
One common scenario with a truly compelling need for const I've encountered
often is when you need to expose application-wide state to subsystems.

Examples:

1. Any subsystem should be able to show a user which permissions he has for
an object. No subsystem but the security subsystem should be able to change
it. Exposing a const version of the data is a simple solution.
(Note that this is not intended to be safe against a malicious programer
only to protect against mistakes.)

And what if you forget const? You do not believe yourself that you
"accidently" set a permission in a method.
Why not making 2 different classes: 1 to set the permissions and 1 to get
them.
2. Inside an Object Relational mapping framework objects containing
information about how classes and fields are mapped to tables and columns
are constantly passed around. None of this information may ever be changed
by the client or chaos ensues.

So why don't you make that fields readonly?
3. An application caches often read data instead of reading it from the
database. Content templates, security restriction etc is kept in the cache.
None of this may ever be changed by a client.

Make it private?
Those are not real use objects? On what grounds do you feel they should be
excluded from the discussion?

They are not selfcontained, so they need protection, either by const,
through a wrapper or through a copy.
 
M

Magnus Lidbom

cody said:
Does that matter? Selfcontained object should never fear modification.
Still waiting for this revolutionary definition of selfcontained that
apparently means any code may do anything to any object and everyting will
still work fine.

Until this definition materializes: Yes it matters. It matters a whole lot.
If you can't guarantee the state of the objects you're working with you
cannot guarantee anything about the functioning of the system.
And what if you forget const? You do not believe yourself that you
"accidently" set a permission in a method.
Errors are always possible. Const is one way to help minimize them.
What was your point?
Why not making 2 different classes: 1 to set the permissions and 1 to get
them.
Duplicate an entire class hierarchy? An entire security subsystem?
That´s better than writing const where appropriate?
So why don't you make that fields readonly?
Because many of them are not known when the constructor is called, and many
are collections for which readonly is not meaningful.
Make it private?
So how will the client read it? How will the subsystem that manages the
cache set up the data?
They are not selfcontained, so they need protection, either by const,
through a wrapper or through a copy.
All collections need const?
Did you have an actual argument against const?

Bear in mind, blanket assertions are not arguments.

/Magnus Lidbom
 
O

ozbear

An array ios like all generic containers not selfcontained neither in C++
nor in C#.
There must be a way to protect such objects from mofification. either
through
wrappers or keywords.
But for all normal objects no const stuff is required imho.


Cody....
As usual, I suggest you quit while you are behind.

Oz
 
F

Frans Bouma

And what if you forget const? You do not believe yourself that you
"accidently" set a permission in a method.

"What if you forget <abc>... " are non-arguments. If a programmer
forgets to include something, it's a bug. NOT wanting to have a feature
because you can also forget something is silly. What if you forgot to
mention 'ref'... ? still it's available.
Why not making 2 different classes: 1 to set the permissions and 1 to
get them.

Why is it so hard for some people to accept that a given feature
CAN be helpful to SOME people? Why o why do I have to read stupid (yes,
stupid) arguments like "You can also create extra classes to do the same
thing" to counter a feature request over and over again? YOu can also
program C++ in C, just type a lot more code. You don't do it, for obvious
reasons. Why is there a switch statement in C# while if() {} can do it
also. Why is there a while statement in C# while 'for' can do it too and
more compact. Silly things too. They had reasons to include these
(although I still doubt why a while is included) but apparently had
reasons to not include const. I tell you one reason: it was too much work.

having const in C# is a very very nice thing to have. I too have
trouble keeping data readonly and because in my tool it is so incredibly
complex to do that, I decided some time ago to just leave it and not
provide a read-only copy, because it would otherwise result in
implementations of IClonable all over the place. (after all, .NET doesn't
support a deep copy feature)
So why don't you make that fields readonly?

You can't do that.
These fields have to be set somewhere by code. Up till today you
can't specify accessor operators in interface definitions, you also can't
define get public and set internal for properties. This means that this
kind of information is changeable.

The example is not that good though. At runtime it's sometimes
handy to be able to change persistence information (if that information is
stored inside the entity that is!) on the fly, for example with dynamic
tables some people use, catalogs/tables chosen on the fly etc.

There are other examples however which suffer from the lack of
const. For example I expose the complete project tree of objects to my
generator framework. This framework uses plugins ala NAnt. I can't make my
project read-only. Normally you don;t change stuff in the project but I
also can't prevent it, or I have to implement IClonable on every class and
that's a hell of a job to do, solely to keep stuff read-only in some
situations. Silly!

FB
 
A

Andreas Huber

cody said:
An array ios like all generic containers not selfcontained neither in C++
nor in C#.
There must be a way to protect such objects from mofification. either
through
wrappers or keywords.
But for all normal objects no const stuff is required imho.

Yes, you have already expressed that opinion. However, you have so far
not defined what exactly a self-contained object is. Enumerating
classes that qualify vs. ones that do not qualify is not enough. We
need clear-cut rules how to identify such classes.
Neither have you said why you think that one must have the ability to
protect a non-self-contained object from modification while
self-contained ones do not need such protection.
I don't think that further discussion will yield any result as long as
you don't clarify these points.

I may be wrong but my impression is that self-containedness is a
rather foggy property. From what you have said so far it somehow seems
to depend on how open a class' interface is. Yes, I see that some
classes do hide their implementation more than others. But why are
users affected by this fact? Users *always* only see the interface,
they should never assume anything about the implementation. Doing so
would be a good recipe for disaster.
Self-containedness somehow seems to depend on the "differences"
between interface and implementation so it is possible that two
different implementations of the same interface also differ in
self-containedness. This strikes me as rather odd because const
correctness is an interface-only thing.

Regards,

Andreas
 
M

Magnus Lidbom

Frans Bouma said:
You can't do that.
These fields have to be set somewhere by code. Up till today you
can't specify accessor operators in interface definitions, you also can't
define get public and set internal for properties. This means that this
kind of information is changeable.

The example is not that good though. At runtime it's sometimes
handy to be able to change persistence information (if that information is
stored inside the entity that is!)
In this case the mapping information has a one to one relationship to the
type of the object that is mapped. Duplicating the mapping information for
each entity would mean huge memory consumption and very bad performance. Is
there such a thing as an object relational mapping framework that duplicates
mapping information for each entity?
on the fly, for example with dynamic
tables some people use, catalogs/tables chosen on the fly etc.
This framework does not support such functionality at the moment. It might
eventually if a compelling need is demonstrated. However, I see no need to
duplicate the mapping information for each entity in order to implement such
functionality. A single new mapping per scenario should suffice.

<snip>

/Magnus Lidbom
 
C

codymanix

3. An application caches often read data instead of reading it from
the
So how will the client read it? How will the subsystem that manages the
cache set up the data?

So provide only getter-methods. The method that read from the Database
should be private.
Did you have an actual argument against const?

Yes. Since EVERY parameter / variable in C# (rarely used structs excluded)
are passed by
reference and providing a copy is not always possible, you have to precede
nearly every
parameter / variable that don't need/should not be modified with const.

Should you ever change a const method into a mutating method you have to go
through
the whole application and refactor the whole app. I call this the const-hell

That was bad in C++ and will be even worse in C# due to the reason I gave at
the top.

In C++ const was useful since there were always char* and other pointer
passed around
unprotected and you had no control over who and when something was modified.

In C#, strings are immutable and all collections provide readonly wrappers.

The C# language designers are not stupid peaple. they considered using const
and came to
the conclusing that is is not suitable for C#.

These are similar reasons as the fact that checked exceptions (like in Java)
were dropped.
Overall development productivity.
 
C

codymanix

An array is like all generic containers not selfcontained neither in C++
Yes, you have already expressed that opinion. However, you have so far
not defined what exactly a self-contained object is. Enumerating
classes that qualify vs. ones that do not qualify is not enough. We
need clear-cut rules how to identify such classes.
Neither have you said why you think that one must have the ability to
protect a non-self-contained object from modification while
self-contained ones do not need such protection.
I don't think that further discussion will yield any result as long as
you don't clarify these points.

Normal objects will be programmed in a way that they will always have a
consistant, well-defined state.
They protect theirselven either through their interface or through thrown
exceptions if a modification is tried
that would result in inconsistent or undefined state.

Generic collections does not have that luxeriority. they are not for a
specific use or context,
they don't even now what types of objects they will hold at a later time.

Providing the ability for generic collections to be readonly makes them
selfcontained, that's all.
At the moment there is the only way to make a collection selfcontained a
wrapper, which is not very efficient.
Self-containedness somehow seems to depend on the "differences"
between interface and implementation so it is possible that two
different implementations of the same interface also differ in
self-containedness. This strikes me as rather odd because const
correctness is an interface-only thing.

How's that?
 
J

Jon Skeet [C# MVP]

codymanix said:
Yes. Since EVERY parameter / variable in C# (rarely used structs excluded)
are passed by reference

No they are not. They are passed by value. The value passed, however,
is a reference. That's an entirely different matter.

Please see http://www.pobox.com/~skeet/csharp/parameters.html

The C# language designers are not stupid peaple. they considered using const
and came to the conclusing that is is not suitable for C#.

I don't think that's necessarily the reason - they may well have
basically run out of time, just like they did for generics.

I wouldn't be at all surprised to see const correctness in a future
version of C#.
 
A

Andreas Huber

codymanix said:
Normal objects will be programmed in a way that they will always have
a consistant, well-defined state.

This is true for all objects, no matter whether they are normal classes,
arrays or collections, is it not?
They protect theirselven either through their interface or through
thrown exceptions if a modification is tried
that would result in inconsistent or undefined state.

The same here. E.g. an array throws an exception if you try to access an
element past the last one.
Generic collections does not have that luxeriority. they are not for a
specific use or context,
they don't even now what types of objects they will hold at a later
time.

Not true. Generic collections and arrays throw exceptions in certain
situations. So they do enforce their "well-defined state" in certain
situations, don't they?
Providing the ability for generic collections to be readonly makes
them selfcontained, that's all.
At the moment there is the only way to make a collection
selfcontained a wrapper, which is not very efficient.


How's that?

I assume you want to know why const correctness is an interface-only thing.
Short answer: the C++ keyword mutable. Long answer: Sometimes classes have
data members that need to be modified even when you call observer functions.
E.g. you have the following class:

class Polygon
{
public:
// other members

double Circumference() const
{
if ( dirty_ )
{
circumference_ = /* expensive calculation */;
dirty_ = false:
}

return circumference_;
}

private:
mutable double circumference_;
mutable bool dirty_;
};

Lets assume Circumference() is an expensive calculation you only want to
make when you absolutely have to. Logically, Circumference() must be const
as it does not modify the object visibly. However, for performance reasons
you need to cache the result in a member variable. In such situations you
can declare the involved members mutable, what allows you to modify them
even in const functions. So, const only concerns the interface because an
implementer always has complete freedom to modify object state in a const
function. However, he must always say so explicitly by declaring mutable all
data members he wants to modify in a const function.

Regards,

Andreas
 
D

Daniel O'Connell [C# MVP]

Jon Skeet said:
No they are not. They are passed by value. The value passed, however,
is a reference. That's an entirely different matter.

Please see http://www.pobox.com/~skeet/csharp/parameters.html



I don't think that's necessarily the reason - they may well have
basically run out of time, just like they did for generics.

I wouldn't be at all surprised to see const correctness in a future
version of C#.
While it would be a noble feature, as has been expressed several times by
various MS employees(I can probably still find references if you want 'em)
because .NET & C# didn't have const in the beginning, the vast amount of
existing code(the framework most notably) that has no concept of const may
make it as much of a silly issue as it is in C++(cast it away easily, etc).
Realistically, adding const to C# really would mean adding it to the BCL or
its usage would be *heavily* marginalized. Just as adding optional exception
specs would effectivly do no good because a very large portion of
functionality doesn't have them, its an issue that will have to be
addressed.
 
M

Magnus Lidbom

Daniel O'Connell said:
While it would be a noble feature, as has been expressed several times by
various MS employees(I can probably still find references if you want 'em)
because .NET & C# didn't have const in the beginning, the vast amount of
existing code(the framework most notably) that has no concept of const may
make it as much of a silly issue as it is in C++(cast it away easily,
etc).

I don't feel const in C++ is silly.
Casting away const is possible, but a competent programmer will do so only
as a last resort. Casting away const on an object that is actually declared
const, not just a reference to const to a non const object, is undefined
behavior. The implementation is allowed to do anything without you having a
right to complain when you write code with undefined behavior. Given that if
you have a need to cast away const you probably wont _know_ wether the
actual object is declared const.....

In C++ const is a statement that the object may not be modified, not a
guarantee that it won't be. For a certain definition of better, i't might be
possible to do better in a managed environment. See below.
Realistically, adding const to C# really would mean adding it to the BCL or
its usage would be *heavily* marginalized. Just as adding optional exception
specs would effectivly do no good because a very large portion of
functionality doesn't have them, its an issue that will have to be
addressed.
I don't feel that having code around which doesn't utilize the safety net
makes the net any less useful to those that do use it. There's no security
feature or good design practice that is used always in all code. I don't
believe that is any reason not to use them all in your own code, or that it
takes anything away from the gain you, your coworker and clients receive
from using it. It does however mean that const in C# would have to be like
in C++, possible to cast away. In a managed environment however, it might be
possible to have an exception automatically thrown if an attempt is made to
modified a non mutable field.

As for the BCL I'd certainly expect it to use const correct code if const
was introduced. Yes, it would be quite a lot of work and possibly have to be
staggered over a few releases.

Regards /Magnus Lidbom
 
M

Magnus Lidbom

codymanix said:
So provide only getter-methods. The method that read from the Database
should be private.
The same question still applies: How will the subsystem that manages the
cache set up the data?
Do you feel that an XML document object should have it's own data access
layer?
Yes. Since EVERY parameter / variable in C# (rarely used structs excluded)
are passed by
reference
Jon already corrected you here.
and providing a copy is not always possible, you have to precede
nearly every
parameter / variable that don't need/should not be modified with const.

Should you ever change a const method into a mutating method you have to go
through
the whole application and refactor the whole app. I call this the const-hell

That was bad in C++ and will be even worse in C# due to the reason I gave at
the top.
You don't need to specify that the method takes a const parameter at the
call site. In fact you can't. You speak as if you remember "hell" in C++,
but what you say only demonstrates that you don't even know how const in C++
works. Changing your method to modify it's argument will cause problems only
if it is passed arguments that are const. And that is exactly the point to
having const. Not modifying instances that may not be modified. That is a
boon, not a problem.
In C++ const was useful since there were always char* and other pointer
passed around
unprotected and you had no control over who and when something was
modified.
An how are references any different?
In C#, strings are immutable and all collections provide readonly
wrappers.
Yes, there are cases where the need for const was so pressing that
the long way around of writing entire extra classes to do the job was
chosen since const isn't around. And this still, after all that work,
results in runtime errors instead of compile time errors, since the type
system cannot tell the purpose of the wrapper. And there is no standard way
of doing this, or of obtaining the readonly wrapper. The result: more work,
more reading docs, more confusing code, more time necessary for
maintenance, more bugs.

How does this mitigate the need for const?

/Magnus Lidbom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

CSharp Coding Standards 18

Top