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

Home Posts Topics Members FAQ

Another C# critique

I have posted a C# critique at
http://www.heron-language.com/c-sharp-critique.html. To summarize I bring up
the following issues :

- unsafe code
- attributes
- garbage collection
- non-deterministic destructors
- Objects can't exist on the stack
- Type / Reference Types
- Boxing / Unboxing
- Mutability
- Classes as Modules and Programs
- Polymorphism through Class Inheritance
- Interface Delegation
- Interface Extensions
- C# Missing Template Template Parameters
- Late Added Generics
- Source File Layout
- Public Fields
- Special Primitives
- Is it a Property or is it a field?
- Microsoft

I am perhaps guilty somewhat of being a flame baiting troll, but at the same
time I honestly want to improve on the critique.

Thanks in advance for your responses.

--
Christopher Diggins
yet another language designer
http://www.heron-language.com
Nov 15 '05
188 7277
> > When you think Flexibility=="I can do every stupid thing I want, the
compiler won't hinder me" then yes, C++ is more flexible.
That is a child-like, absurd interpretation of Flexibility. Please
reconsider.


Erm..Please tell me your definition of Flexibility. Iam not sure what you
meant.
I don't doubt this. C and C++ will be still popular in 10 years and still used in 30 years.


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
Nov 15 '05 #121
> Consider e.g., the CDC class in MFC that wraps the Windows device context.
CDC has an internal state that includes knowledge of all GDI objects
currently selected into the device context. The class is self-contained in
the sense that you cannot access the state directly (unless you access the
underlying device context handle but that is beside the point).

The class has a bunch of const methods, e.g., void DPtoLP(LPRECT lpRect)
const. The constness means that the user of the class can safely call the
member function and not have to worry whether the object's internal state
changes because of the call. This is a big deal.

If the method was not const, you'd have to pour over the documentation to
see whether the method changes the internal state. If the documentation is
vague on this point, you might end up saving the object's state prior to the call and restoring it afterwards, just to be on the safe side. This would be a major inconvenience and also a performance hit.

It doesn't bother me if a method puts a Pixel on that DC, it will have its
good reasons.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #122
> >Normally you don't have to care wheather an object is changed or not
since
all objects should be self-contained


Of course you must care! Whether an object is changed or not is crucially
important - the fact that an object is self-contained is completely
non-sequitor! You appear not to really understand the issues.

Maybe, so explain it please.
C++ seems (In my knowledge) to be the only language with such a feature
which
also means it is not that important as C++ programmers are telling, you are
just used to it.
Iam programming for a long time now and I never missed this feature. It was
useful in C++
but has no great use in other languages.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #123
cody wrote:
Normally you don't have to care wheather an object is changed or
not since all objects should be self-contained


Of course you must care! Whether an object is changed or not is
crucially important - the fact that an object is self-contained is
completely non-sequitor! You appear not to really understand the
issues.

Maybe, so explain it please.
C++ seems (In my knowledge) to be the only language with such a
feature which
also means it is not that important as C++ programmers are telling,
you are just used to it.
Iam programming for a long time now and I never missed this feature.
It was useful in C++
but has no great use in other languages.


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.

Regards,

Andreas

Nov 15 '05 #124
> > The base classes in acyclic visitor are abstract (no implementation) ,
and therefore, they can be expressed as interfaces. While C# does not
support multiple inheritance of classes, it does support multiple
inheritance of interfaces which makes implementing acyclic visitor in
C# trivial (see below).


Yep, I know. I just posted this because the OP made yet another unfounded
blanket statement:

<quote>
Multiple Inheritance is just bad style used by poor
programmers.
All Multiple Inheritance scenarios are best solved using composition
instead.
</quote>

Note that he isn't distinguishing between classes and interfaces...


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.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #125
> >>> Normally you don't have to care wheather an object is changed or
not since all objects should be self-contained

Of course you must care! Whether an object is changed or not is
crucially important - the fact that an object is self-contained is
completely non-sequitor! You appear not to really understand the
issues.


Maybe, so explain it please.
C++ seems (In my knowledge) to be the only language with such a
feature which
also means it is not that important as C++ programmers are telling,
you are just used to it.
Iam programming for a long time now and I never missed this feature.
It was useful in C++
but has no great use in other languages.


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!

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #126

"cody" <do************ *********@gmx.d e> wrote in message
news:bv******** ****@ID-176797.news.uni-berlin.de...
>> Normally you don't have to care wheather an object is changed or
>> not since all objects should be self-contained
>
> Of course you must care! Whether an object is changed or not is
> crucially important - the fact that an object is self-contained is
> completely non-sequitor! You appear not to really understand the
> issues.

Maybe, so explain it please.
C++ seems (In my knowledge) to be the only language with such a
feature which
also means it is not that important as C++ programmers are telling,
you are just used to it.
Iam programming for a long time now and I never missed this feature.
It was useful in C++
but has no great use in other languages.
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






Nov 15 '05 #127
codymanix wrote:
The base classes in acyclic visitor are abstract (no
implementation) , and therefore, they can be expressed as
interfaces. While C# does not support multiple inheritance of
classes, it does support multiple inheritance of interfaces which
makes implementing acyclic visitor in C# trivial (see below).


Yep, I know. I just posted this because the OP made yet another
unfounded blanket statement:

<quote>
Multiple Inheritance is just bad style used by poor
programmers.
All Multiple Inheritance scenarios are best solved using composition
instead.
</quote>

Note that he isn't distinguishing between classes and interfaces...


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

Nov 15 '05 #128
cody wrote:
[snip]
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.


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

Nov 15 '05 #129
> > 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?

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

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Nov 15 '05 #130

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

Similar topics

14
3749
by: Long | last post by:
How to include an HTML doc in another HTML doc Problem: to insert the body section of an HTML doc in another HTML document at a specific location. One possible way is to add a WebCharm tag like this: <%@charm:html 20 0 my_services.htm %> When the HTML template is processed by a WebCharm-aware web server, the
19
2561
by: TC | last post by:
Are there any good sites or forums for a web critique? I went to alt.html.critique and it's pretty dead.
9
2290
by: bowsayge | last post by:
Inspired by fb, Bowsayge decided to write a decimal integer to binary string converter. Perhaps some of the experienced C programmers here can critique it. It allocates probably way too much memory, but it should certainly handle 64-bit cpus :) #include <stdio.h> #include <stdlib.h> char * to_binary (unsigned long value) {
39
1947
by: Eric | last post by:
There is a VB.NET critique on the following page: http://www.vb7-critique.741.com/ for those who are interested. Feel free to take a look and share your thoughts. Cheers, Eric. Ps: for those on comp.programming, this may be off topic, but I've posted there because the critique was part of a discussion in that group.
0
10940
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...
1
10670
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
10308
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
9451
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
7854
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
7030
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
5695
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
5882
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3141
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.