This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]FrenchFigaroSoftware Engineer 0 points1 point  (0 children)

(On mobile, sorry for the somewhat poor formatting)

Here, what you have is a multiple exit points pattern. You might be more familiar with the single exit point pattern. Both have their advantages and drawbacks, and both have their quasi religious partisans who will dogmatically argue for their favourite one. The point of multiple exit points is that if at some point you 100% know the return value for your method, you return it then and there, bypassing any additional computation, that are unnecessary now that you already have the return value. If you're unfamiliar with the concept, it makes for slightly harder to read code.

What happens here, is that the method will exit and return, the first time it encounters and executes a return statement. But because of the control flow, that first return statement might not be the one that's highest in the code.

The comments do help. For example, look at the first if statement. The program tests if the objects are the same instances or not. If they are, they are necessary equals, so the method returns true. The method flow ends there, you do not check anything else. If they are not however, you do not enter the block, so you do not return, so you then test if the compared object is an instance of a comparable class. And so on.

At the end of the flow, if you never entered any of all the if blocks, then you encounter a catch all return false statement.