0

This question is also asking what type of things I better pass as an argument to a class method vs. having as a property of a class? For example, if we have a method func which operates on name of the class, then I got two options here:

@property (nonatomic) NSString *name;
- (void)func;

OR

- (void)funcWithName:(NSString*)name;

Both look as a valid design by ObjC to me. However, it's no clear to me how the user of the class is expected to know that he will need to set the name property before calling func? and If this is not an appropriate usage of the property, then what use cases it is an appropriate usage for a non-readonly property?

sramij
  • 4,775
  • 5
  • 33
  • 55

1 Answers1

1

You're comparing apples with oranges. The two architectures you've suggested have nothing to do with each other.

  • If the class needs a name property for some reason, it should have a name property.

  • If the method needs a new string passed to it, it should have a string parameter.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • Are you sure about that? I am talking here about a case where both points are true, the class needs a name and the method operates on the same name. – sramij Sep 26 '17 at 16:59
  • 1
    Of course I'm sure. What I'm telling you is that _you_ are confusing two orthogonal things. If the method truly operates on the existing name _property_ then it would be idiotic to hand it a name parameter, as this could be a _different_ name. If the method's job is to _set_ the name property _and then_ operate on it, then of course it makes sense to hand it the new value. – matt Sep 26 '17 at 17:15
  • Then we are back to my first point. The method truly operates on the existing name property and I agree it's idiotic to have another string as a name parameter. Then, in this case (and this is my question), who guarantees that the name property will be set before calling this method? otherwise, it won't have anything to operate on. – sramij Sep 26 '17 at 17:20
  • If that's a risk, it's your job, in the method, to check whether the name property _has_ been set. If it is makes no sense for this entire object to come into existence _without_ a name, then it's your job to make its designated initializer _require_ a name. – matt Sep 26 '17 at 17:25
  • Now you will appreciate where this is coming from: https://stackoverflow.com/questions/46373190/designated-initializer-should-only-invoke-a-designated-initializer-on-super-wh/46378647?noredirect=1#comment79789687_46378647 – sramij Sep 26 '17 at 17:36