This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

programming practise

 
shruti dighe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is allowing function to change parameters a good programming practise ? Please help me out
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15367
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

I think most people find it bad practice to change the value of a parameter inside a method. That's because it makes it more difficult to understand the code in the method. Programmers generally expect that the parameter variables that are passed to the method, contain the values that we passed to the method when it was called. If, somewhere inside a method, you change the value, then it makes it harder to follow what value exactly the parameter variable has at any point in the method, making the code hard to follow.
 
shruti dighe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong .. Please help me select the right answer for this question

Q: Which of the following is a good programming practise
1: Declare all variables globally
2: provide comments to each statement of the program
3: function modifies its parameters
4: none of these

options 1 and 2 definitely not correct.
 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shruti dighe wrote:Jesper de Jong .. Please help me select the right answer for this question

This looks like a homework question to me, and here we like you to DoYourOwnHomework (←click).

My suggestion: Post what you think the answer is, AND your reasons for choosing it. Then we might be able to help you.

Winston
 
shruti dighe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
function are written to perform some task .. it is obvious they will change the parameters when it comes to calculations performed inside that function. How can it be a bad programming practise then ?
in that case then all variables in java would have been declared final . and the "printstacktrace" method can always give the stack condition in case of exception..
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shruti

Parameter variables are used to hold the value passed from the calling function. Considering your question about calculated results, you can always have method local variables that holds the calculated results. Correct me if I am wrong.
 
Darryl Burke
Bartender
Posts: 5132
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shruti dighe, please BeForthrightWhenCrossPostingToOtherSites <- click
http://www.java-forums.org/new-java/67999-good-programming-practise.html
 
Winston Gutkowski
Bartender
Pie
Posts: 10427
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shruti dighe wrote:function are written to perform some task .. it is obvious they will change the parameters when it comes to calculations performed inside that function...

It is? Then why does Java allow you to put the qualifier final on a parameter? I think you're confusing using values to perform a calculation, which may indeed produce a different value, and changing those values.

And you still haven't answered the question: What do you think the answer is, and why?

Winston
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic