0

If I create a global socket object like so..

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

and define a function like so..

def function():
    <do something with the socket object>

Would it be considered non-dry programming if I passed the socket object as a function parameter for readability, is this the approach I should take when using sockets inside functions that are declared globally or within a while loop?

Example:

def function(passed_sock):
    <do something with the socket object>
quamrana
  • 37,849
  • 12
  • 53
  • 71
apathy_c0d3x
  • 13
  • 1
  • 4
  • Its better not to have global variables in the first place, but even if you do, its better for functions to take parameters for all the objects they need. (btw nothing to do with DRY). – quamrana Dec 05 '19 at 15:54
  • This is a matter of readability? just making sure I understand correctly. – apathy_c0d3x Dec 05 '19 at 15:56
  • 1
    Ideally, functions should accept as parameters all the data they need, and explicitly return all the data that they produce. That isn't always feasible in every case, but it's a good guideline to follow when in doubt. – Carcigenicate Dec 05 '19 at 15:56
  • Related to Law of Demeter: https://en.wikipedia.org/wiki/Law_of_Demeter – quamrana Dec 05 '19 at 15:58

0 Answers0