6

I'm going through some training on objective-c. The trainer suggests setting single character parameter names. The .NET developer in me is crying. Is this truly the convention? Why?

For example,

@interface Square : NSObject
{
  int size;
}

-(void)setSize: (int)s;

I've seen developers using underscores int _size to declar variables (I think people call the variable declared in @interface ivar for some unknown reason). Personally, I prefer to use descriptive names. E.G.

@interface Square : NSObject
{
  int Size;
}

-(void)setSize: (int)size;

C, like C# is case sensitive. So why don't we use the same convention as .NET?

2 Answers 2

10

There are many standards for variable naming, it depends on the programming language used, and on the core developers involved in the project.

In case of objective-c, I think this page should answer your question: https://developer.apple.com/documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html

As per using single-letter parameter names as a general rule, it is clearly a horrible idea, don't listen to it.

1
  • Especially with good autocompletion. Self-documenting code ftw!
    – anthonyvd
    Jun 28, 2012 at 17:34
-1

If I have understood correctly you are talking about Hungarian Notation.

In this case you can refer to this answer for an explanation.

You can also refer to this answer for the second part of your question.

5
  • 1
    Yes and no. To me, Hungarian Notation implies appending type information to variable names, which is not what I am suggesting at all. I simply mean to say variable names should be descriptive. Regarding underscores, the underscores make sense for private variables. Still, I prefer using casing over appending underscores. This avoids breaking intellisense-like features which sort alphabetically. Jun 28, 2012 at 16:23
  • what do you mean with "descriptive"? appending type names is also descriptive but is wrong, in general creating this bind between labels and descriptions leads to a lot of useless noise in the communication, also remember that this kind of work ( naming ) should be "atomic", as short as possible and with all the meaning in a single word. Something like Tyre.getPressure(), it's abstract enough and you get all the info that you need, it's abstract because you are not binding the concept of the Tyre with a Car or a Bus, continues
    – user827992
    Jun 28, 2012 at 16:36
  • writing CarTyre.getPressure() is wrong because you are not taking advantages of the OO design and you can simply have a class Tyre that inherits from the class Car. If you read the post that i have linked you will find that this behaviour can only generate noise and is just useless and wrong.
    – user827992
    Jun 28, 2012 at 16:39
  • Using single letters for parameters/variables is not Hungarian notation. Jun 28, 2012 at 16:58
  • @user827992, I'm sorry, but Tire inherits from Car? In what world do you live where a "Tire is a kind of Car"? Because that's what inheritance implies. Also, this is indeed not Hungarian Notation at all.
    – anthonyvd
    Jun 28, 2012 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.