 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Richard Guest
|
Posted: Wed Sep 15, 2004 9:50 am Post subject: Problem with lack of pass-by-result |
|
|
Java newbie.
As a Java newbie, I'm charged with learning the language by
maintaining a pretty good-sized pile of code.
The conceptual problem I'm having is with the C convention of using
pass-by-result (and pass-by-value-result) function parameters to
readily show which functions use and change variables outside of the
function scope.
In Java, pass-by-result vars are supposedly verbotem (although check
BufferInputStream.read(byte[] b, int off, int len) for at least one
exception to that rule that I've seen so far).
So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
--
What matters most?
|
|
| Back to top |
|
 |
Stefan Schulz Guest
|
Posted: Wed Sep 15, 2004 10:51 am Post subject: Re: Problem with lack of pass-by-result |
|
|
Richard wrote:
| Quote: | Java newbie.
As a Java newbie, I'm charged with learning the language by
maintaining a pretty good-sized pile of code.
|
Good luck ;)
| Quote: | The conceptual problem I'm having is with the C convention of using
pass-by-result (and pass-by-value-result) function parameters to
readily show which functions use and change variables outside of the
function scope.
|
In Java, you usally do not care outside of the classes implementation.
Each of them should be self-contained, not showing any of the internal
state to the "outside world".
| Quote: | In Java, pass-by-result vars are supposedly verbotem (although check
BufferInputStream.read(byte[] b, int off, int len) for at least one
exception to that rule that I've seen so far).
|
Not really, you pass an Object (an array of bytes) which is passed by
value, just like everything else in java. However, as with all object
references, you can actually _change the state_ of that Object. That you
do this in a much more direct fashion then with most other Objects does
not change that. (Think of passing objects as passing a pointer to a
struct, if you come from the C corner).
| Quote: | So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
|
The convention is that only the Class internals should care. Noone
outside the class should even know about the private members of the
class which may be affected by a method call. As for documentation
within the class, there are no hard-and-fast rules for this, AFAIK. Just
remember that you should not depend on internal state. (For example, you
should not care if a method getResource() just returns a reference to
one of the class' fields, or computes an entirely new Resource. This may
even change!)
|
|
| Back to top |
|
 |
Matt Humphrey Guest
|
Posted: Wed Sep 15, 2004 11:33 am Post subject: Re: Problem with lack of pass-by-result |
|
|
"Richard" <rh310 (AT) hotmail (DOT) com> wrote
| Quote: | Java newbie.
As a Java newbie, I'm charged with learning the language by
maintaining a pretty good-sized pile of code.
The conceptual problem I'm having is with the C convention of using
pass-by-result (and pass-by-value-result) function parameters to
readily show which functions use and change variables outside of the
function scope.
In Java, pass-by-result vars are supposedly verbotem (although check
BufferInputStream.read(byte[] b, int off, int len) for at least one
exception to that rule that I've seen so far).
|
Java very consistently (with no exceptions) follows the rule of
pass-by-value where the value of an object is its reference. All objects
including arrays of primitives (byte [], int []) are represented by a
reference to their associated data. These references are passed by value as
input-only parameters. The references may not be changed, but the contents
to which they refer may be. This concept is fundamental to understanding
Java. Any object that is passed may have its contents modified, but the
reference to the object itself cannot be changed as a parameter. In the
read () method above, the (reference to) the array of bytes is passed in and
the method fills out the contents of the the array. Note that Java
primitives (int, short, byte, long, char) are not objects. There are no
references to primitives.
http://mindprod.com/jgloss/callbyvalue.html
http://mindprod.com/jgloss/callbyreference.html
| Quote: |
So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
|
Method documentation indicates whether the objects refered to by parameters
are intended to be modified.
Cheers,
Matt Humphrey [email]matth (AT) ivizNOSPAM (DOT) com[/email] http://www.iviz.com/
|
|
| Back to top |
|
 |
Richard Guest
|
Posted: Wed Sep 15, 2004 8:29 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
[email]terra (AT) spacetime (DOT) de[/email] wrote...
| Quote: | Richard wrote:
Java newbie.
As a Java newbie, I'm charged with learning the language by
maintaining a pretty good-sized pile of code.
Good luck
|
It's been an "interesting" three months, that's for sure.
| Quote: | The conceptual problem I'm having is with the C convention of using
pass-by-result (and pass-by-value-result) function parameters to
readily show which functions use and change variables outside of the
function scope.
In Java, you usally do not care outside of the classes implementation.
Each of them should be self-contained, not showing any of the internal
state to the "outside world".
|
Understood. Some of the classes I have to maintain are very large,
and their class variables are modified by several different methods.
It's not an impossible problem, I just have a lot of documentation on
what's changing what to create and maintain. Feels primitive. :)
| Quote: | In Java, pass-by-result vars are supposedly verbotem (although check
BufferInputStream.read(byte[] b, int off, int len) for at least one
exception to that rule that I've seen so far).
Not really, you pass an Object (an array of bytes) which is passed by
value, just like everything else in java. However, as with all object
references, you can actually _change the state_ of that Object. That you
do this in a much more direct fashion then with most other Objects does
not change that. (Think of passing objects as passing a pointer to a
struct, if you come from the C corner).
|
Now this is very interesting. I did not know this. Time to go write
some sandbox code.
| Quote: | So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
The convention is that only the Class internals should care. Noone
outside the class should even know about the private members of the
class which may be affected by a method call. As for documentation
within the class, there are no hard-and-fast rules for this, AFAIK. Just
remember that you should not depend on internal state. (For example, you
should not care if a method getResource() just returns a reference to
one of the class' fields, or computes an entirely new Resource. This may
even change!)
|
--
What matters most?
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Fri Sep 17, 2004 1:36 am Post subject: Re: Problem with lack of pass-by-result |
|
|
Stefan Schulz coughed up:
| Quote: | Richard wrote:
Java newbie.
As a Java newbie, I'm charged with learning the language by
maintaining a pretty good-sized pile of code.
Good luck ;)
The conceptual problem I'm having is with the C convention of using
pass-by-result (and pass-by-value-result) function parameters to
readily show which functions use and change variables outside of the
function scope.
In Java, you usally do not care outside of the classes implementation.
Each of them should be self-contained, not showing any of the internal
state to the "outside world".
In Java, pass-by-result vars are supposedly verbotem (although check
BufferInputStream.read(byte[] b, int off, int len) for at least one
exception to that rule that I've seen so far).
Not really, you pass an Object (an array of bytes) which is passed by
value, just like everything else in java. However, as with all object
references, you can actually _change the state_ of that Object. That
you do this in a much more direct fashion then with most other
Objects does not change that. (Think of passing objects as passing a
pointer to a struct, if you come from the C corner).
So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
The convention is that only the Class internals should care. Noone
outside the class should even know about the private members of the
class which may be affected by a method call. As for documentation
within the class, there are no hard-and-fast rules for this, AFAIK.
Just remember that you should not depend on internal state. (For
example, you should not care if a method getResource() just returns a
reference to one of the class' fields, or computes an entirely new
Resource. This may even change!)
|
I'll only add that should you /really/ need to be sure that a called routine
is not mucking with your object, you can clone it first, though doing so
without really knowing what's going on is very bad form.
Similarly, if you are writing a method such as:
public void send(Object sentO) {...}
and you really need sentO to stay intact long after your method returns
because you are entering it into a larger structure or what have you, your
method can clone that object first and use the copy.
--
http://www.allexperts.com is a nifty way to get an answer to just about
/anything/.
|
|
| Back to top |
|
 |
iamfractal@hotmail.com Guest
|
Posted: Fri Sep 17, 2004 2:07 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
Richard <rh310 (AT) hotmail (DOT) com> wrote
| Quote: | Java newbie.
So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
|
C newbie.
What's the convention in C?
..ed
www.EdmundKirwan.com - Home of The Fractal Class Composition
|
|
| Back to top |
|
 |
Chris Smith Guest
|
Posted: Fri Sep 17, 2004 3:38 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
Thomas G. Marshall wrote:
| Quote: | I'll only add that should you /really/ need to be sure that a called routine
is not mucking with your object, you can clone it first, though doing so
without really knowing what's going on is very bad form.
|
Yes, with emphasis on the "very bad form" part. Cloning is useful in
situations where you need to make guarantees and can't trust the code
you're executing, such as in managing security while running dynamically
loaded mobile code from an outside source. It's not a reasonable
solution to have a mess on your hands with your own application code.
Doing the latter would be taking the first step to eventually rewriting
the whole application.
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Fri Sep 17, 2004 4:22 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
Chris Smith coughed up:
| Quote: | Thomas G. Marshall wrote:
I'll only add that should you /really/ need to be sure that a called
routine is not mucking with your object, you can clone it first,
though doing so without really knowing what's going on is very bad
form.
Yes, with emphasis on the "very bad form" part. Cloning is useful in
situations where you need to make guarantees and can't trust the code
you're executing, such as in managing security while running
dynamically loaded mobile code from an outside source. It's not a
reasonable solution to have a mess on your hands with your own
application code. Doing the latter would be taking the first step to
eventually rewriting the whole application.
|
You bet. However for some things it is critical, and for others it is
perfect for defensive programming, both of which are my point.
For example, take a look at this method in
mumble/java/awt/GridBagLayout.java:
/**
* Sets the constraints for the specified component in this layout.
* @param comp the component to be modified
* @param constraints the constraints to be applied
*/
public void setConstraints(Component comp, GridBagConstraints constraints)
{
comptable.put(comp, (GridBagConstraints)constraints.clone());
}
Because GridBagLayout needs to use the passed constraints *long after this
method is done with*, it is cloned to prevent the caller from reusing his
constraints object and mucking with the innards and screwing up the layout
down the road.
--
Forgetthesong,I'dratherhavethefrontallobotomy...
|
|
| Back to top |
|
 |
Richard Guest
|
Posted: Fri Sep 17, 2004 5:35 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
[email]iamfractal (AT) hotmail (DOT) com[/email] wrote...
| Quote: | Richard <rh310 (AT) hotmail (DOT) com> wrote
Java newbie.
So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
C newbie.
What's the convention in C?
|
Stylistically, the strongest way I've seen (and used) is passing all
referenced vars as function parameters, possibly with additions to
the prototype that document intended use*. Second strongest way is
to just pass all the changed vars as function parameters. Third
strongest way is to declare externs inside the function body.
* such as:
#define IN
#define OUT
#define INOUT
int function_prototype( int a IN, int * b OUT, int * c INOUT ) ;
Granted there's an awful lot of old C around that doesn't do this,
written without a lot of concern for maintainability (which is OK;
maintainability isn't a globally high priority).
--
Globalists want a world of China's labor rate and US retail prices.
|
|
| Back to top |
|
 |
Richard Guest
|
Posted: Mon Sep 20, 2004 6:22 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
[email]matth (AT) ivizNOSPAM (DOT) com[/email] wrote...
| Quote: |
"Richard" <rh310 (AT) hotmail (DOT) com> wrote in message
news:MPG.1bb0e67c7c6255d3989dfb (AT) news (DOT) verizon.net...
Java newbie.
As a Java newbie, I'm charged with learning the language by
maintaining a pretty good-sized pile of code.
The conceptual problem I'm having is with the C convention of using
pass-by-result (and pass-by-value-result) function parameters to
readily show which functions use and change variables outside of the
function scope.
In Java, pass-by-result vars are supposedly verbotem (although check
BufferInputStream.read(byte[] b, int off, int len) for at least one
exception to that rule that I've seen so far).
Java very consistently (with no exceptions) follows the rule of
pass-by-value where the value of an object is its reference. All objects
including arrays of primitives (byte [], int []) are represented by a
reference to their associated data. These references are passed by value as
input-only parameters.
|
By which I think you mean that I can't create another Object inside
of the method and assign its reference value to the reference value
of the Object parameter. But I can copy the contents of the new
Object into the paramter Object, using the parameter's reference.
Seems like simply a way to syntactically insure that any change of
Object parameter contents from inside a method is a deliberate act,
rather than the indirection happening as an unplanned side-effect.
| Quote: | The references may not be changed, but the contents
to which they refer may be. This concept is fundamental to understanding
Java. Any object that is passed may have its contents modified, but the
reference to the object itself cannot be changed as a parameter. In the
read () method above, the (reference to) the array of bytes is passed in and
the method fills out the contents of the the array. Note that Java
primitives (int, short, byte, long, char) are not objects. There are no
references to primitives.
http://mindprod.com/jgloss/callbyvalue.html
http://mindprod.com/jgloss/callbyreference.html
So, what's the Java convention for documenting which class-scope
variables (those declared outside of methods) are used and--
specificially--CHANGED by which methods?
Method documentation indicates whether the objects refered to by parameters
are intended to be modified.
Cheers,
Matt Humphrey [email]matth (AT) ivizNOSPAM (DOT) com[/email] http://www.iviz.com/
|
--
Globalists want a world of China's labor rate and US retail prices.
|
|
| Back to top |
|
 |
Thomas G. Marshall Guest
|
Posted: Mon Sep 20, 2004 7:35 pm Post subject: Re: Problem with lack of pass-by-result |
|
|
Richard coughed up:
| Quote: | matth (AT) ivizNOSPAM (DOT) com wrote...
|
....[rip]...
| Quote: | Java very consistently (with no exceptions) follows the rule of
pass-by-value where the value of an object is its reference. All
objects including arrays of primitives (byte [], int []) are
represented by a reference to their associated data. These
references are passed by value as input-only parameters.
By which I think you mean that I can't create another Object inside
of the method and assign its reference value to the reference value
of the Object parameter. But I can copy the contents of the new
Object into the paramter Object, using the parameter's reference.
|
Sure, but regardless, it is still /pass by value/. And you are not allowed
to copy all object contents: the reflection package restricts you based upon
member scope. And as I stated in another ng, even if you /had/ the ability
to copy full contents of one object to another's reference, bit for bit, it
would /still/ be PBV because:
Inside the method, the formal parameter when used
as the LHS of an equation /cannot/ affect the actual
parameter.
| Quote: | Seems like simply a way to syntactically insure that any change of
Object parameter contents from inside a method is a deliberate act,
rather than the indirection happening as an unplanned side-effect.
|
I'd argue that it's not a syntactic issue, but a paradigm one. And
paradigms by themselves carry syntactic rules, but not necessarily the other
way around.
....[rip]...
--
http://www.allexperts.com is a nifty way to get an answer to just about
/anything/.
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|