Question

I'm seeking for best practices and coding conventions, so have a question on this. Looked trough almost all of them, but couldn't find any answers. Example function:

- (int)getSomeNumber
{
    int result = 0;
    if (something) {
       result++;
    }
    return result;
}

Question is, is it better to write result++, or ++result. If someone knows some suggestions or answers why is it better to write in one way or the other it would be very great. Thanks in advance!

Was it helpful?

Solution

It doesn't matter the slightest when used as a statement (note that it does matter when part of a larger expression), as long as you are consistent. Don't mix styles within one source file or project.

Well, even better in this specific example would be return something ? 1 : 0;. Some people even prefer return something;, but I don't because in my eyes it makes no sense to convert a Boolean into an integer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top