0

This is my example I've found:

#define kNumberOfViews (37)
#define kViewsWide (5)
#define kViewMargin (2.0)

Why it cannot be like that?

#define kNumberOfViews 37
#define kViewsWide 5
#define kViewMargin 2.0

And what means k in front? Is there a some guide for it?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
  • 4
    `k` simply is a convention to indicate that the value is a constant/literal. The `()` are generally not needed for a "simple" value (I'm sure someone will come up with an exception), but is highly advised if you have an expression such as `1<<3`, since otherwise the expression can bump up against something that associates first and produce "unpredictable results". – Hot Licks Mar 01 '15 at 14:26
  • 1
    I agree that using an initial `k` is a convention, however I usually see it when the constant refers to a `key` (that's what I believe the `k` comes from). a convention for regular constants is to write them in capitals?! I know these are both "just" conventions, but this distinction gives it a bit more context which I personally like about it. @HotLicks – nburk Mar 01 '15 at 14:29
  • it also means that constants starting with a `k` are distinct, which is not necessarily the case for constants in capitals. – nburk Mar 01 '15 at 14:30
  • (1) you should not use "k ..." for preprocessor macros, don't do that. (2) use ALLCAPS for such macros. (3) the reason you need braces is trivial, to avoid "expansion woes" as Rory explains. – Fattie Mar 01 '15 at 15:18
  • 1
    @JoeBlow - There were, at last count, 3.14159268 x 10**34 different naming conventions for constants and literals. To say that using `k` is wrong and ALLCAPS is right is arbitrarily picking one of those conventions over all the others. – Hot Licks Mar 01 '15 at 15:34
  • FYI - `()` are not brackets, they are parentheses. `[]` and `{}` are brackets (square and curly respectively). – rmaddy Mar 01 '15 at 15:42
  • @rmaddy - They're "brackets" to many non-US people. But you're correct that one needs to be more specific. (And, BTW, `[ ]` are "brackets"; `{ }` are "braces", at least by one convention.) – Hot Licks Mar 01 '15 at 18:57
  • @HotLicks Yeah `{}` are braces, not brackets. I don't know what I was thinking when I wrote that. `()` - "parens" or "parentheses", `{}` - "braces" or "curly braces", and `[]` - "brackets" or "square brackets". – rmaddy Mar 01 '15 at 19:00
  • Hi HotLicks. Your comments about conventions **are I'm afraid totally incorrect**. Programming is **almost nothing but conventions**. It is a huge and important field. Every working team puts a huge effort in to settling conventions. It's literally one of the most important things in programming, let's say one of the four most important issues. In the milieu at hand, there are not "very many" naming conventions. There's pretty much two to choose from and that's it. Note too that specifically in the Apple universe, I've never ever met any team or company that doesn't agree.. – Fattie Mar 04 '15 at 10:26
  • ...you should "do what Apple does" in terms of idoim, variable naming, and conventions such as the one under discussion. the convention that you use caps in the situation shown, is so well-accepted, the example above is quite simply "wrong". And the idea of using kWhatever - in certain situations - is now deprecated ("just don't do it") and when you do use it is very specific .. certainly not in the example given. Just some thoughts. – Fattie Mar 04 '15 at 10:28

3 Answers3

5

It is not really required in your example, but the use of parenthesis in defines is a useful approach to make sure your define states exactly what you mean in the context of the define and protects it from side effects when used in code.

E.g

#define VAR1 40
#define VAR2 20
#define SAVETYPING1 VAR1-VAR2
#define SAVETYPING2 (VAR1-VAR2)

Then in your code

foo(4*SAVETYPING1);  // comes out as foo(140)

Is not the same as

foo(4*SAVETYPING2); // comes out as foo(80)

As for what the k prefix means. It is used for constants. Plenty of discussion here on the origins:

Objective C - Why do constants start with k

Community
  • 1
  • 1
Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
2
#define SOME_VALUE 1234

It is preprocessor directive. It means, that before your code is compiled, all occurrences of SOME_VALUE will be replaced by 1234. Alternative to this would be

const int kSomeValue = 1234;

For discussion about advantages of one or the other see #define vs const in Objective-C

As for brackets - in more complex cases they are necessary exactly because preprocessor makes copy-paste with #define. Consider this example:

#define BIRTH_YEAR   1990
#define CURRENT_YEAR 2015
#define AGE CURRENT_YEAR - BIRTH_YEAR

...
// later in the code
int ageInMonths = AGE * 12;

Here one might expect that ageInMonths = 25 * 12, but instead it is computed as ageInMonths = 2015 - 1990 * 12 = 2015 - (1990 * 12). That is why correct definition of AGE should have been

#define AGE (CURRENT_YEAR - BIRTH_YEAR)

As for naming conventions, AFAIK for #define constants capital cases with underscores are used, and for const constants camel names with leading k are used.

Community
  • 1
  • 1
Jakub Vano
  • 3,833
  • 15
  • 29
-2

k is just a hungarian notation convention to indicate that that is a constant value. Personally I find it dumb, but it is a convention that many people follow. It isn't required for the code to work at all.

I am not sure why the examples you saw had parens around them, but there is no need to have parentheses around #define values.

Tim
  • 2,878
  • 1
  • 14
  • 19
  • There definitely is a need for the parens when the values are expressions vs simple values. Doing it all the time is not a bad habit to get into. – Hot Licks Mar 01 '15 at 14:27
  • I am not sure I would abuse #define too much using very complex expressions. As it is a macro it should generally be limited to more simple uses. – Tim Mar 01 '15 at 14:32
  • It's quite common to see, eg, `#define kSomeConst (1 << 4)`, even in Apple's code. And also fairly common to use `#define` to create a concise version of some messy expression that is frequently used. – Hot Licks Mar 01 '15 at 14:37
  • Ok, fair that sort of expression ok. But really there you are defining an unchanging value, you just can't define it without the expression. And, if you have alot of those I could see just getting in the habit. – Tim Mar 01 '15 at 14:40
  • The heck are you talking about? I was agreeing with him. Quit trolling. – Tim Mar 01 '15 at 15:22
  • Hi Tim. There may have been some confusion about what was referring to what. Note that you mention "there is no need to have parentheses around #define values ". That is totally incorrect. You must have braces around macros - in the trivial case of a label it's just as good practice as in any other trivial programming case. Your comment about "trolling" is bizarre. – Fattie Mar 04 '15 at 10:23
  • The question was for ints. There is absolutely no reason you have to have them to define a static int. And you are trolling because you hopped in the middle of two people having a conversation just to tell me I was wrong and he was right, when I was agreeing with his example. As pointed out by hot licks there are certainly scenarios (expressions) where it matters, and I can buy it isn't a bad habit to get into to do it everywhere. Regardless, I never asked you, nor was I talking to you, so submit your own answer if you disagree with mine. – Tim Mar 06 '15 at 00:13