Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Letting the code try different things until it succeeds, neatly

This is the second time I found myself writing this kind of code, and decided that there must be a more readable way to accomplish this:

My code tries to figure something out, that's not exactly well defined, or there are many ways to accomplish it. I want my code to try out several ways to figure it out, until it succeeds, or it runs out of strategies. But I haven't found a way to make this neat and readable.

My particular case: I need to find a particular type of method from an interface. It can be annotated for explicitness, but it can also be the only suitable method around (per its arguments).

So, my code currently reads like so:

Method candidateMethod = getMethodByAnnotation(clazz);
if (candidateMethod == null) {
  candidateMethod = getMethodByBeingOnlyMethod(clazz);
}
if (candidateMethod == null) {
  candidateMethod = getMethodByBeingOnlySuitableMethod(clazz);
}
if (candidateMethod == null) {
  throw new NoSuitableMethodFoundException(clazz);
}

There must be a better way…

Edit: The methods return a method if found, null otherwise. I could switch that to try/catch logic, but that hardly makes it more readable.

Edit2: Unfortunately, I can accept only one answer :(

like image 517
Henrik Paul Avatar asked Nov 02 '10 07:11

Henrik Paul


People also ask

How do you use try catch in Swift?

try – You must use this keyword in front of the method that throws. Think of it like this: “You're trying to execute the method. catch – If the throwing method fails and raises an error, the execution will fall into this catch block. This is where you'll write code display a graceful error message to the user.

How would you call a function that throws errors and also returns a value?

It's called rethrows . The rethrows keyword is used for functions that don't directly throw an error.

Should you code after work?

Don't get me wrong, developing software is hard work and you are fatigued to a degree. But a body at rest tends to stay at rest while a body in motion tends to stay in motion. If you have a development job and are asking if you should pursue a coding hobby in your free time, then chances are NO, you shouldn't.


1 Answers

To me it is readable and understandable. I'd simply extract the ugly part of the code to a separate method (following some basic principles from "Robert C.Martin: Clean Code") and add some javadoc (and apologies, if necessary) like that:

//...
try {
   Method method = MethodFinder.findMethodIn(clazz);
catch (NoSuitableMethodException oops) {
   // handle exception
}

and later on in MethodFinder.java

/**
 * Will find the most suitable method in the given class or throw an exception if 
 * no such method exists (...)
 */
public static Method findMethodIn(Class<?> clazz) throws NoSuitableMethodException {
  // all your effort to get a method is hidden here,
  // protected with unit tests and no need for anyone to read it 
  // in order to understand the 'main' part of the algorithm.
}
like image 176
Andreas Dolk Avatar answered Sep 18 '22 23:09

Andreas Dolk