Tribler / tribler

Privacy enhanced BitTorrent client with P2P content discovery
https://www.tribler.org
GNU General Public License v3.0
4.82k stars 447 forks source link

Removing circular references #3896

Closed ichorid closed 3 years ago

ichorid commented 6 years ago

Circular references in objects structure is bad practice for many reasons. The worst result of this practice is a God-object anti-pattern. We actually have a whole Trinity of God-objects in LaunchManyCore, Session, and DownloadManager.

These anti-patterns significantly complicate mocking, reduce testing efficiency, and prevent us from achieving a truly modular design, that is the first step to the microservice architecture.

In general, functions/methods should never extract information from its arguments. For example, we want the class __init__ arguments list to represent the minimal set of things that this class requires to be instantiated and do its thing.

In short, we must refactor code like this:


class SomeModule():
    def __init__(self, session):
        self.session = session

    def do_something(self): 
        print(self.session.some_property)
        print(self.session.another_property)

to

class SomeModule():
    def __init__(self, property, another_property):
        self.property = property
        self.another_property = another_property

    def do_something(self): 
        print(self.some_property)
        print(self.another_property)

We are already going to refactor the libtorrent wrapper, so this is a suitable moment to add this rule of no parent referencing to Tribler coding standards.

devos50 commented 3 years ago

This issue has been sufficiently addressed by the components refactoring 👍