Wednesday, July 13, 2005

Code Smell

A


class Foo {

function getFoo() {
if ($this->foo) return $this->foo;
else return $_POST["foo"];

}

}

$f = new Foo();
print $f->getFoo();



OR

B


class Foo {

function getFoo() {
return $this->foo;
}

}

$f = new Foo();
print ($f->getFoo()) ? $f->getFoo() : $_POST["foo"];




Which is it, A or B and why?

1 comment:

Dan said...

Interesting. I would opt for B myself, I'm worried about the possibility of unknown data being injected into my object: everything should go through the setters.
Plus it sucks if you instantiate two instances of the same class to do something [ie, copy from object A to object B] and accidentally fuck with things.

But, any other thoughts on A vs B?