Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding conventions for method returns in C++

I have observed that the general coding convention for a successful completion of a method intended functionality is 0. (As in exit(0)).

This kind of confuses me because, if I have method in my if statement and method returns a 0, by the "if condition" is false and thereby urging me to think for a minute that the method had failed. Of course I do know I have to append with a "!" (As in if(!Method()) ), but isn't this convention kind of self contradicting itself ??

like image 688
Vishnu Pedireddi Avatar asked Sep 22 '10 18:09

Vishnu Pedireddi


People also ask

How do you write a return statement in C?

void report_square( void ) { int value = INT_MAX; long long squared = 0LL; squared = square( value ); printf( "value = %d, squared = %lld\n", value, squared ); return; // Use an empty expression to return void. }

What's an example of coding conventions?

One example from the coding conventions is how to define a constant. Constants should be written with capital letters in Java, where the words are separated by an underscore ('_') character. In the Java coding conventions, a constant is a static final field in a class.

What are naming conventions in C?

The first character of the name should be a letter and all characters (except the period) should be lower-case letters and numbers. The base name should be eight or fewer characters and the suffix should be three or fewer characters (four, if you include the period).

What is return type in C?

Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function.


2 Answers

You need to differentiate between an error code and an error flag. A code is a number representing any number of errors, while a flag is a boolean that indicates success.

When it comes to error codes, the idea is: There is only one way to succeed, but there are many ways to fail. Take 0 as a good single unique number, representing success, then you have every other number is a way of indicating failure. (It doesn't make sense any other way.)

When it comes to error flags, the idea is simple: True means it worked, false means it didn't. You can usually then get the error code by some other means, and act accordingly.

Some functions use error codes, some use error flags. There's nothing confusing or backwards about it, unless you're trying to treat everything as a flag. Not all return values are a flag, that's just something you'll have to get used to.

Keep in mind in C++ you generally handle errors with exceptions. Instead of looking up an error code, you just get the necessary information out of the caught exception.

like image 170
GManNickG Avatar answered Sep 29 '22 18:09

GManNickG


The convention isn't contradicting itself, it's contradicting your desired use of the function.

One of them has to change, and it's not going to be the convention ;-)

I would usually write either:

if (Function() == 0) {
    // code for success
} else {
    // code for failure
}

or if (Function() != 0) with the cases the other way around.

Integers can be implicitly converted to boolean, but that doesn't mean you always should. 0 here means 0, it doesn't mean false. If you really want to, you could write:

int error = Function();
if (!error) {
    // code for success
} else {
    // code for failure, perhaps using error value
}
like image 29
Steve Jessop Avatar answered Sep 29 '22 16:09

Steve Jessop