473,804 Members | 2,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convention question

GC
Within a class, do you use the private keyword for your
internal member variables or do you leave it off like you
would for variables within a function?
Nov 15 '05 #1
41 1631
GC <gc********@yah oo.com> wrote:
Within a class, do you use the private keyword for your
internal member variables or do you leave it off like you
would for variables within a function?


I leave it off in C#, but use it in Java. It's important that they
*are* private, but as that's the default in C# it's not really a
problem.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #2
I am a fan of explicitly writing things like that - because A) I didn't know
that was the default for C# and I'm sure there are others that don't.. B)
when I come back to my code 8 months (and dozens of small projects later) -
I'm not going to be able to remember all the assumptions I made...

"Software bugs are born solely from assumptions." (my quote, although - I
keep refining it.. I'll have the final one soon)

So for the 1/4 second it takes to write this, might save me hours later on
when I'm (or some poor soul) is trying to fix my code..

"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
GC <gc********@yah oo.com> wrote:
Within a class, do you use the private keyword for your
internal member variables or do you leave it off like you
would for variables within a function?


I leave it off in C#, but use it in Java. It's important that they
*are* private, but as that's the default in C# it's not really a
problem.

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

Nov 15 '05 #3
Drebin <tR************ *@hotmail.com> wrote:
I am a fan of explicitly writing things like that - because A) I didn't know
that was the default for C# and I'm sure there are others that don't.
I would personally like to hope that everyone I work with will know the
language well enough to know that - or else they'd ask. Non-private
writable (or mutable) member variables are enough of an eye-opener that
I know *I'd* check what was going on if I suspected some code I was
working with was using them.
B)
when I come back to my code 8 months (and dozens of small projects later) -
I'm not going to be able to remember all the assumptions I made...
Does that mean you're not going to accept all the other things that the
C# specification explicitly states then?

Accepting that C# works as per the specification isn't the same as
assuming that the implementation of something will work in some
particular (undocumented) way.
"Software bugs are born solely from assumptions." (my quote, although - I
keep refining it.. I'll have the final one soon)
Software bugs are often born from *false* assumptions. It's easy to
absolutely *prove* that the assumption is true in this case - you just
look at the C# specification.
So for the 1/4 second it takes to write this, might save me hours later on
when I'm (or some poor soul) is trying to fix my code..


Could you give an example of a bug which leaving out "private" might
cause?

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #4
I can't believe you'd even argue these points!! This is a matter of
principle, to me. This is simply a case of how much does it hurt versus how
much does it help. And the odds are against you, the ONLY advantage to
leaning on the assumptions of a standard is that it lets the developer be
lazy. The Up side FAR outweighs this, by making sure you don't have any
assumptions - documented or not. Mean what you type and type what you
mean... Oh!! I think will be one of my next quotes!! :o)

I don't have a specific problem with private/public - all I know is I spent
most of my career cleaning up and rewriting apps, and running into so many
assumptions. 98% of any problem I've ever had was trying to figure out what
the developer meant and figure out what assumptions they had in thier head
while they were writing this. For example, in VB6 -off the top of your head-
are array's zero-based or one-based? Are enums (that don't have values)
zero-based or one-based? What about VB3? Are you sure?

Things like that - I don't want me, or anyone else looking at my code having
to look up specifications. Especially since what you're saying is fine right
now. What about when C# 2.0 comes out and they change the spec - and you're
looking at this assumption-based code - and you don't know if I wrote it in
the "olden 1.0 days when *these* were the assumptions" or was it written
with the "new improved spec"?? If instead, I just TYPE where my head is at.
If I type public blah, private blah, protected blah - there is ABSOLUTELY no
room for interpretation or assumption. So, if it takes no time to type it -
why WOULDN'T you? Except out of laziness... You have so little work to do,
to gain so much clarity!!!

And I'll tell you, I've been burned SOOOOO many times with the above-type
scenario with developers like you!! And I just like doing my little part to
make my life easier and hopefully some guy that will have to fix my code in
a year from now.. You're applying a very academic viewpoint to real-world
coding, and that's what makes all this such a mess.. on behalf of some
random person that might work for your company and have to fix your code,
please reconsider.. :o)

Lastly, this will really burn you too - but even though I use "using ...;"
statements, I just about always preface the first instance of a class with
the fully-qualified namespace. That is, unless it's regularly used ones,
like System.Data.Ole Db. or System.IO.... I wish every developer would adopt
these simple concepts - I believe this is a huge part of being a responsible
developer...
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Drebin <tR************ *@hotmail.com> wrote:
I am a fan of explicitly writing things like that - because A) I didn't know that was the default for C# and I'm sure there are others that don't.


I would personally like to hope that everyone I work with will know the
language well enough to know that - or else they'd ask. Non-private
writable (or mutable) member variables are enough of an eye-opener that
I know *I'd* check what was going on if I suspected some code I was
working with was using them.
B)
when I come back to my code 8 months (and dozens of small projects later) - I'm not going to be able to remember all the assumptions I made...


Does that mean you're not going to accept all the other things that the
C# specification explicitly states then?

Accepting that C# works as per the specification isn't the same as
assuming that the implementation of something will work in some
particular (undocumented) way.
"Software bugs are born solely from assumptions." (my quote, although - I keep refining it.. I'll have the final one soon)


Software bugs are often born from *false* assumptions. It's easy to
absolutely *prove* that the assumption is true in this case - you just
look at the C# specification.
So for the 1/4 second it takes to write this, might save me hours later on when I'm (or some poor soul) is trying to fix my code..


Could you give an example of a bug which leaving out "private" might
cause?

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

Nov 15 '05 #5
GC,
I normally explicitly list them as Private. Independent of the language.

As Jon stated, private is the default, so its really not needed.

Hope this helps
Jay

"GC" <gc********@yah oo.com> wrote in message
news:02******** *************** *****@phx.gbl.. .
Within a class, do you use the private keyword for your
internal member variables or do you leave it off like you
would for variables within a function?

Nov 15 '05 #6
Drebin <tR************ *@hotmail.com> wrote:
I can't believe you'd even argue these points!! This is a matter of
principle, to me.
Likewise.
This is simply a case of how much does it hurt versus how
much does it help.
Likewise.
And the odds are against you, the ONLY advantage to
leaning on the assumptions of a standard is that it lets the developer be
lazy.
On the contrary - I gain readability (see below) precisely because I
*haven't* been lazy - because I've read the standard! I've put in a bit
of effort up front to make my life easier in the long term.
The Up side FAR outweighs this, by making sure you don't have any
assumptions - documented or not. Mean what you type and type what you
mean... Oh!! I think will be one of my next quotes!! :o)
You still have assumptions though - you just choose different ones to
make.
I don't have a specific problem with private/public - all I know is I spent
most of my career cleaning up and rewriting apps, and running into so many
assumptions. 98% of any problem I've ever had was trying to figure out what
the developer meant and figure out what assumptions they had in thier head
while they were writing this. For example, in VB6 -off the top of your head-
are array's zero-based or one-based? Are enums (that don't have values)
zero-based or one-based? What about VB3? Are you sure?
I don't do VB, so I don't know. However, when I'm writing in a language
I like to know what it does - so unless it actually makes things harder
to understand (as knowing and always relying on operator precedence as
opposed to grouping things with brackets does), I'll use the guarantees
the specification makes.

In this case, the keyword private is not only redundant, but I believe
it's distracting. I find it clearer to read code which doesn't have the
modifier when it's not needed - it means that any modifiers which *are*
there stick out more.
Things like that - I don't want me, or anyone else looking at my code having
to look up specifications.
Just how far does that go? What about knowing the default values for
static/instance variables?

Insisting on the modifier reminds me of people setting variables to
null when they're not needed any more, "just in case".
Especially since what you're saying is fine right
now. What about when C# 2.0 comes out and they change the spec - and you're
looking at this assumption-based code - and you don't know if I wrote it in
the "olden 1.0 days when *these* were the assumptions" or was it written
with the "new improved spec"??
If they make breaking changes like that to the spec, I'd be very
surprised - and if you don't know the spec to start with, you're not
going to know which of *your* assumptions have been broken by changes.
If instead, I just TYPE where my head is at.
If I type public blah, private blah, protected blah - there is ABSOLUTELY no
room for interpretation or assumption.
In this particular case. As I say though: you *do* rely on guarantees
the specification makes, otherwise you'd have no language to code in at
all. For instance, I bet you assume that "i" isn't a keyword - that
it's a valid identifier. Better watch out if they change that in C#
v2...
So, if it takes no time to type it -
why WOULDN'T you? Except out of laziness... You have so little work to do,
to gain so much clarity!!!
No, to *lose* clarity in my view - it means that those things which
*do* have modifiers no longer stand out as much.
And I'll tell you, I've been burned SOOOOO many times with the above-type
scenario with developers like you!!
Developers like me who are making accurate assumptions based on
specifications, or developers who *aren't* like me and who are guessing
at behaviour?

I'd be surprised if you have been burned often by people rigidly
following specifications.
And I just like doing my little part to
make my life easier and hopefully some guy that will have to fix my code in
a year from now.. You're applying a very academic viewpoint to real-world
coding,
Not at all - I'm applying my practical experience to the situation.
and that's what makes all this such a mess.. on behalf of some
random person that might work for your company and have to fix your code,
please reconsider.. :o)
On behalf of people who know the spec and who don't like reading
redundant and distracting code, please reconsider.
Lastly, this will really burn you too - but even though I use "using ...;"
statements, I just about always preface the first instance of a class with
the fully-qualified namespace. That is, unless it's regularly used ones,
like System.Data.Ole Db. or System.IO.... I wish every developer would adopt
these simple concepts - I believe this is a huge part of being a responsible
developer...


Again, I consider that pointless. If I have any doubts about what
namespace the class comes from, I can always hover over it and find
out. Sure, that doesn't help if the code is being viewed other than in
an IDE, but I don't see that as a problem: the gain is readability for
the vast majority of situations, ie where the developer actually knows
full well which classes they're using.

As for "the first instance" - that sounds like a recipe for
inconsistency, as what is originally the first instance may well not be
in the future, and more to the point it may *very* well not be the
first time that a developer sees the class referred to, as they may
well be looking in a specific method or property rather than reading
the whole class from the top down.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #7
Jon,

What's it like to never make a mistake ?

JIM
"Jon Skeet" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@news.microsof t.com...
Drebin <tR************ *@hotmail.com> wrote:
I can't believe you'd even argue these points!! This is a matter of
principle, to me.


Likewise.
This is simply a case of how much does it hurt versus how
much does it help.


Likewise.
And the odds are against you, the ONLY advantage to
leaning on the assumptions of a standard is that it lets the developer be lazy.


On the contrary - I gain readability (see below) precisely because I
*haven't* been lazy - because I've read the standard! I've put in a bit
of effort up front to make my life easier in the long term.
The Up side FAR outweighs this, by making sure you don't have any
assumptions - documented or not. Mean what you type and type what you
mean... Oh!! I think will be one of my next quotes!! :o)


You still have assumptions though - you just choose different ones to
make.
I don't have a specific problem with private/public - all I know is I spent most of my career cleaning up and rewriting apps, and running into so many assumptions. 98% of any problem I've ever had was trying to figure out what the developer meant and figure out what assumptions they had in thier head while they were writing this. For example, in VB6 -off the top of your head- are array's zero-based or one-based? Are enums (that don't have values)
zero-based or one-based? What about VB3? Are you sure?


I don't do VB, so I don't know. However, when I'm writing in a language
I like to know what it does - so unless it actually makes things harder
to understand (as knowing and always relying on operator precedence as
opposed to grouping things with brackets does), I'll use the guarantees
the specification makes.

In this case, the keyword private is not only redundant, but I believe
it's distracting. I find it clearer to read code which doesn't have the
modifier when it's not needed - it means that any modifiers which *are*
there stick out more.
Things like that - I don't want me, or anyone else looking at my code having to look up specifications.


Just how far does that go? What about knowing the default values for
static/instance variables?

Insisting on the modifier reminds me of people setting variables to
null when they're not needed any more, "just in case".
Especially since what you're saying is fine right
now. What about when C# 2.0 comes out and they change the spec - and you're looking at this assumption-based code - and you don't know if I wrote it in the "olden 1.0 days when *these* were the assumptions" or was it written
with the "new improved spec"??


If they make breaking changes like that to the spec, I'd be very
surprised - and if you don't know the spec to start with, you're not
going to know which of *your* assumptions have been broken by changes.
If instead, I just TYPE where my head is at.
If I type public blah, private blah, protected blah - there is ABSOLUTELY no room for interpretation or assumption.


In this particular case. As I say though: you *do* rely on guarantees
the specification makes, otherwise you'd have no language to code in at
all. For instance, I bet you assume that "i" isn't a keyword - that
it's a valid identifier. Better watch out if they change that in C#
v2...
So, if it takes no time to type it -
why WOULDN'T you? Except out of laziness... You have so little work to do, to gain so much clarity!!!


No, to *lose* clarity in my view - it means that those things which
*do* have modifiers no longer stand out as much.
And I'll tell you, I've been burned SOOOOO many times with the above-type scenario with developers like you!!


Developers like me who are making accurate assumptions based on
specifications, or developers who *aren't* like me and who are guessing
at behaviour?

I'd be surprised if you have been burned often by people rigidly
following specifications.
And I just like doing my little part to
make my life easier and hopefully some guy that will have to fix my code in a year from now.. You're applying a very academic viewpoint to real-world coding,


Not at all - I'm applying my practical experience to the situation.
and that's what makes all this such a mess.. on behalf of some
random person that might work for your company and have to fix your code, please reconsider.. :o)


On behalf of people who know the spec and who don't like reading
redundant and distracting code, please reconsider.
Lastly, this will really burn you too - but even though I use "using ....;" statements, I just about always preface the first instance of a class with the fully-qualified namespace. That is, unless it's regularly used ones,
like System.Data.Ole Db. or System.IO.... I wish every developer would adopt these simple concepts - I believe this is a huge part of being a responsible developer...


Again, I consider that pointless. If I have any doubts about what
namespace the class comes from, I can always hover over it and find
out. Sure, that doesn't help if the code is being viewed other than in
an IDE, but I don't see that as a problem: the gain is readability for
the vast majority of situations, ie where the developer actually knows
full well which classes they're using.

As for "the first instance" - that sounds like a recipe for
inconsistency, as what is originally the first instance may well not be
in the future, and more to the point it may *very* well not be the
first time that a developer sees the class referred to, as they may
well be looking in a specific method or property rather than reading
the whole class from the top down.

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

Nov 15 '05 #8
james <no****@hyperco n.net> wrote:
I have to agree with you Drebin, I believe that by explicitly writing
private
you show future readers that you truly meant private and didn't just forget
to put it there because you were lazy ! If you see a variable with no specifier you have to ask yourself, did the
programmer mean for it to be private or just get sloppy and forget to type
it ?


Variables should almost always be private. When was the last time you
actually wanted to make something non-private, but forgot and
*accidentally* made it private, without the compiler whinging because
you then tried to use it?

