Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

I wonder if there is a better name for a DB API function called existsByUserNameAndPassword() which returns boolean. There are more function called like this: existsByOldId()... It sounds a little bit unperfect... Is there a better concept for naming "exists" functions? I want to keep the "exists" somehow. To use this functions in a very speakable context like:

if ($DB->existsByUserNameAndPassword($name, $password)) {
    // ...
share|improve this question
It seems I cannot comment on your question however you should try Code Review @ codereview.stackexchange.com question maybe suited for that site. –  BOWS Feb 23 '12 at 11:07
Ok, thank you! I forgot about codereview. I'm a little bit confused about those code related stackexchanges... review/programmers/stackoverflow/codegolf/serverfault/unix/... –  powtac Feb 23 '12 at 11:38
Just read the FAQ of each to see what each one is about. –  ChrisF Feb 23 '12 at 12:15
Why do you need password for existence test? –  Codism Feb 24 '12 at 22:50

7 Answers 7

Why not UserExists(name, password)?

I don't think it is necessary to mention in the name of the method that the name and password are the first and second parameters. I can see that looking up at the parameters.

Furthermore, I don't know the language you're using, but by choosing a name without such indication you can have proper method overloading if you decide you need another one (for example UserExists(id))

share|improve this answer
I want to add the parameters as well to the name, because I have several almost similar functions but with different parameters. –  powtac Feb 23 '12 at 11:35
@powtac I'll guess you are using PHP, and I don't know anything about that, though I've just seen there is no proper method overloading in PHP. In Java or C# for example, the convention is that functions performing the same job should be called the same, only the parameter change (unless you have functions with the same signature, in which case you can't do it) –  Jalayn Feb 23 '12 at 11:40
I do use PHP. But I don't want to use overloading since I want to know very clearly what the purpose of used function is. It's just a question of finding the correct name. –  powtac Feb 23 '12 at 12:26
@powtac Yes, I understand. I just wanted to explain why I proposed this name and method overloading. That is why I +1-ed the answer of Trevor Pilley. –  Jalayn Feb 23 '12 at 12:28
There are even better possibilities for method overloading in PHP using the magic __call(): stackoverflow.com/a/4697775/22470 –  powtac Feb 23 '12 at 15:28

How about isValidUser(name,password) ?

share|improve this answer


Two areas come to mind for me that might warrant some additional exploration:

  1. Should this method be part of your DB class, or should it be attached to, say, the User model?

    The DB class might be responsible for executing the query or stored procedure, but should the DB class also be responsible for determining which query/SP to execute?

  2. What is the purpose of checking to see if a username/password combination exists?

    Are there any use cases other than, say, validating login credentials where you would want to query the database to make sure that a specific username/password combination exists?

Perhaps something like the following might be just as understandable:

if( User::isValidCredentials($username, $password) ) {

(or if you prefer a verb, validateCredentials(), perhaps)

See How to use Dependency Injection with Static Methods if you are wondering how to inject the DB adapter into a static method (not PHP-centric, but it should give you some ideas).


If you are looking for polymorphism in PHP, I'm afraid you just won't find it. Because PHP is loosely typed, it's not practical to have this.

There are ways to simulate it (methods revolving around __call(), func_get_args() and/or array parameters come to mind), but all of them introduce maintainability problems:

  • Your function signatures* are no longer self-documenting.
  • In some cases, you can end up with ambiguous arguments (does passing a string value to exists() mean to search by username, email address or UID?).

* i.e., what your teammates' IDEs are going to show them when they invoke your function in their code

share|improve this answer

If you can't do method overloading I'd go for something along these lines:

userExistsWithNameAndPassword(userName, password);

That way you are fully describing the intent of the method and inferring the return type is a boolean value.

share|improve this answer


function checkUser () {
    $params = func_get_args();
    // do stuff 

With func_get_args you are not limited only to username and pass and by this function you'll be able to solve a variety of scenarios without changing the function prototype. The idea of creating a function is to solve a particular case scenario and put the implementation inside. Checking the user is the concept, and the details about how is done are inside.

share|improve this answer
It might just be me but this is really bad mojo -- having functions that do not declare their args and grab it from a param array for convenience is going to create maintenence nightmares. –  Wyatt Barnett Feb 23 '12 at 12:48
hmmm. I prefer this to having existsByUserNameAndPassword(), existsByFirstName(), existsBySession (), existsByWhateverAndElse ().. and so on... a lot of functions doing basically the same thing. I only meant to give an alternative so he can choose. –  Elzo Valugi Feb 23 '12 at 12:56
As an example to build on the point @WyattBarnett made, I often create a method named resolve() on my models whose job is to take an unknown-at-runtime-type and return a corresponding instance of that model (e.g., given a username, return the User record for that user; given a PK ID, return the User record with that ID; etc.). But with some models, I run into huge problems with ambiguity (does passing a string value to resolve() mean searching for a matching username, email address or UID?). Without REALLY good documentation that is constantly kept up-to-date, this can lead to confusion. –  user34530 Feb 24 '12 at 21:11

I think exists is about perfectly semantic here -- I wouldn't change it myself.

share|improve this answer

what about

boolean authenticate(userName, password) 
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.