• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Best Practices: Using the side effect of a parameter

 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to determine if it is acceptable to use the side effect of a parameter outside of the method it's changed in. For instance:

To answer myself:

0) Comment it. (Is this good enough?)

1) Make someMethod() an instance variable. But then it's visibly to all methods.

2) Make a static inner class that defines a record to return. But then you can get some ugly code like:

 
Bartender
Posts: 1845
10
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My 2 cents:

Comment it. Javadoc it. Name the method appropriately so that it suggests that the parameter will be changed.
For instance: Collections.sort(myList) in the standard Java API does exactly that.

If you do modify your treatment return the result list from the method, then make sure that the list passed in is not changed as a side effect.
If I see: List myMethod(List inputList) I would assume that my inputList is unchanged, and the return value has the result after copying the inputList and modifying it.




 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic