Rob,

You're right, many people would consider

        value = rs("Username");

to be better style than

        value = rs(3);

The reason why is that the retrieval by key technique does a better job of
explaining what's going on than retrieval by index does. From "rs(3)", I
have no idea what the value represents. But from "rs("Username")", I can
easily tell that the value is apparently a username. Thus the retrieval by
key approach is easier to understand and makes the code more maintainable.

Better yet, the variable the retrieved value is being stored in should
really be called something more descriptive than "value." Instead, that
variable should be called something like "username." Then even code like

        username = rs(3);

would be fairly self explanatory and code like

        username = rs("Username")

would be downright redundant. So then you could use either name or index
retrieval with equal clarity, and can make your decision based on how likely
it is that the index of values may change.

Since you seem interested in these issues, you might like to check out the
book "Smalltalk Best Practice Patterns" by Kent Beck (ISBN 0-13-476904-X)
(http://vig.prenhall.com/catalog/academic/product/1,4096,013476904X,00.html)
. One of its patterns, "Explaining Temporary Variable," makes the case of
using well named temp vars to explain to developers the role of the value
being retrieved.

Although the book is on Smalltalk, most of the techniques it describes are
directly applicable to Java (such as when to use variables, what scope they
should be, and how to name them well). Its Introduction chapter provides a
really strong motivation for designing code that not only compiles and runs
well, but also that programmers maintaining the code can understand well.

Bobby

-----
Bobby Woolf
Senior Architect
GemStone Systems, a Brokat company
[EMAIL PROTECTED]


-----Original Message-----
From: Rob L'Estrange [mailto:[EMAIL PROTECTED]]
Sent: Sunday, April 29, 2001 18:27
To: [EMAIL PROTECTED]
Subject: [EJB-INT] Off topic: Best practice question.


Hi All

This is offtopic, but I figure we're going to have a good percentage of
commercial developers here...

This is pretty much a (programming) language neutral question.

We have no choice on how we access the members of a simple array - we must
access them via numbered index. For instance,
value = someArray(8);

But for some collections (eg. a ResultSet (basically a table of data
returned by an SQL query) in Java), we can access elements via numbered
index or by name. For instance,
/* By numbered index.
 * This returns the value of a cell in the current row at the third column.
 * (The columns collections is 1-based).
 */
value = rs(3);
/* By name.
 * This returns the value of a cell in the current row at the Username
column.
 */
value = rs("Username");

My question concerns this second type of collection; the type in which
entries can be access by numbered index or by name. I'm after feedback on
the following statement:

"In a commercial software development enviroment, where maintainability is a
significant issue, using 'by name' retrieval of data is preferable to 'by
index' for collections that support both methods of data access. Assume that
we are speaking of non-iterative retrieval of data."

Any and all feedback welcome and appreciated.

Rob

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to