title: Javascript: Using pattern Null object pattern, RORO, undefined etc. as returning parameter for a Get-function in case requested object „not found“
body: <p>First of all, I’m not sure whether the topic belongs to StackOverflow or not. However, I’ve found some discussions regarding other programming languages that addresses the topic on StackOverflow but not for Javascript. So, I’m just going to throw my question into the ring:</p>
  
  <p><strong>What is the best way/pattern to handle a possible „not found“ scenario in hypothetical GetUserByUsername function in Javascript.</strong></p>
  
  <p>Why I’m asking? I want to follow a certain coding standard throughout my project. </p>
  
  <p>My more specific scenario the following:</p>
  
  <ul>
  <li>I have class User </li>
  <li>I have a list of user which contains instances of
  the class User </li>
  <li>I look up for a certain user but this user doesn’t
  exist in the list</li>
  </ul>
  
  <p>How should I handle that “not find” situation in my regarding function?</p>
  
  <p>I found the following approaches:</p>
  
  <ul>
  <li>Return undefined</li>
  <li>RORO (Receive an object, return an object)</li>
  <li>Null object pattern</li>
  <li>Throwing a custom error</li>
  </ul>
  
  <p>So what do I mean exactly. Taking the following example class User.</p>
  
  <pre><code>module.exports = class User {
    constructor() {   
      this.userName = null,
      this.name = null,
      this.age = null
    }
    ...
    getName() {
      console.log(this.name);
    }
  }
  </code></pre>
  
  <p>Somewhere in my code I have a function that provides the functionality to return a certain user by its user name from a list of users. That list is an array what consists of instances of the class User.</p>
  
  <p>A simple implementation could like that:</p>
  
  <pre><code>{
    ...
  
    getUserByUsername(pUserName) {
      /* listOfUsers array with objects of the class User */
      var lUser = this.listOfUsers.find(user =&gt; user.userName === pUserName);
      return lUser;
    }
    ...
  }
  </code></pre>
  
  <p>In some case it could be possible that the passed user name doesn’t belong to a user. Therefore no user is found. </p>
  
  <p>How could I handle it?</p>
  
  <p>1.) Return undefined - like the example code does. </p>
  
  <pre><code>/* We expect "Unknown" is not a user name in that list*/
  var lRequestedUser = getUserByUsername("Unknown");
  
  /* Check if requested user is undefined. Note: I'm using lodash for staff like that */
  if(_.undefined(lRequestedUser)) {
    //Handling 
  }
  
  lRequestedUser.getName();
  </code></pre>
  
  <p>2.) RORO (Receive an object, return an object)
  I’m passing an object to my function and I’m returning an object as result. Within the object I’m returning, I have an indicator what shows if the operation was successful. </p>
  
  <pre><code>getUserByUsername(pParams) {
    if(_.undefined(pParams.userName)) {
      //Throw custom error 
    }
  
    var lReturnObject = {
      isSuccessfull: false,
      data: null
    }
  
    /* listOfUsers array with objects of the class User */
    var lUser = this.listOfUsers.find(user =&gt; user.userName === pParams.userName);
  
    if(!(_.undefined(lUser))) {
      lReturnObject.isSuccessfull = true;
      lReturnObject.data = lUser
    }
    return lUser;
  }
  ...
  }
  
  /* 
  Building a object which is passed to the function. Username is a member of that object
   */
  var lRequest = {
    userName: "Unknown"
  }
  
  /* We expect "Unknown" is not a user name in that list*/
  var lRequestedUser = getUserByUsername(lRequest);
  
  if(!(lRequestedUser.isSuccessfull)) {
  //Handling 
  }
  
  lRequestedUser.data.getName();
  </code></pre>
  
  <p>3.) Null object pattern
  The thing I don’t like about that solution is the fact, that I always have to enhance the class for the null objects if the main class gets an additional function. </p>
  
  <pre><code>  module.exports = class NullUser {
        constructor() {   
          this.userName = null,
          this.name = null,
          this.age = null
        }
        ...
        getName() {
          console.log(this.name);
        }
      }
  
  
  {
    ...
  
    getUserByUsername(pUserName) {
      /* listOfUsers array with objects of the class User */
      var lUser = this.listOfUsers.find(user =&gt; user.userName === pUserName);
  
      if(_.undefined(lUser)) {
        return new NullUser();
      }
  
      return lUser;
    }
    ...
  }
  
  
  /* We expect "Unknown" is not a user name in that list*/
  var lRequestedUser = getUserByUsername("Unknown");
  lRequestedUser.getName();
  </code></pre>
  
  <p>4.) Throwing a custom error</p>
  
  <pre><code>{
    ...
    getUserByUsername(pUserName) {
      /* listOfUsers array with objects of the class User */
      var lUser = this.listOfUsers.find(user =&gt; user.userName === pUserName);
  
      if(_.undefined(lUser)) {
        throw new CustomError(...);
      }
  
      return lUser;
    }
    ...
  }
  
  /* We expect "Unknown" is not a user name in that list*/
  try {
    var lRequestedUser = getUserByUsername("Unknown");
    lRequestedUser.getName();
  } catch (e) {
    if (e instanceof CustomError) {
        //Handling
    }
  }
  </code></pre>
  
  <p>Personally I prefer the option to return an object, which contains an indicator, and the option of throwing a custom error. The example is synchronous. In my projects I also have asynchronous code. I’m using promises for that.</p>
  
  <p>So any thoughts that would be the “best” solution or the way I should prefer?</p>