Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3318688
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:44:10+00:00 2026-05-17T22:44:10+00:00

This is the second time I found myself writing this kind of code, and

  • 0

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 🙁

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:44:10+00:00Added an answer on May 17, 2026 at 10:44 pm

    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.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've a application that must work after another application. This second application has a
I found this statement is some old code and it took me a second
I've got a Perl script that needs to execute another Perl script. This second
I've seen second one in another's code and I suppose this length comparison have
This is a second-hand question from an OS development site, but it made me
This most be the second most simple rollover effect, still I don't find any
Despite this being one of the best error messages I've ever seen (second only
How do I add a second item to order by with this? I want
Can someone explain this result to me. The first test succeeds but the second
Sorry for the second newbie question, I'm a developer not a sysadmin so this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.