Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way of preventing other programmers from calling -init

When designing a class hierarchy, sometimes the subclass has added a new initWithSomeNewParam method, and it would be desirable to disable calls to the old init method inherited from the superclass.

First of all, I've read the question here, where the proposed alternatives are either override init to throw an exception at runtime, or override and set default values for properties. In my case, I don't wan't to provide default values, and I want to clearly indicate that the old method should not be called, and instead the new method with parameters should be used.

So the runtime exception are fine, but unless the code is debugged, there's no way for other programmers in the team to notice that the old method is no longer intended to be used.

If I'm correct, there's no way to mark a method as "private". So, apart from adding comments, is there a way to do this?

Thanks in advance.

like image 709
Mister Smith Avatar asked Mar 09 '12 12:03

Mister Smith


People also ask

Why are most programmers single?

Most programmers are primarily single because of their age, gender, and workload. 72% of programmers are under the age of 35 and 91% of them are male. Younger men are more likely to be single with 39% of them not having a partner. Plus the workload of a programmer can sometimes make it harder to meet a partner.

What are the 3 types of programming errors?

When developing programs there are three types of error that can occur: syntax errors. logic errors. runtime errors.

Is pair programming still a thing?

Pair programming doesn't seem as if it will be going away anytime soon, and there are good reasons for your organization to implement it. But it's not a plug-and-play solution, and you might face some resistance from developers telling you why pair programming is bad.


1 Answers

You can explicitly mark your init as being unavailable in your header file:

- (id) init __unavailable; 

or:

- (id) init __attribute__((unavailable)); 

With the later syntax, you can even give a reason:

- (id) init __attribute__((unavailable("Must use initWithFoo: instead."))); 

The compiler then issues an error (not a warning) if someone tries to call it.

like image 164
DarkDust Avatar answered Sep 29 '22 10:09

DarkDust