• Post Reply Bookmark Topic Watch Topic
  • New Topic

Decide what method returns  RSS feed

 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was writing a method to read a file. I did not think it through and gave the return type as void and modifier as static but I am not sure if it has to be void.

How do you decide the return type of the method? Is there a good rule of thumb in such cases?

Also, does the use of static needs to be done sparingly?
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the static keyword is the exception so it should be unusual. If your method does not use any information from an instance of its class, nor write any information to such an instance, then you should consider making it static. There is a very dubious classification of methods around, so look at that. Does all the information for that method come through its parameters? Does all the information sent back from that method come in its return type? If it has any side‑effects, are they all outwith the instance?

No, there is not a rule of thumb about what a method should return. You have to think, “If I call method, foo, what do I want it to send me back?” Are you going to send a true to indicate successful reading? Are you going to send a List of objects created from the data read? Are you going to return a count of lines read?

It is quite likely that you will have a method which reads a file into a List passed as a parameter. It is quite likely that you will have a method which takes the file name as a parameter (or requests the file name itself). It is quite likely that you will have a method which deals with all its own errors (Exception handling, etc). If that is the case, then it is probably good design to mark that method static, and give it a void return type. It is also probably better to put that method in its own utility class.I think I would prefer to create a List<String> in the method, and return that as an unmodifiable List, in which case the return type of that method would be List<String>.

There are probably several bits in that code unfamiliar to you; if you can't find out about them do ask
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More about static methods: if you are implementing a method from an interface, it is never static.
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am not sure about return type of function, then i might not clear what exactly I need. When designing function, it is important we should know what are the pre-condition before calling function and what client will expect after calling this. This will yield the correct method signature.
Also discuss with team member who will use this function and what they expect after calling this will help.

Regarding static, check whether it make sense to call this function without creating an object first. If its ok then it might be your usecase to define static. I hope it helps.
 
Ahsan Bagwan
Ranch Hand
Posts: 254
1
Java MySQL Database Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell,

That was a thorough comprehensive answer. And how! I did not expect my question would warrant such a thoughtful reply.

Muhammad, Thank you.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're always welcome
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!