Design question...

M

Mark Broadbent

This is probably quite an idiotic question for some of you gurus but I'm
good at asking them :)
is it generally considered bad form for code in a private class procedure to
access private class variables?
Personally I don't see a problem with it, except that I've got this niggling
thing at the back of my mind stemming back to my Pascal days which I seem to
recall suggested that all procedures should only access data through their
own private vars or via parameters and return values. Adhering to this would
(in my opinion cause design headaches in OOP and unnecessarily bloat code).


e.g.
Class MyClass
{
private string firstName;
private void resetName()
{
firstName = "";
}

//various public methods
.....
}

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
 
J

Jon Skeet [C# MVP]

Mark Broadbent said:
This is probably quite an idiotic question for some of you gurus but I'm
good at asking them :)
is it generally considered bad form for code in a private class procedure to
access private class variables?

Nope, not at all.
Personally I don't see a problem with it, except that I've got this niggling
thing at the back of my mind stemming back to my Pascal days which I seem to
recall suggested that all procedures should only access data through their
own private vars or via parameters and return values. Adhering to this would
(in my opinion cause design headaches in OOP and unnecessarily bloat code).

Haven't heard of that.

Admittedly there's the school of thought which says that if there's a
property which effectively exposes a variable, *all* access should go
through the property - the variable itself shouldn't be touched outside
the property code. Unfortunately that's not feasible in C# v1 as you
can't have a public accessor but private mutator. Roll on v2!
 
C

C# Learner

Mark said:
This is probably quite an idiotic question for some of you gurus but I'm
good at asking them :)

is it generally considered bad form for code in a private class procedure to
access private class variables?

Sometimes when writing private methods I wonder whether or not they
should be instances methods which access the instance's state directly,
or static methods which are passed parameters and which perhaps return
values.

I think it depends on the situation, though I tend to use instance
methods because either it seems more suitable or it makes the code
shorter and more readable.

Was there any particular code that you were looking at which prompted
your question?
 
M

Mark Broadbent

nothing particular, just me playing around with code.

--


Br,
Mark Broadbent
mcdba , mcse+i
=============
C# Learner said:
Mark said:
This is probably quite an idiotic question for some of you gurus but I'm
good at asking them :)

is it generally considered bad form for code in a private class procedure to
access private class variables?

Sometimes when writing private methods I wonder whether or not they
should be instances methods which access the instance's state directly,
or static methods which are passed parameters and which perhaps return
values.

I think it depends on the situation, though I tend to use instance
methods because either it seems more suitable or it makes the code
shorter and more readable.

Was there any particular code that you were looking at which prompted
your question?
 

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