0

Long has Objective C annoyed me with its decision to use self instead of this. Now, I want to end that frustration by placing this code somewhere at or near the start of my program:

#define this self

I know this will compile, and likely will work as I expect. However, I can't help but think there are... unforeseen consequences to this. Is this a safe thing to do, or are there problems that will arise outside the simple problem I'm trying to solve?

Ky -
  • 30,724
  • 51
  • 192
  • 308
  • 5
    I understand the desire, but I implore you not to do this if your code is read or maintained by others. – Ben Zotto Apr 27 '15 at 22:23
  • There are other languages that use `self` similar to Objective-C. You should be able to match the language's convention rather than changing it to what you are comfortable with. – Eric Amorde Apr 27 '15 at 22:52
  • @EricAmorde oh, I've not heard of those. Which do that? – Ky - Apr 27 '15 at 23:02
  • The first that come to my mind are Python and Ruby, although in Python it is only a convention to name the variable `self`. – Eric Amorde Apr 27 '15 at 23:09
  • 1
    The question contains a factual component, which is: is "this" reserved in Objective-C? The religion-free answer to that is no...one is free to define this as self. Funny how much dust got kicked up here, IMO. I wouldn't do it, but I don't imagine myself having any trouble reading otherwise well-written code that made this choice. – danh Apr 27 '15 at 23:14
  • @danh thank you! That's the kind of answer I was looking for, rather than the workplace-centric ones others have provided – Ky - Apr 28 '15 at 14:59

2 Answers2

6

The consequence will likely be that no-one else but you can read your code and will get confused by your own convention here.

qwerty_so
  • 35,448
  • 8
  • 62
  • 86
2

Another consequence is that, if imported into C++ or Objective-C++ code, it could break code that uses this by turning it into self which would then be an undefined variable.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • Can you elaborate on what bad things will happen? Your answer popped up in the VLQ review, probably because of the length. – apaul Apr 30 '15 at 01:21