sashank
sashank

Reputation: 1541

Which method signature is good and why?

Given an IP Address Range ( a.b.c.d - a.b.c.e) i would like a method to return the ip address's in an array list between the range.

Option 1 :

public static int getIPAddressesFromRange(String rangeStr, List list ) ;

return value is count and the input list would be populated with list of IP's the range has

Option 2:

public static List getIPAddressesFromRange(String rangeStr)

return value is the list of ip addresses'

My Option is 2, but that is intuition, not able to support my argument though.

Edit: Is there any design principle the option 1 is violation ?

Upvotes: 0

Views: 178

Answers (11)

Platinum Azure
Platinum Azure

Reputation: 46193

Your second option is best because the first option has two problems:

  1. It's redundant. If a List is returned, you can use its size() method to get that count, so you gain nothing by returning the count.
  2. The list must be validated and in some cases the method outright cannot perform its work. If the caller passes null, there is danger of a NullPointerException being thrown if the code was not written carefully. Also in that case, reassigning the parameter to point to a new list will not be observed by the caller, so your only remotely sane option is to throw a clear exception. With the second option, you have full control of the list until it is returned to the caller.

Upvotes: 2

user988106
user988106

Reputation:

It depends on whether you want to fill pre-created lists, or create new ones.

For example: You could do multiple calls to your function using the same List object to save some memory.

Or: To compare multiple lists, you may want to return a new List for each call.

I would go with Option 2.

Upvotes: 0

Anuj Mehta
Anuj Mehta

Reputation: 1138

Why do you want to return count in first method? You can fetch the number of IP's from List itself.

Second method should be the preferred one

Upvotes: 2

cporte
cporte

Reputation: 2191

I think the 2nd one is better :

  • The count is the size of the list
  • You don't have to give a list to the function
  • Less null pointer exception risk

Upvotes: 1

Piotr Buda
Piotr Buda

Reputation: 353

IMO method signature suggests it will return a list of ip addresses from range, not how many addresses are in this range, hence I'm also for option 2.

Upvotes: 1

kyiu
kyiu

Reputation: 1976

My opinion is that the second method signature is generally the best one as the first one will exposes your list object to concurrent modification. Thus, at the end of your method, it may hold less, more, other objects than expected.

Upvotes: 0

Joonas Pulakka
Joonas Pulakka

Reputation: 36577

Stuff in, stuff out. That's what your Option 2 does.

Option 1 mutates its input argument and returns redundant value (count, which can be got from the list).

Another thing is, perhaps a range of IP addresses would be described better by some other type than a String.

Upvotes: 1

nomoa
nomoa

Reputation: 1052

Your intuition is mine also, it is better to let getIPAddressesFromRange use its preferred implementation of List and avoid someone to give you an already populated list.

Upvotes: 0

Tamás
Tamás

Reputation: 48071

I'd say

public static List<String> getIPAddressesFromRange(String rangeStr)

if you decide to represent IP addresses as strings.

Arguments against #1:

  • The caller needs to construct the list in advance
  • It is not straightforward what the return value is unless you document it
  • The method mutates one of its arguments, which is not in general forbidden, but it is best to avoid surprising the user of your API (especially if they are prone not to read the documentation)
  • Passing in a null value accidentally for the list parameter will result in a NullPointerException.
  • You can always get the length of the list from the list itself if you really care about it.

Upvotes: 5

amit
amit

Reputation: 178471

Option two is probably better, since it is clear for any reader what is the method returning.
Method 1 might cause future coders to spend time thinking what is this parameter (unless it is properly documented), while method 2 is realy straight forward.

Option two also makes it more neat if you later need to iterate on the retrieved list, no need for temporary variables:

for (Object o : getIPAddressesFromRange(String rangeStr)) { ... }

You should also prefer using the generic type List<> and not the raw type.

Upvotes: 1

Mesop
Mesop

Reputation: 5263

Prefer the option 2 to the option 1.

The list contains its count anyway, so there is no need to return two values (the count and the list).

Also, since you know the type of the list, you can use generics: List<String>.

Finally, you might also consider taking two arguments: the beginning and the end of the range.

Upvotes: 2

Related Questions