[thelist] Classes, methods, return values and best practice

Jon jon at daemon.com.au
Wed Aug 21 19:28:01 CDT 2002


> So once again, it comes down to a design decision of how safe you want
your
> code to be versus how fast your code needs to be.  I know it sounds like a
> political statement saying "Well, I support this group's view, but I can
> also see how this other group wants something else," but the sense of when
> error checking is really necessary versus when it's simply convenient is
> something that you'll develop intuitively as you write more code.  Just
take
> the time to think about what you're doing and why you're doing it, and
> you'll be fine.  Don't do something simply because other people think it's
> right or because they told you so - understand the reasons behind their
> arguments, and make your own conclusions.


Once you can show that your code is operating correctly ( heavily tested,
both syntheticaly, and in the real world ), then you can think about doing
things like not having get and set methods, and not validating your data.

I would recommend the book 'Effective Java' for a viewpoint that is
extremely defensive ( and I think rightly so ).

As to your question, the typical java practice is to return the object
reference itself.

Object setId( int newId )
{
    this.id = newId;
    return this;
}

The reason being that then you can chain calls to the object,

System.out.println( exampleObject.setId( someId ).getId().toString());

Whether the extra complexity is worth it is another argument :)
It does however make for a reasonably easy to read, left to right
progression.
You do have to know what your return types are, and casting in that context
gets messy.

At any rate, thats for java, and as I dont know any php I cant tell you if
its appropriate ( ie works ) in that context.

Jon.




More information about the thelist mailing list