J
James Thurley
I'm having a problem figuring out the best way to use Properties in my
code. Here are the 'good practices' I have in my head:
1) Variables should always be accessed through Properties where possible
in case you need to add, for example, some checking code or whatever
later on.
2) A class should always give copies of its internal objects to external
classes to stop it being corrupted.
For example:
class A{
private Font f;
public Font F{
get{return f;} // Returns reference to internal object
set{
/*Checking code here*/
f=value;
}
}
}
class B{
private Font f;
public Font F{
get{return (Font)f.Clone();} // Returns copy.
set{
/*Checking code here*/
f=value;
}
}
}
class C{
void func(A a, B b){
Font f1 = a.F;
Font f2 = b.F;
f1.Size = f2.Size = 100;
}
}
In the above code class C has bypassed the checking code for the Font
object in class A, but class B is safe.
That's all fine, but what if I have a class derived from class B:
class B2 : B{
public void FrequentDraw(){
/* Code to draw stuff which uses property F */
}
}
Where FrequentDraw() is called many times a second. Here the fact that
F has to copy the Font object each time will become a significant overhead.
I basically want to know what people think should be done in this
situation?
- Should I simply make the f object protected and access it directly,
even though this is 'bad practice'?
- Should I modify the property to return a reference, although this
makes the class less safe and makes my coding inconsistent.
- Should I create a second protected Property called something like
FRef which returns a reference, although this makes the code messy.
Or is there some better way I'm missing?
I have run benchmarks on code returning copies of objects, and while the
overhead is often not significant with small objects, it is highly
significant with more complex ones like the Font object when done
frequently.
Thanks,
James.
code. Here are the 'good practices' I have in my head:
1) Variables should always be accessed through Properties where possible
in case you need to add, for example, some checking code or whatever
later on.
2) A class should always give copies of its internal objects to external
classes to stop it being corrupted.
For example:
class A{
private Font f;
public Font F{
get{return f;} // Returns reference to internal object
set{
/*Checking code here*/
f=value;
}
}
}
class B{
private Font f;
public Font F{
get{return (Font)f.Clone();} // Returns copy.
set{
/*Checking code here*/
f=value;
}
}
}
class C{
void func(A a, B b){
Font f1 = a.F;
Font f2 = b.F;
f1.Size = f2.Size = 100;
}
}
In the above code class C has bypassed the checking code for the Font
object in class A, but class B is safe.
That's all fine, but what if I have a class derived from class B:
class B2 : B{
public void FrequentDraw(){
/* Code to draw stuff which uses property F */
}
}
Where FrequentDraw() is called many times a second. Here the fact that
F has to copy the Font object each time will become a significant overhead.
I basically want to know what people think should be done in this
situation?
- Should I simply make the f object protected and access it directly,
even though this is 'bad practice'?
- Should I modify the property to return a reference, although this
makes the class less safe and makes my coding inconsistent.
- Should I create a second protected Property called something like
FRef which returns a reference, although this makes the code messy.
Or is there some better way I'm missing?
I have run benchmarks on code returning copies of objects, and while the
overhead is often not significant with small objects, it is highly
significant with more complex ones like the Font object when done
frequently.
Thanks,
James.