I'm building a Spring backend. I've got a controller which gets a "search object" - an object with like 10 fields which only one of them should be filled, so the search function (which I did not write but need to make changes to and refactor) is written like this:
if( param1 != null ) user = getUserByParam1(param1);
else if ( param2 != null ) user = getUserByParam2(param2);
.
.
.
else if(lastName != null || lastName != null) user = getUserByName(firstName, lastName);
else user = getUserById(id);
if(user == null) throw costumException;
return user;
Notice the 2 special cases in the end- one of them checks the availability of 2 parameters instead of one and sends them both to same function (which can handle null in one of the fields, but not both), and the default case which assumes an ID is being passed (in case its not- it gets handled by the if (user == null)
check after to throw exception).
Is there a way to refactor this piece of code to be more readable/good looking? Is there any design pattern or a known method I can use to do this? Or is it actually the best way to write this kind of functionality?
I've done a lot of thinking but couldn't find a nicer way to do so. I've had an idea to somehow send the field name that is filled and its value to another function which will "switch-case" on the field name and send the value to the appropriate function, but it doesn't really save much code (as I still need to manually iterate on all fields to find the one which is filled) and I'm not so sure it'll more readable.
I'm also quite new to Java so I do not know all the available APIs and interfaces, maybe there is something you can throw at me for help.