From: bugbear on
Joshua Cranmer wrote:
> On 02/25/2010 07:38 AM, Michael Tsang wrote:
>> Some languages have native support for property, e.g. C#, Python, PHP.
>> Although C++ doesn't, I always emulate them by defining helper classes
>> that
>> overload the copy constructor and the conversion operator. In Java,
>> there is
>> no operator overloading, how do I emulate them? If I can't do this, I
>> will
>> have trouble doing OO in Java.
>
> The standard way of implementing properties in Java is with get and set:
>
> public class Foobar {
> private String name;
>
> public String getName() { return name; }
> public void setName(String newName) { name = newName; }
> }
>
> Yes, it requires typing about 4 more characters, but otherwise, it would
> be pretty much the same.

Yeah, this:

http://java.sun.com/docs/books/tutorial/javabeans/properties/index.html

BugBear
From: Peter Duniho on
news.telesweet.net wrote:
> And In C# you are assuming that what one programmer uses the property
> idiom for is exactly the same as what you are assuming they are using
> it for. If they don't follow the MS convention that you seem to be
> prescribing to, then you can have exactly the same problem in C#.

Not true at all. There is only one "property idiom" in C#, and it's
defined by the language. It's not possible to write a property in C#
and fail to comply with the "convention". It's built into the language.

If you do something Java-like and write explicit "set" and "get" methods
in C#, then you aren't writing properties. It's one thing to call those
properties in a language like Java that doesn't actually have properties
as a feature. To call that properties in a language like C# is just dumb.

Pete
From: news.telesweet.net on
On 3/2/2010 12:35 PM, Peter Duniho wrote:
> news.telesweet.net wrote:
>> And In C# you are assuming that what one programmer uses the property
>> idiom for is exactly the same as what you are assuming they are using
>> it for. If they don't follow the MS convention that you seem to be
>> prescribing to, then you can have exactly the same problem in C#.
>
> Not true at all. There is only one "property idiom" in C#, and it's
> defined by the language. It's not possible to write a property in C# and
> fail to comply with the "convention". It's built into the language.
>

Of course it is possible. For example, after the following code, what
does x equal?

a.B = 5;
x = a.B;

With C# properties you have absolutely no idea. If you happen to know
that the person who programmed it followed the conventions for
properties, then you could probably say it was 5. If they didn't, you
have no idea, because C# properties are just shorthand for method calls.
Personally I find this extremely distasteful. It's as bad as operator
overloading in my opinion.

> If you do something Java-like and write explicit "set" and "get" methods
> in C#, then you aren't writing properties. It's one thing to call those
> properties in a language like Java that doesn't actually have properties
> as a feature. To call that properties in a language like C# is just dumb.

From: Peter Duniho on
news.telesweet.net wrote:
>> Not true at all. There is only one "property idiom" in C#, and it's
>> defined by the language. It's not possible to write a property in C# and
>> fail to comply with the "convention". It's built into the language.
>
> Of course it is possible. For example, after the following code, what
> does x equal?
>
> a.B = 5;
> x = a.B;

The answer to the question you asked is irrelevant. The question isn't
what a property _does_ but what it _is_.

It's true, a C# property getter and setter can do anything it wants.
But only a property getter and setter can be in a property, and the
property _is_ a property, regardless of the implementation.

> With C# properties you have absolutely no idea. If you happen to know
> that the person who programmed it followed the conventions for
> properties, then you could probably say it was 5. If they didn't, you
> have no idea, because C# properties are just shorthand for method calls.

No, that's not all they are.

Pete
From: Arved Sandstrom on
Peter Duniho wrote:
> news.telesweet.net wrote:
>> You are completely failing to see my point.
>
> I would say the exact same thing about you.
>
>> My point is that effective
>> use of C# properties relies on certain conventions being followed as
>> much as use of Javabeans properties relies on certain conventions being
>> followed.
>
> That has nothing to do with the question that has been discussed here.
>
>> Without those conventions, a C# property can do whatever the
>> hell it wants, and has the potential for creating far more obfuscated
>> code than the corresponding Java code.
>
> No. The code would be exactly as obfuscated a the corresponding Java
> code, except for the additional problem that Java has which is that you
> can't reliably know for sure what in a class is actually supposed to be
> a property and what's not.
[ SNIP ]

Just an aside observation about Java, and "properties": a lot of the
time, depending on what kind of Java programming you do, this question
simply never arises. For example, in my line of work the two main times
that I have to follow the getX/setX conventions have to do with JSF EL
and entity class accessors, but there is no thought here that these
methods are "properties" - they are simply accessors. So in the sense
that we have been talking about .NET properties, and why and how they
are useful, I for one don't encounter these use cases when using Java at
all.

This is anecdotal. YMMV. I'm only pointing it out to indicate that for
_me_ Java does *not* have the problem you mention, because in my use of
Java I do not require the "property" concept. It wouldn't actually even
add any value.

AHS