6

I have been learning Objective-C, noting that different books and examples use differing conventions for the location of the star (*) when naming reference variables:

MyType* x; MyType *y; MyType*z; // this also works 

I see the first two used interchangeably, and sometimes in the same code I've seen differing uses of both. Is one format easier to read, easier to maintain, or less error prone than the other formats, or is it simply a matter of personal preference?

4
  • I hardly see why this is "not constructive". Given the answer provided by dukeofgaming it is now clear to me there is a real reason why this is relevant. That being said the FAQ states that subjective questions are allowed. Commented Nov 18, 2012 at 14:03
  • It is also not as accepted an answer as you seem to thing. But closing this question has stopped discussion on the subject. That is also what the admins want they do not want discussion like question as there is not real answer just a bunch of people with opinions. Commented Nov 18, 2012 at 15:46
  • 2
    The first version MyType* x; is more C++ like. The second one MyType *y; is more C like. The third one is just lazy. But there is no absolute agreement. Your best bet is to use a convention and stick with it. If the source is already one way then stick with the already established convention of the file. Commented Nov 18, 2012 at 15:49
  • You forgot MyType * a, which I like the most. Commented Dec 12, 2012 at 10:38

1 Answer 1

8

In my opinion this is more semantically clear:

MyType *x; 

Since you can also have this without changing the convention:

MyType *x, *y, *z; 

Also, I generally look at the variable declaration, not the type, when it comes to pointers.

5
  • 8
    Ah, I think I understand. So for MyType* x, y, z only x is declared as a pointer type? Commented Nov 18, 2012 at 4:46
  • 1
    @BrettRyan: That argument is used a lot as a reason. But most coding standards also say that declaring variables that way is also band because of this corner case. So it becomes a null argument in my mind. Personally I think it is more clear the other way with the star beside the type (as to me a C++ programmer it is part of the type). And this is were the arguments break down as C++ programs tend to fall one way while C programers the other. But on both sides there is not a universal agreement. Commented Nov 18, 2012 at 15:43
  • @LokiAstari I really don't think it is a corner case, more like a misinterpreting of the grammar. I'm a C++ programmer more than a C programmer and I don't think that has anything to do. Commented Nov 18, 2012 at 16:51
  • 1
    I am fine that some people like it one and other people the other way. In the end it boils down to the same things thus making no difference. BUT your argument for is a invalid. Because that situation does not occur in real code because it is explicitly banned by coding standards (or happens so really that that when it does happen you get laughed at by experience programmers during code review and don't do it again. :-) You are more of php programmer than either. Commented Nov 18, 2012 at 19:20
  • I don't see how my argument is invalid, not all people respect coding standards, or have coding standards at all, or have coding standards that cover this specific case, or practice code reviews. You would be surprised about what happens in very real code, specially with big distributed teams. Commented Nov 19, 2012 at 3:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.