I've just came across some code which is slightly strange, I was wondering if anyone could shed light on why it might be written like this.
I think it's got something to do with concurrency - so that the variables can't get changed if another thread accesses it (because variable updates aren't atomic). Or it's speed (because local variables are faster than class level variables?) OR I'm wrong on everything I've written here :)
Oh, I'm not talking about the Hungarian notation, I'm talking about the local assignments inside the methods.
public Class Space
{
private double m_dWidth = 0;
// Constructors & other methods omitted for readability
//...
public double getWidth()
{
double dWidth = m_dWidth;
return dWidth;
}
}
It looks like the person writing the code has misunderstood how return works. It is a common misunderstanding among my java students (university first year) that return only works on locally defined variables instead of arbitrary expressions.
As @Steve suggests, it could also be a hangover from an older, more complicated version of the code. Which one is more likely depends on where you found the code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With