Proposal: Renaming of the "private" Keyword

C

C# Learner

A recent discussion in here has got me reflecting on the "private"
keyword. As I mentioned in the discussion, I find the keyword
problematic, in that it looks too similar to "public" and therefore
makes quickly scanning for public members among private ones, hard.

This kind of thing wasn't so important in a language like C++ or Delphi,
where class member declarations were separate. However, since C#
members aren't declared ahead - but defined in place - I feel this is an
issue in this language.

Consider the following code:

class Test
{
public void Foo()
{
Blah();
}

private int Blah()
{
Bleh();
return 0;
}

private SomeType Bleh()
{
Foobar();
return new SomeType();
}

public string Blow()
{
Fewbar();
return String.Empty;
}

private void Fewbar()
{
F00Bar();
}

private bool F00Bar()
{
Bl4h();
return true;
}
}

In the above, can you, while quickly scanning, indicate which methods
are public?

I know I can't. I'd imagine others would agree.

"private" is too similar to "public".

It could be renamed to, for example, "concealed".

Here's the above code edited to use "concealed" instead of "private".

class Test
{
public void Foo()
{
Blah();
}

concealed int Blah()
{
Bleh();
return 0;
}

concealed SomeType Bleh()
{
Foobar();
return new SomeType();
}

public string Blow()
{
Fewbar();
return String.Empty;
}

concealed void Fewbar()
{
F00Bar();
}

concealed bool F00Bar()
{
Bl4h();
return true;
}
}

In my opinion, it is an order of magnitude easier to tell which are the
public methods in the above.

Whadda ya think?
 
P

Peter Rilling

"private" is probably too engrained in the developers' mind and our legacy
to be changed.

There is nothing that says you cannot organize your code by grouping like
members.
 
C

C# Learner

Peter Rilling wrote:

Hi Peter,

Thanks for the reply.
"private" is probably too engrained in the developers' mind and our legacy
to be changed.

I understand that. I feel it's also engrained in my mind, but, still,
I'm open for change. I'd accept this change to "private" for the
benefit it'd give, as detailed in the original post.
There is nothing that says you cannot organize your code by grouping like
members.

This makes C# code harder to read, in my opinion. I prefer to see
private methods that are called by public methods directly under the
public methods, and private methods called by those private methods
directly under *them*, and so on.

I've seen other C# code that uses this structure, too, so it isn't just
me :)

I'd be interested in others' feedback too.

Regards
 
M

mikeb

C# Learner said:
A recent discussion in here has got me reflecting on the "private"
keyword. As I mentioned in the discussion, I find the keyword
problematic, in that it looks too similar to "public" and therefore
makes quickly scanning for public members among private ones, hard.

This kind of thing wasn't so important in a language like C++ or Delphi,
where class member declarations were separate. However, since C#
members aren't declared ahead - but defined in place - I feel this is an
issue in this language.

Consider the following code:

...snip...


In my opinion, it is an order of magnitude easier to tell which are the
public methods in the above.

Whadda ya think?

I think that regardless of whether or not its a good idea, it's not
enough of a benefit to get into the language.

There are 2 major roadblocks:

1) adding a new keyword to the language is something the designers
are very reluctant to do. by definition, it's a breaking change since
any new keyword you come up with could be used as an identifier by
existing code.

2) to keep compatibility with existing code, the language would still
have to support the 'private' keyword. So everyone used to using
private would almost certainly continue using it.

To overcome these problems, the benefit would have to be clear.
 
D

Doug Forster

As well as what Mike says IMO the direct text structure of code is pretty
much irrelevant with todays sophicated IDE's. I mostly navigate around a
class using the various navigation tools rather than looking directly at the
code. The access status is as clear as a bell in the class explorer and you
can choose to display by access if you wish.

Cheers

Doug Forster
 
C

Chris A. R.

C# Learner,

The difference between the two words is quite clear to me, but then, I
also write fiction for fun as a sideline, so maybe my ability to discern
differences in words has been sharpened over the years.

Chris A.R.
 
R

Roy Fine

I do hope that the standards people don't change the language every time
someone comes up with a 'neat' little twist.

This is a programming language - learn it, master it, and move on. It ain't
yours to change! If you were allowed to inject your changes, then you must
allow others, and so on. Then the problem you have with 'not liking the
name of private' becomes trivial in comparison.

roy fine
 
M

Michael Giagnocavo [MVP]

If you have a hard time seeing the difference between private and public,
then get a better IDE. If VS.NET doesn't cut it, check out CodeRush from
www.devexpress.com, since it adds little icons to represent each member.

-mike
MVP
 
M

Mattias Sjögren

Since private is always the default, perhaps you could leave out the
private keyword completely if you think that would that make your code
easier to read.



Mattias
 
J

Jon Skeet [C# MVP]

C# Learner said:
A recent discussion in here has got me reflecting on the "private"
keyword. As I mentioned in the discussion, I find the keyword
problematic, in that it looks too similar to "public" and therefore
makes quickly scanning for public members among private ones, hard.

<snip>

I certainly wouldn't advocate a change of the language at this stage,
and the names "public" and "private" are so common in computer science
that I think the boat has sailed on that one. However, an IDE could
syntax highlight them in different colours to make it clearer.

Alternatively, you could miss out the private modifier like I do :)
 
R

Ravichandran J.V.

1. You can group member objects according to your preference. For
instance, you can set all the private members at the end of the class
and the public ones at the top.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
C

C# Learner

C# Learner wrote:

<snip>

Thank for the replies everyone. I'd be extremely surprised if this
change made its way into the language, but it doesn't hurt to post my
thoughts and for others to consider them :)

I think mikeb made some good points about why this mightn't be very
practical.

Roy Fine, I understand this isn't my language to change. I just see
this as an unfortunate design mistake, and feel it would benefit users
of the language for this to be changed.

I think I'll stick to not using the keyword for private members (which I
currently do). I have no problem with doing this, and think it makes
code clearer than using the keyword, but I still feel that it'd be nice
to be able to use a distinct keyword here.
 

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

Top