Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thoughts about find

 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranchers,

this is the method-signature of my find-method:

public int[] find(String[] criteria);

And the assignment says:

Returns an array of record numbers that match the specified criteria.

My find-implementation returns null, when no record is found. Is this correct, or could this lead to an automatic failure or to a deduction of my score?

The assignment says only "Returns an array of record numbers that match the specified criteria" and NOT "Returns an array of record numbers that match the specified criteria, else returns null".

Thus I am not sure, whether I MUST return an empty int-array when no record matches the criteria.

I think, it's a good design to return a null-value when a create-, build- or find-method wasn't successful, but I don't know if I fulfill the requirements in this specific case.

Could anyone help me, please?

Regards,

Oliver
 
T. Anthony Chen
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't go wrong with returning an empty array. Returning null is little risky. You don't know if Sun runs any automatic test and if they expect a null or not. But, I guess if you really want to return null you must document that.
 
Anton Golovin
Ranch Hand
Posts: 527
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oliver Roell:
Hello ranchers,

this is the method-signature of my find-method:

public int[] find(String[] criteria);

And the assignment says:

Returns an array of record numbers that match the specified criteria.

My find-implementation returns null, when no record is found. Is this correct, or could this lead to an automatic failure or to a deduction of my score?

The assignment says only "Returns an array of record numbers that match the specified criteria" and NOT "Returns an array of record numbers that match the specified criteria, else returns null".

Thus I am not sure, whether I MUST return an empty int-array when no record matches the criteria.

I think, it's a good design to return a null-value when a create-, build- or find-method wasn't successful, but I don't know if I fulfill the requirements in this specific case.

Could anyone help me, please?

Regards,

Oliver



I think if you document your decision, you will be ok. If they run software to check on this, returning a non-empty int[] array will not lead to any good results as well. It's up to you what the method returns.
 
mike acre
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver,

One of the golden rules is if a method returns a collection/array and logically you are saying I don't have any elements to return, then you return an empty array/collection. Null is a bad decision. Null may be right if you are saying something other than I have no elements to return, maybe some error condition, though obviously exceptions would be the usual way to go in that case.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> One of the golden rules is

In Effective Java, Joshua Bloch calls it an idiom - Item 27: Return zero-length arrays, not nulls.
 
Anton Golovin
Ranch Hand
Posts: 527
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Marlene Miller:
>> One of the golden rules is

In Effective Java, Joshua Bloch calls it an idiom - Item 27: Return zero-length arrays, not nulls.


final int[] integer = new int[0]; does compile. Well, that's it, then - zero-length arrays.
 
peter wooster
Ranch Hand
Posts: 1033
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Oliver Roell:
Hello ranchers,

this is the method-signature of my find-method:

public int[] find(String[] criteria);

And the assignment says:

Returns an array of record numbers that match the specified criteria.

My find-implementation returns null, when no record is found. Is this correct, or could this lead to an automatic failure or to a deduction of my score?

The assignment says only "Returns an array of record numbers that match the specified criteria" and NOT "Returns an array of record numbers that match the specified criteria, else returns null".

Thus I am not sure, whether I MUST return an empty int-array when no record matches the criteria.

I think, it's a good design to return a null-value when a create-, build- or find-method wasn't successful, but I don't know if I fulfill the requirements in this specific case.

Could anyone help me, please?

Regards,

Oliver


Definitely go for the empty array. The expression result.length should tell the user how many hits there were. You should only return null if you somehow want to distinguish that result form a normal case of no records found. Eg. if the RandomAccessFile instance was null you could return null, but in that case throwing an exception is much cleaner.

I know the interface doesn't provide a checked exception, I use a runtime exception called DataException to signal fatal errors such as IOException in the DBAccess and Data methods. I also let standard runtime exceptions such as NullPointerException to the job where they are easy to generate.

If you want to stick strictly to the interface and only throw the provided exceptions, you should reserve the null result for the case where an exception would have been thrown if you could. I tried this approach and the results are quite ugly.
 
Oliver Rensen
Ranch Hand
Posts: 109
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ranch hands,

thank you very much for your quick and meaningful answers!

I will change my null-return-values in empty arrays.

Regards,

Oliver
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic