0

So I know a bit of scripting and basic concepts of OOP, however when it comes down to writing actual code, the syntax kills me.

I downloaded the Stanford course on iTunes U to try to learn Objective C, and the professor keeps reiterating that you want your code to read as much like English as possible. I have a BA in English and I do not think it reads like English in the slightest.

Maybe this question is too off the paradigm, but can anyone make a comparison of the Objective C syntax to that of English?

I would love to make these connections in my brain so that I could write code without struggling with every single line.

Thanks for your help.

Costique
  • 23,712
  • 4
  • 76
  • 79
Lucas
  • 37
  • 1

2 Answers2

6

What the professor meant was the difference between common C-like programming code, such as:

foo.drawCircle(p, r, c); //C#, C++, …

versus common Objective-C programming code, such as:

[foo drawCircleAtPoint:p withRadius:r andColor:c]; //Objective-C

Which one tells you immediately what it's doing and what the parameters stand for?

With method parameters being (kind of) labeled in Objective-C even complex methods (parameter-wise) can be read kind of like plain english. They differ in capitalization and whitespace, but the wording pretty much reflects english language.

Objective-C is not language specific though (apart from if, do…while, etc). You could easily write the line above as:

[foo zeichneKreisAnPunkt:p mitRadius:r undFarbe:f]; //Objective-C

and now your code reads somewhat German. Not that you should do that (seriously, don't!), though.


A language that very much resembles the english language would be Applescript. But it's this very feature that makes Applescript such a pain to work with. English (natural languages in general) simply isn't/aren't meant to be used for instructing computers.

Regexident
  • 29,441
  • 10
  • 93
  • 100
  • This use of named parameters is the one thing I like about Objective-C - it really is ingenious. I would love to see this style migrate into C# (since it has named parameters as well). – Ken Wayne VanderLinde Aug 06 '11 at 21:39
  • That makes a lot of sense, and you are right, I totally understand what the Objective-C code is saying. Drawing a circle at the point stated by p, with the radius stated in r and the color of c. That makes sense. When I look at most Objective-C code though, it doesn't come across as all that clear. – Lucas Aug 06 '11 at 22:06
  • Well, yeah the language design encourages well-named methods, but eventually it's all up to you to actually choose a descriptive method name. The ones in Apple's frameworks are pretty well named and consistent though, imho. – Regexident Aug 06 '11 at 22:55
1

I am really sorry for your disappointment. It is not right to compare English (Verbal Language) and Objective-C (Programming Language). But to some extent the syntax of objective-c is more understandable than other programming language for that matter.

On first glimpse for a beginner, he will be able to understand how the code works "Logically" (compared to C or C++, where he is dependent on some documentation)! He would not be able to understand the programming aspects of the code in Objective-C, but its easier to comprehend whats going on.

For example, in C, you will have create methods for initializing a string, and you can name the method like createString(); In objective-C, you can start writing code like how you might feel.

user.text = [NSString stringWithFormat:@"%@ is a %@", string1, string2];

or

html = [html stringByReplacingOccurrencesOfString:
       [NSString stringWithFormat:@"%@>", text] withString:@""];

There are so many examples, and you will be happy for this language only if you start comparing with other programming language. I think your professor meant that in a positive way so as to encourage the students to learn.

Legolas
  • 12,145
  • 12
  • 79
  • 132
  • Comparing it to other programming languages is kind of hard since I wouldn't say I am all that fluent in any of the other ones. The closest thing for me would be javascript, but I still struggle overall. Then again, its one thing to turn in assignments, its another thing to actually create a running program on my own. – Lucas Aug 06 '11 at 22:03