473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Prefix methods

Rob
Hi,

Would it be safe to prefix private and protected methods and properties with
and underscore? Would this cause any issues with other languages?

Cheers,
Jan 19 '07 #1
18 1452

Rob wrote:
Hi,

Would it be safe to prefix private and protected methods and properties with
and underscore? Would this cause any issues with other languages?

Cheers,
Prefixing the underscore is not acceptable in .net standards..

type qualifiers are enough to differentiate.

Jan 19 '07 #2
Rob wrote:
Would it be safe to prefix private and protected methods and properties with
and underscore? Would this cause any issues with other languages?
It would be safe, but not recommended for protected members. Protected
members should conform to the framework guidelines (PascalCase for both
methods and properties). Think of 'protected' as 'public, only slightly
less so'. There's a huge gap between private and protected.

What you do in private, well, nobody really cares ;)

-- Barry

--
http://barrkel.blogspot.com/
Jan 19 '07 #3
Rob
Thanks for the reply. I do however feel that you misunderstood my question
but I'll replay as follows.

I honestly don't really care about the acceptable .net standards, all
standards change and sometimes not for the good. This has been happening
since the beginning of programming so I believe if you don't have to follow
standards(which sometimes aren't as good), do what feels right and makes you
most productive.

My motivation for prefixing protecteds and privates are simple, one of them
are: when you type the underscore and activate intellisence you get all your
custom members not available to the "outside" so to speak. This is very
helpful to me since you don't have to hunt as hard for that method or
property you've created 3 months ago.

I've had a long and hard battle with myself regarding following the "current
standard" or using my own. To me it's just soo much simpler typing _^space
and getting a list of the internals that are actually performing the work in
my class.

If underscore would break things I'll just go with "z" as a prefix as I've
seen being used by other companies.

So, back to the original question, will prefixing private and protected
methods and properties with an underscore break anything?

"Karthik D V" <Ka************@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>
Rob wrote:
>Hi,

Would it be safe to prefix private and protected methods and properties
with
and underscore? Would this cause any issues with other languages?

Cheers,

Prefixing the underscore is not acceptable in .net standards..

type qualifiers are enough to differentiate.

Jan 19 '07 #4
Rob wrote:
Thanks for the reply. I do however feel that you misunderstood my question
but I'll replay as follows.

I honestly don't really care about the acceptable .net standards, all
standards change and sometimes not for the good.
Don't underestimate the value of consistency and convention!
This has been happening
since the beginning of programming so I believe if you don't have to follow
standards(which sometimes aren't as good), do what feels right and makes you
most productive.
Sure - as long as you understand the rationale behind the standard etc.
My motivation for prefixing protecteds and privates are simple, one of them
are: when you type the underscore and activate intellisence you get all your
custom members not available to the "outside" so to speak.
Protected members are available to the outside - all the outside has to
do is derive. Protected is almost, but not quite, public.
If underscore would break things I'll just go with "z" as a prefix as I've
seen being used by other companies.

So, back to the original question, will prefixing private and protected
methods and properties with an underscore break anything?
I beg of you, if you write code that will be used or maintained by
anybody else, stick with the .NET standards for protected members.

Private and internal members aren't visible to the outside world, so
they will not cause any problems. If you're using IL, you have a much
broader palette of characters to choose from, including '$', '.', etc.

-- Barry

--
http://barrkel.blogspot.com/
Jan 19 '07 #5
Rob
Lol, Barry, some points taken and I do appreciate you time for making those
points. My days of low level stuff like mov ah,4c int 21 and vtables are
long gone Now it's high level all the way, not time to play. Have alook at
my inline comments.

"Barry Kelly" <ba***********@gmail.comwrote in message
news:8p********************************@4ax.com...
Rob wrote:
>Thanks for the reply. I do however feel that you misunderstood my
question
but I'll replay as follows.

I honestly don't really care about the acceptable .net standards, all
standards change and sometimes not for the good.

Don't underestimate the value of consistency and convention!
Never had a problem with consistency, it's the convention thats not to my
liking. Why throw apples, oranges in one bag if you can have them in
separate bags? When I stick my hand in the bag I'd rather know what I'll be
taking out.
>This has been happening
since the beginning of programming so I believe if you don't have to
follow
standards(which sometimes aren't as good), do what feels right and makes
you
most productive.

Sure - as long as you understand the rationale behind the standard etc.
I've always had the "Guidelines for Names" in my VS Help Favorites although
I've never seen any reason why not to prefix. All I see is things like "Do
not use a prefix for field names. For example, do not use g_ or s_ to
distinguish static versus non-static fields.". Then again I can always buy
the book "Framework Design Guidelines: Conventions, Idioms, and Patterns for
Reusable .NET Libraries" that the MSDN refers to.
>My motivation for prefixing protecteds and privates are simple, one of
them
are: when you type the underscore and activate intellisence you get all
your
custom members not available to the "outside" so to speak.

Protected members are available to the outside - all the outside has to
do is derive. Protected is almost, but not quite, public.
By "outside" I meant outside the class. They are still protected in the
derived class.
>If underscore would break things I'll just go with "z" as a prefix as
I've
seen being used by other companies.

So, back to the original question, will prefixing private and protected
methods and properties with an underscore break anything?

I beg of you, if you write code that will be used or maintained by
anybody else, stick with the .NET standards for protected members.
Not to worry, that's where the "Convention" wins hands down.
Private and internal members aren't visible to the outside world, so
they will not cause any problems. If you're using IL, you have a much
broader palette of characters to choose from, including '$', '.', etc.
Hmm, maybe I'll start using IL :P
-- Barry

--
http://barrkel.blogspot.com/

Jan 19 '07 #6
Rob wrote:
Lol, Barry, some points taken and I do appreciate you time for making those
points. My days of low level stuff like mov ah,4c int 21 and vtables are
long gone Now it's high level all the way, not time to play.
Lucky you. I work on compilers now, both Win32 and .NET - I can't escape
the MOVs.
"Barry Kelly" <ba***********@gmail.comwrote:
Rob wrote:
[...]
I've always had the "Guidelines for Names" in my VS Help Favorites although
I've never seen any reason why not to prefix. All I see is things like "Do
not use a prefix for field names. For example, do not use g_ or s_ to
distinguish static versus non-static fields.".
Yes, but those apply to public fields (which ought to be rare, if ever -
properties etc.). You can do whatever you like in private, and whatever
other people on your team can tolerate in internal.
Then again I can always buy
the book "Framework Design Guidelines: Conventions, Idioms, and Patterns for
Reusable .NET Libraries" that the MSDN refers to.
Most of the naming content (for example) is available in the SDK
guidelines, but it does collect together a lot of other info that is
spread out in blog postings etc. over the past few years. I didn't learn
a huge amount from it, but it's definitely worthwhile to bring it all
together. There are a few nuggets of info in the annotations too.
My motivation for prefixing protecteds and privates are simple, one of
them
are: when you type the underscore and activate intellisence you get all
your
custom members not available to the "outside" so to speak.
Protected members are available to the outside - all the outside has to
do is derive. Protected is almost, but not quite, public.

By "outside" I meant outside the class. They are still protected in the
derived class.
Sure. The larger point I suppose I was making is that when your classes
aren't sealed, you're designing for two different users: people who
instantiate the class, and people who derive from the class. I think too
many people overestimate how "protected" protected is - it isn't really
"protected" at all, only slightly less public. For example, invariants
still need to hold with protected methods (or be very well documented),
there can't be any less-secure methods in there, etc.

Don't get me started on virtual methods; that's even worse, because
there are two sides - the implementer and the caller, and the caller may
be the class itself, where the implement or might need to deal with
invariants not holding etc. Implementing virtual methods and esp.
documenting them is not that easy.

-- Barry

--
http://barrkel.blogspot.com/
Jan 19 '07 #7
Hi,
Prefixing the underscore is not acceptable in .net standards..
The standards make no mention (anymore) of the format for private members,
unfortunately.
type qualifiers are enough to differentiate.
However, if by type qualifiers you mean Hungarian notation, it's not
recommended for class libraries:

General Naming Conventions
http://msdn2.microsoft.com/en-us/library/ms229045.aspx

"These guidelines apply to all identifiers"

--
Dave Sexton
http://davesexton.com/blog

Jan 19 '07 #8
On Fri, 19 Jan 2007 17:17:56 +1300, "Rob" <ro*@axebluebird.comwrote:
>Hi,

Would it be safe to prefix private and protected methods and properties with
and underscore? Would this cause any issues with other languages?

Cheers,
The C++ standard reserves all identifiers containing two consecutive
underscores, "__", for the implementation. In some circumstances it
also reserves identifiers starting with underscore followed by a
capital letter.

To retain C++ compatibility it is safest to either make sure that any
leading underscore is followed by a lowercase letter only or to use
the "m_xxxx" convention for private data or methods.

As others have pointed out, protected data and methods should normally
follow the standard naming conventions.

rossum

Jan 19 '07 #9
Barry Kelly <ba***********@gmail.comwrote:

<snip>
Don't get me started on virtual methods; that's even worse, because
there are two sides - the implementer and the caller, and the caller may
be the class itself, where the implement or might need to deal with
invariants not holding etc. Implementing virtual methods and esp.
documenting them is not that easy.
Absolutely - inheritance is great when it's really, really what's
needed, but composition is much easier to cope with.

See http://msmvps.com/jon.skeet/archive/...itancetax.aspx
for my general rant about inheritance :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '07 #10
Hi,

I don't mean to start up this discussion again, but I have to point out that
I think this is the problem - trying to preserve compatibility in managed
languages to unmanaged conventions for the sake of compatibility only.

Of course, people have brought to my attention the usefulness of having m_
or __ as a prefix for private fields, but I still believe that its limited
value (and its value doesn't apply to everyone who actually uses it) isn't
worth the degradation in readability or the confusion of programmers that
aren't accustomed to using it.

Now, if your entire organization is based on C++ or Java programmers that
want to retain the convention, then by all means do so :)

--
Dave Sexton
http://davesexton.com/blog

"rossum" <ro******@coldmail.comwrote in message
news:b3********************************@4ax.com...
On Fri, 19 Jan 2007 17:17:56 +1300, "Rob" <ro*@axebluebird.comwrote:
>>Hi,

Would it be safe to prefix private and protected methods and properties
with
and underscore? Would this cause any issues with other languages?

Cheers,
The C++ standard reserves all identifiers containing two consecutive
underscores, "__", for the implementation. In some circumstances it
also reserves identifiers starting with underscore followed by a
capital letter.

To retain C++ compatibility it is safest to either make sure that any
leading underscore is followed by a lowercase letter only or to use
the "m_xxxx" convention for private data or methods.

As others have pointed out, protected data and methods should normally
follow the standard naming conventions.

rossum

Jan 19 '07 #11
hi Barry,

Barry Kelly wrote:
It would be safe, but not recommended for protected members. Protected
members should conform to the framework guidelines (PascalCase for both
methods and properties).
Where can i find these guidelines?
mfG
--stefan <--
Jan 19 '07 #12

Barry Kelly wrote:
Sure. The larger point I suppose I was making is that when your classes
aren't sealed, you're designing for two different users: people who
instantiate the class, and people who derive from the class. I think too
many people overestimate how "protected" protected is - it isn't really
"protected" at all, only slightly less public. For example, invariants
still need to hold with protected methods (or be very well documented),
there can't be any less-secure methods in there, etc.
Barry,

Yep. Recently I've been sealing all of my classes immediately and
unsealing them only when I want to allow the class to be subclassed and
after I have thought out all of the consequences. To me anyway, one of
the biggest issues of non-sealed classes is versioning. Non-sealed
classes are more difficult to version because subclasses can tweak the
semantics of the class in ways that are difficult to predict.

I know there has been debate in the past about whether classes should
be sealed by default. I *think* the Framework Design Guidelines book
says that classes should remain unsealed unless there is a specific and
compelling reason to seal them (don't quote me on that though).
Admittedly, I've been wavering back on forth on this topic for awhile
now. Comments?

Brian

Jan 19 '07 #13
Stefan Hoffmann <st*************@explido.dewrote:
It would be safe, but not recommended for protected members. Protected
members should conform to the framework guidelines (PascalCase for both
methods and properties).
Where can i find these guidelines?
http://tinyurl.com/2cun

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '07 #14
hi Jon,

Jon Skeet [C# MVP] wrote:
>>It would be safe, but not recommended for protected members. Protected
members should conform to the framework guidelines (PascalCase for both
methods and properties).
Where can i find these guidelines?
http://tinyurl.com/2cun
Thanks.
mfG
--stefan <--
Jan 19 '07 #15
Hi Brian,

I'd like to see classes sealed by default, unsealing them only when there is
a specific, compelling reason. To code inheritance properly isn't always
easy or straight-forward and can cause problems for programmers that derive
from your classes "hoping" that they can extend its functionality safely. A
sealed class lets them know that they probably shouldn't derive from your
class, and can't. If sealed is the default, then an unsealed class would
let developers know that they can safely derive from your class.

--
Dave Sexton
http://davesexton.com/blog

"Brian Gideon" <br*********@yahoo.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>
Barry Kelly wrote:
>Sure. The larger point I suppose I was making is that when your classes
aren't sealed, you're designing for two different users: people who
instantiate the class, and people who derive from the class. I think too
many people overestimate how "protected" protected is - it isn't really
"protected" at all, only slightly less public. For example, invariants
still need to hold with protected methods (or be very well documented),
there can't be any less-secure methods in there, etc.

Barry,

Yep. Recently I've been sealing all of my classes immediately and
unsealing them only when I want to allow the class to be subclassed and
after I have thought out all of the consequences. To me anyway, one of
the biggest issues of non-sealed classes is versioning. Non-sealed
classes are more difficult to version because subclasses can tweak the
semantics of the class in ways that are difficult to predict.

I know there has been debate in the past about whether classes should
be sealed by default. I *think* the Framework Design Guidelines book
says that classes should remain unsealed unless there is a specific and
compelling reason to seal them (don't quote me on that though).
Admittedly, I've been wavering back on forth on this topic for awhile
now. Comments?

Brian

Jan 19 '07 #16
Hi,

That links to the old MSDN content. Here's the latest and greatest:

..NET Framework Developer's Guide
Design Guidelines for Developing Class Libraries
http://msdn2.microsoft.com/en-us/library/ms229042.aspx

--
Dave Sexton
http://davesexton.com/blog

"Stefan Hoffmann" <st*************@explido.dewrote in message
news:eq****************@TK2MSFTNGP02.phx.gbl...
hi Jon,

Jon Skeet [C# MVP] wrote:
>>>It would be safe, but not recommended for protected members. Protected
members should conform to the framework guidelines (PascalCase for both
methods and properties).
Where can i find these guidelines?
http://tinyurl.com/2cun
Thanks.
mfG
--stefan <--

Jan 19 '07 #17
Dave Sexton <dave@jwa[remove.this]online.comwrote:
I'd like to see classes sealed by default, unsealing them only when there is
a specific, compelling reason.
Ditto. C# improved on Java by making *methods* non-virtual by default -
I'd have thought that making classes sealed by default would have been
the logical next step.
A sealed class lets them know that they probably shouldn't derive
from your class, and can't. If sealed is the default, then an
unsealed class would let developers know that they can safely derive
from your class.
Indeed - and that deliberate action invites the further deliberate
actions which are effectively required: documenting *how* to safely
derive from the class, what can be expected when overriding particular
methods etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 19 '07 #18
Hi Jon,

Good point. One good practice leads to another (hopefully :)

I tend to mark classes as sealed as soon as they are created, by default,
but it's easy to forget. At least in Visual Studio a simple item template
could solve that problem, although I don't use one myself.

--
Dave Sexton
http://davesexton.com/blog

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Dave Sexton <dave@jwa[remove.this]online.comwrote:
>I'd like to see classes sealed by default, unsealing them only when there
is
a specific, compelling reason.

Ditto. C# improved on Java by making *methods* non-virtual by default -
I'd have thought that making classes sealed by default would have been
the logical next step.
>A sealed class lets them know that they probably shouldn't derive
from your class, and can't. If sealed is the default, then an
unsealed class would let developers know that they can safely derive
from your class.

Indeed - and that deliberate action invites the further deliberate
actions which are effectively required: documenting *how* to safely
derive from the class, what can be expected when overriding particular
methods etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jan 20 '07 #19

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

Similar topics

1
by: Holger Joukl | last post by:
Hi there, 2 questions regarding build/installation issues: 1. In the python 2.3.3 setup.py script, the detect_modules method of class PyBuildExt contains the following code: 253 if...
3
by: Jacques | last post by:
I'm experiencing the following problem The source xml file uses a prefix (wpl:) and look like this (shortened): ----------------------------------------------------------------- <?xml...
4
by: jb | last post by:
I have discovered that when the WSDL is auto-generated in .NET (i.e. http://.../MyService.asmx?WSDL): * Prior to SP1, it generated xmlns:s0="http://mynamespace/" in <wsdl:definitions>, and then...
2
by: littlebeam | last post by:
Suppose there is a table containing these recodes. country ------- CON_CHN CON_JAP JAP CON_CHN When I use the following sql:
4
by: BizTalk Benjamin | last post by:
Hi, I have an XmlDocument loaded from a memory stream. I set the document element prefix in this way XmlElement e = xDoc.DocumentElement; e.Prefix = "abc" When i simply write the document...
1
by: GR | last post by:
Hi, I have something like the following: (Please note this not actual code and it's very rough) string source = "<abc:Root xmlns:abc="http://mynamespace.com><abc:Record/></abc:Root>"...
2
by: scottpet | last post by:
Hi, I want to add a namespace prefix to the root node of an object I am serializing to XML. I have been reading though this article:...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)” to mean “1+2”....
11
by: cjt22 | last post by:
Hi startswith( prefix]) States: Return True if string starts with the prefix, otherwise return False. prefix can also be a tuple of suffixes to look for. However when I try and add a tuple...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.