• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

passing parameters to private methods

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have this:


public class Model
{
private int value;
.
.
.
value = processCollision(value);
.
.
.
private int processCollision(int value)
{
.
.
return value;
}
}


i could just say:
processCollision();
and access the member variable directly
opinions?
[ January 14, 2005: Message edited by: Randall Twede ]
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Randall Twede:
i have this:


i could just say:
processCollision();
and access the member variable directly
opinions?

[ January 14, 2005: Message edited by: Randall Twede ]



Why would you do this though? passing an instance variable as a parameter and operate on it and pass it back? value being an instance variable don't you have direct access to it?
[ January 14, 2005: Message edited by: Udayan Patel ]
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just because it makes it more clear what is going on: that the variable is being modified by the method. thats why im asking, because i realized i dont have to pass that parameter.
 
blacksmith
Posts: 1332
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it depends on whether the function is otherwise coupled with the class' member data.

If it is, the function adjust the state of the object anyway, so skipping the return and modifying directly might be fine.

If it isn't - for example, if it doesn't read or write to other member data at all - I would return the value and not adjust the member data directly. This decreases coupling, which generally reduces the prospect for bugs. In this case, I would also declare the function static to make it clear that it does not change the object state - and perhaps make it easier to factor out and reuse if it turns out to be useful in other classes too.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Randall Twede:
just because it makes it more clear what is going on: that the variable is being modified by the method. thats why im asking, because i realized i dont have to pass that parameter.



I find it likely that not using the argument makes for code that communicates better.

When a method calls another method, ideally those two methods are at different levels of abstraction - the caller contains the more abstract logic, the callee the less abstract details.

In your example, one could imagine a method



This method tells us at a very nice abstract level what is happening - something is moved and after that collisions are processed. When I'm looking at this method, this might be all I'm interested in, any additional detail just obscures this view.

When I want to know how this works in detail, I can look at the implementation of such a method. When I look at processCollision(), I actually *want* to know what exactly this is doing. Not directly accessing the field here actually hides this important fact from me - I need to use additional brain cycles to find out what the real impact of this method on the object is.

This is to a large part speculation from my side, though - well educated speculation, but still speculation. I had to see more of the code to really decide this for me.

I hope that showing you my reasoning helps you come to your own conclusion, though.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Warren Dew:
In this case, I would also declare the function static to make it clear that it does not change the object state - and perhaps make it easier to factor out and reuse if it turns out to be useful in other classes too.



You have a good point.

On the other hand, I'd typically prefer to factor out more than just a static method - a whole class, that not only encapsulates the behaviour, but also the part of the data it is working on. To do that, the method doesn't necessarily need to be static beforehand. It's probably more a matter of taste and personal experience which of the two versions you find easier to refactor.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
warren,

i see what you mean. here is the other method i have that is like that:


public class Model
{
static final String GOODSET = "bcdfgjklmnpqrstvxz";
.
private String input;
.
.
private class Hash
{
int current; // index into input string
.
.
.
private char isGood(char next) // I could eliminate this parameter
{
int index = GOODSET.indexOf(next);
while ((index == -1) && (next != (char)3))
{
current++;
if (current == input.length())
{
next = (char)3;
}
else
{
next = input.charAt(current);
index = GOODSET.indexOf(next);
}
}
return next;
}


the method is already coupled because it acesses not only a member variable of Hash class but also 2 member variables of Model. so im going to eliminate the parameter. thanks guys
[ January 15, 2005: Message edited by: Randall Twede ]
 
I've been selected to go to the moon! All thanks to this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic