G
Guest
Sometimes a method in a class requires the use of class instance
variables/fields that will not be used outside of the method itself.
Currently this means you must create a instance field in the class such that
from a maintenance stand point it is disconnected from the method, and also
affords the opportunity for other methods to mess with the variable when they
never should.
For example:
public class MyClass
{
bool dataSent_=false;
/* IMAGINE TONS OF OTHER CODE HERE */
public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}
}
Notice out method has no connection to the field even though it is
the only method that should be using it. So there is both a maintenance
issue and a chance for accidentally mucking with the state external from
the method.
--------------------------------------------------------------------
Instead what if we could declare the class/method like this.
public class MyClass
{
/* IMAGINE TONS OF OTHER CODE HERE */
public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_=false;
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}
}
The beauty of this solution is the field that is used only by the method
is directly connected to and only accessible from the method that uses
it. This clarifies the field usage clearly and prevents mucking with
this value elsewhere.
The only issues I see with this are if you needed to clone the object
state it could be a problem depending on your objects usage.
An Alternative to this pattern that is not quite as encapsulated but is
more flexible is to explictly tag the variable with friend methods.
Such as:
public class MyClass
{
[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_=false;
/* IMAGINE TONS OF OTHER CODE HERE */
public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}
public object Clone()
{
MyClass clone=new MyClass()
clone.dataSent_=dataSent_;
return clone;
}
}
This clearly defines/associates which methods are allowed to use the
instance field AND enforces it, as a compiler error should result if used by
another method not listed as a friend.
variables/fields that will not be used outside of the method itself.
Currently this means you must create a instance field in the class such that
from a maintenance stand point it is disconnected from the method, and also
affords the opportunity for other methods to mess with the variable when they
never should.
For example:
public class MyClass
{
bool dataSent_=false;
/* IMAGINE TONS OF OTHER CODE HERE */
public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}
}
Notice out method has no connection to the field even though it is
the only method that should be using it. So there is both a maintenance
issue and a chance for accidentally mucking with the state external from
the method.
--------------------------------------------------------------------
Instead what if we could declare the class/method like this.
public class MyClass
{
/* IMAGINE TONS OF OTHER CODE HERE */
public void SendDataFirstTimeOnly()
{
[InstanceField]
bool dataSent_=false;
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}
}
The beauty of this solution is the field that is used only by the method
is directly connected to and only accessible from the method that uses
it. This clarifies the field usage clearly and prevents mucking with
this value elsewhere.
The only issues I see with this are if you needed to clone the object
state it could be a problem depending on your objects usage.
An Alternative to this pattern that is not quite as encapsulated but is
more flexible is to explictly tag the variable with friend methods.
Such as:
public class MyClass
{
[FriendMethods(SendDataFirstTimeOnly,Clone)]
bool dataSent_=false;
/* IMAGINE TONS OF OTHER CODE HERE */
public void SendDataFirstTimeOnly()
{
if (!dataSent_)
{
Do this();
dataSent_=true;
}
}
public object Clone()
{
MyClass clone=new MyClass()
clone.dataSent_=dataSent_;
return clone;
}
}
This clearly defines/associates which methods are allowed to use the
instance field AND enforces it, as a compiler error should result if used by
another method not listed as a friend.