iHackerMe
iHackerMe

Reputation: 591

Why are all things in Objective-C created by * lower case?

I'm really confused about this thing. In any tutorial, code on github and documentation I have seen BOOLs, NSStrings, NDDictionaries etc all start in lower case. Eg. NSString *aString Is this necessary? Will anything get ruined if I use upper-case while creating BOOLs etc? I'm really confused here :P Any help is greatly appreciated!

Edit: Will using an underscore and then declaring variables in Capitals be a good idea?

Upvotes: 0

Views: 157

Answers (6)

Popeye
Popeye

Reputation: 12093

Added from comments

Its more of a coding standards thing. I have just taken my SCJP exam and they have an entire chapter on coding standards. Its up to the programmer how they develop there code but think about the person who has to make amendments to it. Will using capitals make it easier to read or just make it harder. My recommendation is to stick with the coding standards as most developers understand them.

Also under coding standards you would normally Have your Classes declared with a capital and variables starting with lower case. This is so it doesn't confuse other developers when make amendments and more importantly so it doesn't confuse you. It wouldn't be good if you got confused by your own code.

But standards say it should be like this :

 NSString *nsString; // Not that you should be having a variable like this. But stops the any confusion.
 [nsString uppercaseString];

it would start to get confusing if you had this

 // Compile won't actually let you have this just an example of confusion and reserved words. 
 NSString *NSString;
 [NSString uppercaseString];

In coding standards must experienced developers know that anything starting with a capital is generally a class and anything starting with lower case is a variable. This is in almost all programming languages. I did see a bit of confusion around coding standards when studying prolog.

Like Sun, Apple have there coding standards here

Upvotes: 5

Abizern
Abizern

Reputation: 150615

Its one of the coding conventions in Cocoa.

There is a document about Coding Guidelines For Cocoa on the Apple site.

It's useful to follow these conventions. Not only because it makes your code easier to be read by other Cocoa Developers, but also because the modern LLVM compiler relies on these conventions sometimes, and you'll be getting compiler warnings for things you'll think are false positives rather than because of your unconventional code.

Upvotes: 2

TheTiger
TheTiger

Reputation: 13354

This is just style or standard of Objective-C. You can also declared your variables like this -

BOOL  MyVariable;

It would also work, but it would create a confusion b/w a class name and variable name this is not friendly code. So we use proper rule for declare variables.

Upvotes: 0

Sulthan
Sulthan

Reputation: 130102

This is a common standard helping you to differentiate between classes and instances of classes / variables.

Different languages use different standards but you should always use the style common for the language you are using.

Upvotes: 1

Eric
Eric

Reputation: 4061

it's called camelBack Notation. Ruby guys like underscore_notation, iOS camelBack. Though in my opinion it also makes it easier to distinguish objects (camelBack) from classes (capital).

Upvotes: 0

Wooble
Wooble

Reputation: 89927

It's just a style issue. Nothing will get ruined by using capitals, except perhaps your reputation among people who follow that style.

Upvotes: 7

Related Questions