Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Hello,
I am following along with this tutorial, and I need some help understanding some code.

In the interface of Terrain.h the following lines of code are there:
Code:
    #define kMaxHillPoints 1000
    int _offsetX;
    CGPoint _hillKeyPoints[kMaxHillKeyPoints];
    CCSprite *_stripes;

Why is there an underscore before offsetX and stripes, is that just a naming convention used by the author or is it a known Objective-C naming convention? I have the same question with the #define line, I have seen these before, but why is there a k in front of MaxHillPoints?

Can someone also explain the CGPoint with the brackets beside it, what do the brackets mean?


In the Terrain.m there is a synthesize line that looks like this:
@synthesize stripes = _stripes;

What this code mean? I have seen and used @synthesize, but why is the author setting it equal to something?

I know this tutorial was written for IOS, but I am using the Mac port of Cocos2d.

Thanks!
 

wlh99

macrumors 6502
Feb 7, 2008
272
0
It's a naming convention used by the author.
The idea is that to set a member with
Code:
stripes = myStripe;
instead of
Code:
self.stripe = myStripe;
can cause a leak. By hiding the the member with an underscore, it forces the use of accessor methods. The = equates the accessor method "setStripe" to the member "_stripe" instead of the default "stripe"

I don't think the convention is used very often with Cocca. I tried to learn and use it in a project once. It only confused me and screwed things up.

Using k is very common. It lets you know that is not a variable in the program, but rather a defined constant. I think "k" is derived from the first sound in the word constant.
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Can someone also explain the CGPoint with the brackets beside it, what do the brackets mean?

_hillKeyPoints is an array of CGPoint, size is kMaxHillPoints.

In the Terrain.m there is a synthesize line that looks like this:
@synthesize stripes = _stripes;

What this code mean? I have seen and used @synthesize, but why is the author setting it equal to something?

Synthesize the stripes getter and setStripes: setter to use the _stripes instance variable. This syntax is necessary when the backing ivar isn't named the same as the declared property.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
Thanks everyone!
What is the advantage to using a constant rather than an int or a float?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
Thanks everyone!
What is the advantage to using a constant rather than an int or a float?

Self documenting code. kMaxHillPoints is clearer than 1000.

Maintainability. If kMaxHillPoints is used consistently, than the size of the array can be changed just by changing the constant.
 

Blakeasd

macrumors 6502a
Original poster
Dec 29, 2009
643
0
What I mean is why do this:
Code:
#define kMaxHillPoints 1000

rather than

Code:
int maxHillPoints;

and setting it equal to 1000?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
What I mean is why do this:
Code:
#define kMaxHillPoints 1000

rather than
Code:
int maxHillPoints;

and setting it equal to 1000?

Actually I do prefer const int (the const is important) over #define. I don't like using preprocessor defines for anything except conditional compilations (and a small class of macros such as asserts and logs).

But it can be a bit tricky when the constant is used as in the cited context. For you, I would suggest sticking with #define here.
 

Shawnpk

macrumors 6502
Jan 13, 2011
350
0
Los Angeles, CA
What I mean is why do this:
Code:
#define kMaxHillPoints 1000

rather than

Code:
int maxHillPoints;

and setting it equal to 1000?

Let's say you use maxHillPoints 20 times in your code and set it to 1000. Then you decide 1000 isn't enough and you want to change it to 10,000. Without the #define, you would have to change the 1000 to 10,000 20 different times. If you use #define kMaxHillPoints 1000 and you wanted to change it, you would only need to change it in one place and the change would take affect globally. It's also much easier to read and understand kMaxHillPoints than just a plane 1000.
 

Sander

macrumors 6502a
Apr 24, 2008
521
67
What I mean is why do this:
Code:
#define kMaxHillPoints 1000

rather than

Code:
int maxHillPoints;

and setting it equal to 1000?

Your question has been answered already, but I think it was misunderstood. Modern C programming does indeed prefer

Code:
const int maxHillPoints = 1000;

since use of the preprocessor is preferably minimized. One advantage of using const int instead of a #define is that in the debugger, the symbol "maxHillPoints" is still visible and the #define will have been replaced with its numerical value. It then becomes harder to track where the number came from.

I think it's largely a matter of style though.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.