title: Best practices coding c++
body: <p>I'm wondering, is there a better way to write code when there are functions with status returns.</p>
  
  <p>Below is an example. (Please ignore simple code errors if there are any. I'm specifically talking about the structure. Plus, I'm at work and don't have a compiler on this computer)</p>
  
  <pre><code>#include "Session.h"
  
  Session::Session(const char * IPaddress, unsigned int openPort)
  {
      ssh_session mySession; 
      hostIP = IPaddress;
      port = openPort;
  }
  
  int Session::cBeginSession()
  {
      try
      {
          int status = ssh_options_set(mySession, SSH_OPTIONS_HOST, &amp;hostIP);
          if (status == 0)
          {
              status = ssh_options_set(mySession, SSH_OPTIONS_LOG_VERBOSITY,                       
                                       SSH_LOG_PROTOCOL);
              if(status == 0)
              {
                  status = ssh_options_set(mySession, SSH_OPTIONS_PORT, &amp;port);
                  if (status == 0)
                  {
                      std::cout &lt;&lt; "Session started\n";
                      return 0;
                  }
                  else
                  {
                      std::cout &lt;&lt; "Unable to set port\n";
                      return -3;
                  }
  
              }
              else
              {
                  std::cout &lt;&lt; "Protocol option log verbosity unable to set\n";
                  return -2;
              }
          }
          else
          {
              std::cout &lt;&lt; "Unable to set Host address\n";
              return -1;
          }
      }
      catch (...) 
      {
          std::cout &lt;&lt; "Unknown exception occurred\n";
          return -8;
      }
  }
  </code></pre>
  
  <p>I typically use if-else statements with the status parameters, but I tend to end up with large nests of if-else statements if there are more than one or two functions involved. Is there a more readable way to write something like this? It turns into a rats nest very quickly.</p>
  
  <p><strong>EDIT: Thank you for all the replies. I think I have some ideas of how to structure my code better. I appreciate all the diligent suggestions.</strong></p>