Search code examples
iosobjective-csolid-principlesopen-closed-principle

How to add another parameter to an existing method following SOLID principle in Objective C


I have a method

-(NSData*)getCommonDataWithCommandID:(int)commandID withChannelNumber:(int)channelNo withDataArray:(NSArray*)dataArray withByteArraySize:(int)byteArraySize
{

}

This is called from a lot of places and have a big hierarchy of calling. How can I pass value from dimSliderDecreasedTapped: or tapOnFifthChannel: This is just a portion!

enter image description here

Now I need to add another parameter to this method

bool isFeedbackExpected

But this will violet Open Closed Principle.

What is the best way to do this?


Solution

  • Add another method with that parameter:

    - (NSData*)getCommonDataWithCommandID:(int)commandID
                        withChannelNumber:(int)channelNo
                            withDataArray:(NSArray*)dataArray
                        withByteArraySize:(int)byteArraySize
                         feedbackExpected:(BOOL)feedbackExpected {
          ...
    }
    

    and call this method from your original method

    - (NSData*)getCommonDataWithCommandID:(int)commandID
                        withChannelNumber:(int)channelNo
                            withDataArray:(NSArray*)dataArray
                        withByteArraySize:(int)byteArraySize {
        return [self getCommonDataWithCommandID:commandID
                              withChannelNumber:channelNo
                                  withDataArray:dataArray
                              withByteArraySize:byteArraySize
                               feedbackExpected:NO];
    }
    

    This will extend the functionality but it will keep the old interface. In Swift this would be even simpler because you could add an optional parameter (a parameter with a default value).