Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices coding c++ [closed]

I'm wondering, is there a better way to write code when there are functions with status returns.

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)

#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, &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, &port);
                if (status == 0)
                {
                    std::cout << "Session started\n";
                    return 0;
                }
                else
                {
                    std::cout << "Unable to set port\n";
                    return -3;
                }

            }
            else
            {
                std::cout << "Protocol option log verbosity unable to set\n";
                return -2;
            }
        }
        else
        {
            std::cout << "Unable to set Host address\n";
            return -1;
        }
    }
    catch (...) 
    {
        std::cout << "Unknown exception occurred\n";
        return -8;
    }
}

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.

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.

like image 825
Rethipher Avatar asked Nov 20 '15 17:11

Rethipher


1 Answers

In modern C++ programming, typically, if you encounter an error where the program can't continue, then I think it's better to throw an exception.

So your function would wouldn't return anything (i.e. void). Whenever it ran into a can't continue situation, you would throw an exception that tells what the error is. The calling code would then deal with the error.

The advantage to this, is that you get to choose where to deal with the error. For example, the stack may unwind all the up to main.

You code could look like this:

void Session::cBeginSession()
{
    if (ssh_options_set(mySession, SSH_OPTIONS_HOST, &hostIP))
    {
        // throw an exception
    }
    if (ssh_options_set(mySession, SSH_OPTIONS_LOG_VERBOSITY, SSH_LOG_PROTOCOL))
    {
        // throw an exception
    }
    if (ssh_options_set(mySession, SSH_OPTIONS_PORT, &port))
    {
        // throw an exception
    }
}

Once you get the hang of coding with exceptions, code tends to be cleaner and more robust since you're not always worrying about checking return codes.

EDIT

To answer you comment. You can choose how and when to handle the error. You can just catch the exception above your call. But, in general, if you want to do something that can fail (but not end a program) you can make another function that returns a boolean status.

bool Session::tryCBeginSession()

Now, your original function void Session::cBeginSession() would be implemented in terms of this new function. I've found that in most cases writing these dual functions is done only in a limited number of cases.

like image 129
Anon Mail Avatar answered Sep 28 '22 22:09

Anon Mail