Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange Java Coding Style - Why?

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;
  }
}
like image 832
user114381 Avatar asked Aug 12 '26 04:08

user114381


1 Answers

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.

like image 194
evilcandybag Avatar answered Aug 14 '26 18:08

evilcandybag