3

I have the following code:

public final boolean doesExistById(Long id) {
    return dataAccessObject.findById(id) != null;
}

public final boolean doesExistByName(String name) {
    return dataAccessObject.findByName(name) != null;
}

public final boolean doesExistByDisplayName(String displayName) {
    return dataAccessObject.findByDisplayName(displayName) != null;
}

public final boolean doesExistByWebId(String webId) {
    return dataAccessObject.findByWebId(webId) != null;
}

My Product class has properties id, name, displayName, wedId.
dataAccessObject.findBy____() returns an object of type Product, if it can be found in the data store, or null if it cannot.

I would like to reduce this chunk of code, if possible, because I have many objects that require the doesExist() pattern as above. The client code will only know one of these properties.


A possible solution I thought of would be to do this:

public final boolean doesExist(Long id, String name, String displayName, String webId) {..}

and then call it with null for unknown fields while using if statements to determine which field has a value. But is there another way that is more elegant?

yiwei
  • 4,022
  • 9
  • 36
  • 54
  • 7
    I think what you already have is fine and looks cleanest. – Sotirios Delimanolis Jul 08 '15 at 18:00
  • I don't think you can make that any better. Not even reflection would make this simpler. – EpicPandaForce Jul 08 '15 at 18:02
  • What are you trying to save by combining the methods in one? `doesExist(null, name, null, null)` is far worse than `doesExistByName(name)`, and since you must specify both the value and the attribute (i.e. `id`, `name`, `displayName`, etc.) you might as well embed the attribute into the name of the method. – Sergey Kalinichenko Jul 08 '15 at 18:11
  • @RobertBain your assumption is correct. However, the client code calling these methods doesn't always know all the fields (including `id`), which is why I had to split them up. – yiwei Jul 08 '15 at 18:23
  • Thanks for all the comments guys! I was really stuck thinking of a solution but I guess this is as far as it can go. – yiwei Jul 08 '15 at 18:24
  • It's probably a question for [Code Review Stack Exchange](http://codereview.stackexchange.com). – naXa stands with Ukraine Jul 09 '15 at 15:32

2 Answers2

7

You are recognizing that the "does exist" part of all of these methods is exactly the same, and that makes you want to avoid repeating it, but the "ByXxx" part of these methods is completely different.

What you've got is far better than what you're thinking of doing. Please don't change your method signature to require callers to provide null values for all but one argument. That is highly error-prone, as it provides no compile-time errors for a variety of different ways that people might use the method signature incorrectly.

One thing you might want to consider doing is separating the "does exist" piece of this into its own mechanism:

public final Optional<MyObj> byId(Long id) {
    return Optional.ofNullable(dataAccessObject.findById(id));
}

So instead of saying service.doesExistById(123), you'd say service.byId(123).isPresent(). This represents the same meaning semantically, but it's broken into separate parts, which means you can reuse byId(), byName(), etc., for other purposes where you need the actual object, and not just to know whether it exists.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • 1
    This is a great solution. It requires a rewrite of some more code, but I think it will work out for the better later on. Thanks! – yiwei Jul 08 '15 at 18:26
-2

You can write a method that takes object type I recommend you check out this page. What is reflection and why is it useful?

Community
  • 1
  • 1
kpie
  • 9,588
  • 5
  • 28
  • 50
  • Uh oh. My recommendation is to avoid reflection unless indispensable. It is slow in runtime and breaks the language rules. What you have already is much better. – Daniel Langdon Jul 08 '15 at 18:08
  • How is this even an answer to the specific question asked by the OP? – Sergey Kalinichenko Jul 08 '15 at 18:08
  • ==NULL nine more -- http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java -- > ActionNotFoundException – kpie Jul 08 '15 at 18:10