What is the alternative to overusing the point operator in Obj-C?

See what you think of this line of code:

if ([pickerViewController.picker.bvc.currentResolve.name isEqualToString:message]) ... 

Do you think this is overuse of the point operator?

If not, I can leave it as it is.

But if so, what is the preferred alternative?

+4
source share
5 answers

This is more likely a violation of the Demeter law than a problem with the point operator. A β€œclean” way to do this is to let the object understand it yourself so that you can do something like

 if ([pickerViewController hasPickedName:message]) 
+10
source

I do not think the excessive use of property notation. If an object has a property, refer to it as such; he shows the reader what a programmer means.

Oh, and the proactive team "looks like a structure"; if you cannot specify a structure from an object in your code, reorganize your code.

0
source

As long as each use of the point operator really extracts a property (i.e., not a method that basically does the job for purposes other than the return value), then that's fine. In fact, if you check out Wil Shipley's blog, he is actually a fan of the chain, as many function calls are concatenated as necessary (he does not like the overuse of local variables).

0
source

I believe that this kind of debugging is always more of a problem than it’s worth, so I try to create intermediate variables for each access to the properties. Or, as others suggested, reorganize it so that it looks easier on the use site (by putting smart methods into a method).

0
source

I agree with Chuck and the commentators. Your method depends on too many other objects, but putting hasPickedName: on pickerViewController means pickerViewController pickerViewController does [picker.bvc.currentResolve.name isEqualToString:message] .

Instead, you can put hasPickedName: in bvc and paste bvc as a delegate (e.g. id<NamePickerDelegate> ) into your top-level object using Interface Builder. To be truly compatible with Demeter, make currentResolve grow a nameMatches: method that combines [currentResolve.name isEqualToString:message] .

You must carefully study the difficulty caused by the problem, the complexity styles that each solution will implement. If you think the source code is simpler and easier to maintain than alternatives, keep it.

0
source

All Articles