I virtually never use non-private variables other than for constants
etc - I would hope that any developer I work with would take the same
attitude. Given that, it's entirely reasonable to assume that someone
working in a language which has private as the default modifier
actually *means* private when they don't put anything else on.

Note that there are various other parts of the spec where in fact
modifiers aren't even *allowed* because there's no real choice - if you
*could* put modifiers on constants, for instance, would you declare:

public static const int Foo = 5;

etc everywhere as well?

There are places where redundancy is good for readability. I don't
believe this is one of those places.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #9
james <no****@hyperco n.net> wrote:
What's it like to never make a mistake ?


I make mistakes - but I can't see how this particular situation would
lead to a mistake. Could you enlighten me with an example?

Congratulations on not addressing any of my points, by the way.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #10

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

Similar topics

3
5029
by: boxboy | last post by:
I am hoping someone from Microsoft could shed some insight into this question. Why did microsoft decide to use a verb based naming convtion for events rather such as Close and Click for rather than prepending them with On as in OnClose and OnClick? The way it is now, I encounter a lot of naming conflicts/ambiguity when trying to follow Microsoft's reccommened approach in naming my own events.
9
2335
by: Sandeep Sharma | last post by:
For many years I have been following the convention of naming all class attributes with a leading underscore. This enables me to quickly identify the class attributes when I encounter them in the source code. Even the GoF book follows this convention, although it's true that the GoF book is not an authoritative source for coding guidelines. Recently a colleague remarked that using a leading underscore is a bad programming practice...
14
3146
by: 42 | last post by:
Hi, Stupid question: I keep bumping into the desire to create classes and properties with the same name and the current favored naming conventions aren't automatically differentiating them... (both are "Pascal Case" with no leading or trailing qualifiers). For example... I'll be modelling something, e.g. a computer, and I'll
19
2280
by: John Salerno | last post by:
I just realized that the C# convention for uses braces is to put the opening brace immediately after the first line that defines the block (except for class declarations) like this: if (something) { stuff } Why is that? Do a lot of people do this? It seems like putting braces on separate lines is much nicer and easier to read. In the book I'm
0
1748
by: Carl Colijn | last post by:
Hi all, Disclaimer: before I might trigger your "let's start a holy war!" button, I'd like to say I'm not intended to; I just post this message to get some input and not to promote "Yet Another Naming Convention". It's a bit of a long post, but I've spent the past few days on perfecting this, finally came to the conclusion that maybe I went a bit overboard with it, almost willing to just say "the @#$ with it" but I didn't want to let it...
2
3153
by: Geler | last post by:
A theoretical question: Sorry if its a beginner question. Here is a quote from the MSDN explaning the C/C++ calling convention.. It demonstrates that the calling function is responsible to clean the stack pointer and it does it by the command "add esp,8" after returning from the called function. My questions: 1. Is the stack pointer common in a certain thread(or process)? 2. How does the called function get the parameters, is it by...
1
3202
by: mrpubnight | last post by:
Hi everyone, just hoping to get some opinions from those who have been using/creating namespaces for a while. Currently I have our namespace as follows: CompanyName.Location.Client.Class So for class DMRequest I may do something like this: ABC.Base.DMRequestBase -base class
114
7889
by: Jonathan Wood | last post by:
I was just wondering what naming convention most of you use for class variables. Underscore, "m_" prefix, camel case, capitalized, etc? Has one style emerged as the most popular? Thanks for any comments. --
35
12201
by: Smithers | last post by:
Is it common practise to begin the name of form classes with "frm" (e.g., frmOneForm, frmAnotherForm). Or is that generally considered an outdated convention? If not "frm" what is a common or recommended practise? Thanks.
3
1676
by: parez | last post by:
Hi, My namespace looks like DDD.DEF.Something.Otherthing DDD is abbreviation for the company name and DEF for the product name. FxCop is asking me to change it Ddd.Def.Something.Otherthing.
0
9705
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9576
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
7613
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
6847
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
5516
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
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2988
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.