<p>getEmployeeNameByBatchId(int batchID)<BR>
getEmployeeNameBySSN(Object SSN)<BR>
getEmployeeNameByEmailId(String emailID)<BR>
getEmployeeNameBySalaryAccount(SalaryAccount salaryAccount)<BR></p>

<p>or</p>

<p>getEmployeeName(int typeOfIdentifier, byte[] identifier) -> In this methods the typeOfIdentifier tells if identifier is batchID/SSN/emailID/salaryAccount</p>

<p>Which one of the above is better way implement a get method? </p>

<p>These methods would be in a Servlet and calls would be made from an API which would be provided to the customers.</p>

[{'answer_id': 80914, 'author': 'Tim Mooney', 'author_id': 15178, 'author_profile': 'https://Stackoverflow.com/users/15178', 'pm_score': 2, 'selected': False, 'text': '<p>I\'d go with the "many" approach.  It seems more intuitive to me and less prone to error.</p>\n'}, {'answer_id': 80915, 'author': 'Stephan', 'author_id': 11946, 'author_profile': 'https://Stackoverflow.com/users/11946', 'pm_score': 4, 'selected': True, 'text': "<p>Why not overload the getEmployeeName(??) method? <br>\n<br> getEmployeeName(int BatchID)<br>\n<strike>getEmployeeName(object SSN)</strike>(bad idea)<br>\ngetEmployeeName(String Email)<br>\netc.</p>\n\n<p>Seems a good 'many' approach to me.</p>\n"}, {'answer_id': 80916, 'author': 'Raithlin', 'author_id': 6528, 'author_profile': 'https://Stackoverflow.com/users/6528', 'pm_score': 0, 'selected': False, 'text': '<p>I would use the first option, or overload it in this case, seeing as you have 4 different parameter signatures. However, being specific helps with understanding the code 3 months from now.</p>\n'}, {'answer_id': 80920, 'author': 'Aaron Jensen', 'author_id': 11229, 'author_profile': 'https://Stackoverflow.com/users/11229', 'pm_score': 1, 'selected': False, 'text': "<p>First option, no question. Be explicit. It will greatly aid in maintainability and there's really no downside.</p>\n"}, {'answer_id': 80929, 'author': 'larsivi', 'author_id': 14047, 'author_profile': 'https://Stackoverflow.com/users/14047', 'pm_score': 0, 'selected': False, 'text': '<p>The first is probably the best in Java, considering it is typesafe (unlike the other). Additionally, for "normal" types, the second solution seems to only provide cumbersome usage for the user. However, since you are using Object as the type for SSN (which has a semantic meaning beyond Object), you probably won\'t get away with that type of API.</p>\n\n<p>All-in-all, in this particular case I would have used the approach with many getters. If all identifiers have their own class type, I might have gone the second route, but switching internally on the class instead of a provided/application-defined type identifier.</p>\n'}, {'answer_id': 80936, 'author': 'micahwittman', 'author_id': 11181, 'author_profile': 'https://Stackoverflow.com/users/11181', 'pm_score': 0, 'selected': False, 'text': '<p>Is the logic inside each of those methods largely the same?</p>\n\n<p>If so, the single method with identifier parameter may make more sense (simple and reducing repeated code).</p>\n\n<p>If the logic/procedures vary greatly between types, a method per type may be preferred.</p>\n'}, {'answer_id': 80942, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>@Stephan: it is difficult to overload a case like this (in general) because the parameter types might not be discriminative, e.g.,</p>\n\n<ul>\n<li>getEmployeeNameByBatchId(int batchId)</li>\n<li>getEmployeeNameByRoomNumber(int roomNumber)</li>\n</ul>\n\n<p>See also the two methods getEmployeeNameBySSN, getEmployeeNameByEmailId in the original posting.</p>\n'}, {'answer_id': 80943, 'author': 'Adam Vigh', 'author_id': 1613872, 'author_profile': 'https://Stackoverflow.com/users/1613872', 'pm_score': 0, 'selected': False, 'text': "<p>As others suggested the first option seems to be the good one. The second might make sense when you're writing a code, but when someone else comes along later on, it's harder to figure out how to use code. ( I know, you have comments and you can always dig deep into the code, but GetemployeeNameById is more self-explanatory)</p>\n\n<p>Note: Btw, usage of Enums might be something to consider in some cases.</p>\n"}, {'answer_id': 80954, 'author': 'Javaxpert', 'author_id': 15241, 'author_profile': 'https://Stackoverflow.com/users/15241', 'pm_score': -1, 'selected': False, 'text': '<p>You are thinking C/C++.</p>\n\n<p>Use objects instead of an identifier byte (or int).</p>\n\n<p><strong>My Bad, the overload approach is better and using the SSN as a primary key is not so good</strong></p>\n\n<pre><code>public ??? getEmployeeName(Object obj){\n\nif (obj instanceof Integer){\n\n  ...\n\n} else if (obj instanceof String){\n\n...\n\n} else if .... // and so on\n\n\n} else throw SomeMeaningFullRuntimeException()\n\nreturn employeeName\n}\n</code></pre>\n\n<p>I think it is better to use Unchecked Exceptions to signaling incorrect input.</p>\n\n<p>Document it so the customer knows what objects to expect. Or create your own wrappers.  I prefer the first option.</p>\n'}, {'answer_id': 80956, 'author': 'user9116', 'author_id': 9116, 'author_profile': 'https://Stackoverflow.com/users/9116', 'pm_score': 0, 'selected': False, 'text': "<p>In a trivial case like this, I would go with overloading. That is:</p>\n\n<pre><code>getEmployeeName( int batchID );\ngetEmployeeName( Object SSN );\n\netc.\n</code></pre>\n\n<p>Only in special cases would I specify the argument type in the method name, i.e. if the type of argument is difficult to determine, if there are several types of arguments tha has the same data type (batchId and employeeId, both int), or if the methods for retrieving the employee is radically different for each argument type.</p>\n\n<p>I can't see why I'd ever use this</p>\n\n<pre><code>getEmployeeName(int typeOfIdentifier, byte[] identifier)\n</code></pre>\n\n<p>as it requires both callee and caller to cast the value based on typeOfIdentifier. Bad design.</p>\n"}, {'answer_id': 80960, 'author': 'FrankS', 'author_id': 3134, 'author_profile': 'https://Stackoverflow.com/users/3134', 'pm_score': 0, 'selected': False, 'text': '<p>If you rewrite the question you can end up asking:</p>\n\n<p>"SELECT name FROM ... "<br>\n"SELECT SSN FROM ... "<br>\n"SELECT email FROM ... "<br>\nvs.<br>\n"SELECT * FROM ..."</p>\n\n<p>And I guess the answer to this is easy and everyone knows it.</p>\n\n<p>What happens if you change the Employee class? E.g.: You have to remove the email and add a new filter like department. With the second solution you have a huge risk of not noticing any errors if you just change the order of the int identifier "constants".\nWith the first solution you will always notice if you are using the method in some long forgotten classes you would otherwise forget to modify to the new identifier.</p>\n'}, {'answer_id': 80962, 'author': 'm_pGladiator', 'author_id': 446104, 'author_profile': 'https://Stackoverflow.com/users/446104', 'pm_score': 2, 'selected': False, 'text': '<p>The methods are perfect example for usage of overloading. </p>\n\n<pre><code>getEmployeeName(int batchID)\ngetEmployeeName(Object SSN)\ngetEmployeeName(String emailID)\ngetEmployeeName(SalaryAccount salaryAccount)\n</code></pre>\n\n<p>If the methods have common processing inside, just write one more getEmplyeeNameImpl(...)  and extract there the common code to avoid duplication</p>\n'}, {'answer_id': 81009, 'author': 'Sietse', 'author_id': 6400, 'author_profile': 'https://Stackoverflow.com/users/6400', 'pm_score': 2, 'selected': False, 'text': "<p>I don't like getXByY() - that might be cool in PHP, but I just don't like it in Java (ymmv).</p>\n\n<p>I'd go with overloading, unless you have properties of the same datatype. In that case, I'd do something similar to your second option, but instead of using ints, I'd use an Enum for type safety and clarity. And instead of byte[], I'd use Object (because of autoboxing, this also works for primitives).</p>\n"}, {'answer_id': 81013, 'author': 'Philip Helger', 'author_id': 15254, 'author_profile': 'https://Stackoverflow.com/users/15254', 'pm_score': 0, 'selected': False, 'text': '<p>I personally prefer to have the explicit naming "...ByRoomNumber" because if you end up with many "overloads" you will eventually introduce unwanted errors. Being explicit is imho the best way.</p>\n'}, {'answer_id': 81018, 'author': 'jrudolph', 'author_id': 7647, 'author_profile': 'https://Stackoverflow.com/users/7647', 'pm_score': 3, 'selected': False, 'text': '<p>You could use something like that:</p>\n\n<pre><code>interface Employee{\n    public String getName();\n    int getBatchId();\n}\ninterface Filter{\n    boolean matches(Employee e);\n}\npublic Filter byName(final String name){\n    return new Filter(){\n        public boolean matches(Employee e) {\n            return e.getName().equals(name);\n        }\n    };\n}\npublic Filter byBatchId(final int id){\n    return new Filter(){\n        public boolean matches(Employee e) {\n            return e.getBatchId() == id;\n        }\n    };\n}\npublic Employee findEmployee(Filter sel){\n    List&lt;Employee&gt; allEmployees = null;\n    for (Employee e:allEmployees)\n        if (sel.matches(e))\n            return e;\n    return null;\n}\npublic void usage(){\n    findEmployee(byName("Gustav"));\n    findEmployee(byBatchId(5));\n}\n</code></pre>\n\n<p>If you do the filtering by an SQL query you would use the <code>Filter</code> interface to compose a WHERE clause.</p>\n\n<p>The good thing with this approach is that you can combine two filters easily with:</p>\n\n<pre><code>public Filter and(final Filter f1,final Filter f2){\n    return new Filter(){\n        public boolean matches(Employee e) {\n            return f1.matches(e) &amp;&amp; f2.matches(e);\n        }\n    };\n}\n</code></pre>\n\n<p>and use it like that:</p>\n\n<pre><code>findEmployee(and(byName("Gustav"),byBatchId(5)));\n</code></pre>\n\n<p>What you get is similar to the <code>Criteria</code> API in Hibernate.</p>\n'}, {'answer_id': 81049, 'author': 'timvw', 'author_id': 15267, 'author_profile': 'https://Stackoverflow.com/users/15267', 'pm_score': 1, 'selected': False, 'text': '<p>Sometimes it can be more conveniant to use the <a href="http://en.wikipedia.org/wiki/Specification_pattern" rel="nofollow noreferrer">specification pattern</a>.</p>\n\n<p>Eg: GetEmployee(ISpecification&lt;Employee&gt; specification)</p>\n\n<p>And then start defining your specifications...</p>\n\n<p>NameSpecification : ISpecification&lt;Employee&gt;<br/>\n{<br/>\n private string name;<br/>\n public NameSpecification(string name) { this.name = name; }<br/>\n public bool IsSatisFiedBy(Employee employee) { return employee.Name == this.name; }<br/>\n}<br/>\n<br/>\nNameSpecification spec = new NameSpecification("Tim");<br/>\nEmployee tim = MyService.GetEmployee(spec);<br/></p>\n'}, {'answer_id': 81083, 'author': 'paercebal', 'author_id': 14089, 'author_profile': 'https://Stackoverflow.com/users/14089', 'pm_score': 0, 'selected': False, 'text': '<p><strong>I agree with Stephan: One task, one method name</strong>, even if you can do it multiple ways.\nMethod overloading feature was provided exactly for your case.</p>\n\n<ul>\n<li>getEmployeeName(int BatchID)</li>\n<li>getEmployeeName(String Email)</li>\n<li>etc.</li>\n</ul>\n\n<p>And <strong>avoid</strong> your second solution at all cost. It smells like "thy olde void * of C". Likewise, passing a Java "Object" is almost as poor style as a C "void *".</p>\n'}, {'answer_id': 81119, 'author': 'Henry B', 'author_id': 6414, 'author_profile': 'https://Stackoverflow.com/users/6414', 'pm_score': 0, 'selected': False, 'text': "<p>If you have a good design you should be able to determine if you can use the overloading approach or if you're going to run into a problem where if you overload you're going to end up having two methods with the same parameter type.</p>\n\n<p>Overloading seems like the best way initially, but if you end up not being able to add a method in future and messing things up with naming it's going to be a hassle.</p>\n\n<p>Personally I'd for for the approach of a unique name per method, that way you don't run into problems later with trying to overload the same parameter Object methods. Also, if someone extended your class in the future and implemented another void getEmployeeName(String name) it wouldn't override yours.</p>\n\n<p>To summarise, go with a unique method name for each method, overloading can only cause problems in the long run.</p>\n"}, {'answer_id': 81218, 'author': 'adriaanp', 'author_id': 12230, 'author_profile': 'https://Stackoverflow.com/users/12230', 'pm_score': 1, 'selected': False, 'text': '<p>I will use explicit method names. Everyone that maintains that code and me later will understand what that method is doing without having to write xml comments.</p>\n'}, {'answer_id': 81712, 'author': 'Krakkos', 'author_id': 15533, 'author_profile': 'https://Stackoverflow.com/users/15533', 'pm_score': -1, 'selected': False, 'text': '<p>stick all your options in an enum, the have something like the following</p>\n\n<pre><code>GetEmployeeName(Enum identifier)\n{\n    switch (identifier)\n    case eBatchID:\n    {\n        // Do stuff\n    }\n    case eSSN:\n    {\n    }\n    case eEmailId:\n    {\n    }\n    case eSalary:\n    {\n    }\n    default:\n    {\n        // No match\n        return 0;\n    }\n}\n\nenum Identifier\n{\n    eBatchID,\n    eSSN,\n    eEmailID,\n    eSalary\n}\n</code></pre>\n'}, {'answer_id': 83054, 'author': 'Paralife', 'author_id': 11906, 'author_profile': 'https://Stackoverflow.com/users/11906', 'pm_score': 0, 'selected': False, 'text': '<p>The decoupling between the search process and the search criteria jrudolf proposes in his example is excellent. I wonder why isnt it the most voted solution. Do i miss something?</p>\n'}, {'answer_id': 88858, 'author': 'moffdub', 'author_id': 10759, 'author_profile': 'https://Stackoverflow.com/users/10759', 'pm_score': 0, 'selected': False, 'text': '<p>I\'d go with <a href="http://martinfowler.com/eaaCatalog/queryObject.html" rel="nofollow noreferrer">Query Objects</a>. They work well for accessing tables directly. If you are confined to stored procedures, they lose some of their power, but you can still make it work.</p>\n'}]
['https://Stackoverflow.com/questions/80892', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15264/']