Re: Get/Set vs Public Variables



Am Mon, 4 Feb 2008 05:54:37 -0800 (PST) schrieb Jon Skeet [C# MVP]:

On Feb 4, 1:48 pm, JB <jamesb...@xxxxxxxxx> wrote:
I was always taught in C++ that using public variables was a bad idea,
which i could understand, so some generic get/set methods were usually
wrote to access the data.

It's exactly the same in C#. I don't really see how you can understand
why it's a good thing in C++, but not in C#.

First, more tahn 90% of properties I saw in my life are simple setters and
getters without any useful functionality.

Second, chances are really low that a construct like

class Person
{
string name;
string firstname;
}

EVER will need any functionality in getters and setters for the two string
fields.

So, why is it a bad idea to expose them directly to clients?

- dirty management for business objects? No. This requires to explicitely
call some function (IPropertyChanged) in the setter, only for that purpose.
There are better ways.

- Binding to external code? No. When someone ref-binds to a public variable
and this is later changed to a property, we must change the statement and
recompile. Big deal? IMO, no.

- Databinding etc? No. It is a conceptual lack of WinForms (and WPF)
databinding that it can bind only to properties and not to variables.
Technicaly, it is no problem to include variables in binding. But LINQ
alleviates most of that.

Of course, there are situations where this is sinply wrong. If I must
anticipate the situation that my implementation might change, I definitely
am better off with properties. But for > 90% of all classes with data
members this is NOT the case. And if I make a mistake here, the cost is low
- mostly recompilation, and some syntax errors. Definitely not worth to
make everything private.... - at least until auto-properties were invented.

Greetz
Paule



.



Relevant Pages

  • Re: why use the sealed ?
    ... > I'll want to add functionality to something at a later date. ... > I sortof wish you could add functionality to (or replace members of) ... > exceeds past the end of the string. ... > application to be a Remoting Client. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Predicting the Future and Kolmogorov Complexity
    ... to the source string), k scales with the string, thus destroying the ... excess of symmetry or a lack of it that strikes you as particularly ... The same thing is true of radio signals. ... What do I mean by levels of functionality? ...
    (talk.origins)
  • Re: basic_string causes crash in _vsnprintf???
    ... and any other output string sets used. ... want or revert back to the old stl. ... >> this functionality so while it was not a standard STL implementation ... > In STLport it's just luck that it "works". ...
    (microsoft.public.vc.stl)
  • Re: Alphabetizing a Comma Delimited String
    ... > client to put it back into a comma-delimited string. ... One *wonders* why this functionality is not in the server engine. ... This function was added in MySQL version 4.1. ... You can remove the separator altogether by specifying SEPARATOR "". ...
    (microsoft.public.sqlserver.programming)
  • Re: invoking a method
    ... Getters and Setters are used to encapsulate the ... to a string and add it to another string. ... the same as "inserting" it... ...
    (comp.lang.cobol)