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

B&S 2.2.2 - Unlock()

 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

If I receive an invalid record number, I would like to throw a RecordNotFoundException, however, this is not declared in the interface signature and hence not possible.
The RecordNotFoundException exception that I have defined extends Exception and not RuntimeException. I would prefer to leave it as a “checked exception”.
Any suggests as to how I can report an invalid recNo please ?

There is another school of thought ... the unlock () should only follow a successful lock () and therefore the recNo should be valid and so not an issue ! I am not too keen on this.

Pete
 
Cless Alvein
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Palmer wrote:There is another school of thought ... the unlock () should only follow a successful lock () and therefore the recNo should be valid and so not an issue ! I am not too keen on this.

Since the JavaDoc comments are in your hands, you can specify what the method does if the record number is invalid. In my opinion it actually makes sense not to throw a RecordNotFoundException because with my assignment (URLyBird 1.3.2) I needed to lock a record to update or delete it. Well, if I lock a record and then delete it, the record number is now invalid! Since it doesn't quite make sense to throw a RecordNotFoundException when the deleted record is unlocked, there were two choices: either I could implicitly unlock a record when it is deleted or I could change the contract (but not the method signature) of the unlock method so that an exception is not thrown unless the record number is clearly incorrect (out of bounds). I ended up choosing the latter, but hopefully this anecdote helps explain why it might not make sense to throw a RecordNotFoundException from the unlock method.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cless,

So what action do you take if the LockManager.unlock () method is invoked with a recNo = 1 and you determine that recNo 1 is not locked ?

This is the scenario that I am pondering over.

Pete
 
Cless Alvein
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then I throw an IllegalStateException.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cless,
Yes that sound very reasonable.

As you are on line ( i hope you don't mind) , another quick question. Would you use "assert" statement to detect conditions like another one I am pondering over


Sorry for dropping this one.

Thanks
Pete
 
Cless Alvein
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A rule of thumb is not to use assert statements except in private methods. Public methods should throw an exception.
At any rate I would say that in the scenario you posted, it is better to use a for-each loop or do where you don't even need to check the map size.
Also.. a stylistic note but isEmpty() is probably preferred to size() > 0.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Cless

thank you for both the clarification & top tips.

Pete
 
Alecsandru Cocarla
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Palmer wrote:So what action do you take if the LockManager.unlock () method is invoked with a recNo = 1 and you determine that recNo 1 is not locked ?

If the record is not locked, I think it's obvious that the cookie can't be valid. So you throw a SecurityException.

It should never be the case that you must throw a RecordNotFoundException from the unlock() method.
 
Pete Palmer
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Evening Alecsandru,

I like this forum. You have viewed the problem scenario from another angle and made me think again and not for the first time.

My view of the scenario was a case of a genuine attempt to unlock a record with what is believed to be a valid record number & cookie value. For example after a lock -> update -> unlock. However, in the lock(), the record number is incorrect. I think raising the IllegalStateException exception as suggested by Cless is good, as this condition should not arise. If the question is asked as to how we get into this state, then the answer is more likely to be due to implementation bug, which needs to be rectified and then the condition should not arise.

Which than really leaves your view which after thinking over again, sounds more plausible in this case.

Thank you.


Pete

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic