1

I'm doing some code here and faced this good/bad practice dilemma:

        message_id = self.get_message_id(msg)
        Notification.set_notification_type(self, message_id)

As you can see, I get some return from self.get_message_id(msg) and send it as an argument to Notification.set_notification_type().

My question is: should I, instead of doing this above, do this:

        Notification.set_notification_type(self, msg)

And then, on the Notification.py file:

        def set_notification_type(obj, msg):
            message_id = obj.get_message_id(msg)

That is, should I receive raw things in the method and use the obj parameter to handle what I have to handle or should I send everything already handled by the obj?

Thank you!

1 Answers1

0

This would be more readable and would define clearly what your set_notification_type method would give and what it expects as parameters.

def get_notification_type(messageId):
        //Do stuff to populate notificationType
        return notificationType;

message_id = self.get_message_id(msg)
notification_type = Notification.get_notification_type(message_id)

I am from c# background. So the syntax might have mistakes. Try to keep the method definition as clear as possible so that it can be reused easily.

Isuru
  • 950
  • 1
  • 13
  • 34
  • Great, thank you! I guess this has something to do with "low coupling and high cohesion" right? –  May 01 '19 at 05:27
  • You are welcome. I think we can't relate low coupling and high cohesion to above scenario. Low coupling and high cohesion comes to picture when we talk about classes and modules and their inter-dependability. Here there might be a performance implication according to these two posts. Please have a look https://stackoverflow.com/questions/24570175/python-self-keyword-advantages-and-drawbacks https://stackoverflow.com/questions/1418588/how-expensive-are-python-dictionaries-to-handle/1419324#1419324 – Isuru May 01 '19 at 06:00