1

I'm working with Parse.com and in a lot of VC's I'm calling PFUser *currentUser = [PFUser currentUser];

I figured it might be good practice to #define currentUser in the prefix file. Is that good practice?

Also, how do I define an object like this?

bdv
  • 1,154
  • 2
  • 19
  • 42

2 Answers2

1

Instead of using a #define, you should use a static const.

static const PFUser *kCurrentUser = [PFUser currentUser];

By convention the lowercase k is added in front of the constant name to indicate that it is a constant. If you want to read more on why static const is preferred over #define, look here.

Community
  • 1
  • 1
Morgan Chen
  • 1,137
  • 7
  • 18
  • thanks, that's a good one i guess. However it's not quite static. Values of the user can change. Should I still call it a const then? – bdv Oct 30 '14 at 23:42
  • No, because static variables can only be assigned once. If `[PFUser currentUser]` ever changes, then unfortunately your best bet is to just stick with that particular method call or store a reference to `currentUser` every time it is defined or changes. – Morgan Chen Oct 30 '14 at 23:46
0

Basically #define is almost never a good practice. We have been trying to get away from them for a couple of decades.

When a developer reads currentUser he will not know what is happening, it will be a WTF moment. WTF moments are to be avoided, according to Uncle Bob the quality of code can be measured in the units: "WTFs/min". If you really want to do something like this make a one-line method or property.

Go for the most understandable code to the next developer in the future--it may even be you!

zaph
  • 111,848
  • 21
  • 189
  • 228
  • I don't think currentUser will cause a WTF moment. currentUser.name is pretty good readable. – bdv Oct 30 '14 at 22:40
  • 2
    @bdv only until after few years someone else is trying declare `MyUser *currentUser` which translate to `MyUser *[PFUser currentUser]` – Bryan Chen Oct 30 '14 at 22:53