{"action":"created","issue":{"url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/issues/1800","repository_url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot","labels_url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/issues/1800/labels{/name}","comments_url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/issues/1800/comments","events_url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/issues/1800/events","html_url":"https://github.com/Cog-Creators/Red-DiscordBot/issues/1800","id":329310350,"node_id":"MDU6SXNzdWUzMjkzMTAzNTA=","number":1800,"title":"[V3] Singleton Design Discussion","user":{"login":"Tobotimus","id":7438501,"node_id":"MDQ6VXNlcjc0Mzg1MDE=","avatar_url":"https://avatars1.githubusercontent.com/u/7438501?v=4","gravatar_id":"","url":"https://api.github.com/users/Tobotimus","html_url":"https://github.com/Tobotimus","followers_url":"https://api.github.com/users/Tobotimus/followers","following_url":"https://api.github.com/users/Tobotimus/following{/other_user}","gists_url":"https://api.github.com/users/Tobotimus/gists{/gist_id}","starred_url":"https://api.github.com/users/Tobotimus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tobotimus/subscriptions","organizations_url":"https://api.github.com/users/Tobotimus/orgs","repos_url":"https://api.github.com/users/Tobotimus/repos","events_url":"https://api.github.com/users/Tobotimus/events{/privacy}","received_events_url":"https://api.github.com/users/Tobotimus/received_events","type":"User","site_admin":false},"labels":[{"id":593836458,"node_id":"MDU6TGFiZWw1OTM4MzY0NTg=","url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/labels/V3","name":"V3","color":"006b75","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-06-05T05:49:44Z","updated_at":"2018-06-06T03:27:41Z","closed_at":null,"author_association":"NONE","body":"Red as we know it is an application with various singleton components at its core (literally, in the `core` package). What I'm referring to as singletons are components for which there exist only one instance of per application.\r\n\r\nIn the core package currently, this boils down to:\r\n - the bank\r\n - modlog\r\n - the Sentry manager\r\n - the help formatter\r\n - the cog manager\r\n - the data manager\r\n\r\nThere is also the RPC server manager, which is currently being refactored over at #1780, and of course the bot itself, however we're not really talking about the bot class itself here, we're talking about other singletons.\r\n\r\n### Current design of these components\r\nAs of right now, the design of these singletons is inconsistent. Some use a singleton class, which is instantiated on bot initialisation and attached to the bot as an attribute. This includes sentry, the help formatter, the cog manager, and RPC.\r\n\r\nThe rest simply use their module as a container for data and as an interface to functions, which can be imported at the top of a module. Python allows us to do this as an alternative to the more \"Java-esque\" approach of singleton classes. But is it good practice? Well, that's what this issue is about.\r\n\r\n#### The singleton class approach\r\nPersonally, I see this approach as simpler, easier to maintain, and more extendable. On the other hand, it can be seen as polluting the bot object's attributes. It also means we need access to the bot in order to have access to these components, but thankfully the bot object is very easy to get a hold of.\r\n\r\nFor the help formatter, this approach is actually forced upon us by discord.py and is necessary to allow anyone to customise the help formatter, hence why I mentioned this approach being more extendable.\r\n\r\n#### The singleton module approach\r\nThis approach is fairly easy to document and use, as users only need to do an import and they can access all of these singleton components. However, this does lead to more imports than may be necessary, and sometimes I fear one day we're gonna fall upon some horrible circular import problems  within the core package.\r\n\r\n### What's better?\r\nPersonally I prefer the former approach, but this issue is for others to chip in and add to what I have stated above so we can have some sort of standard moving forward.\r\n\r\n#### So when we decide what's better, we should rewrite everything to conform to that?\r\nIn short, no, not simply for the sake of conforming to a new standard. There may be a reason down the line for us to switch existing singletons over, but right now it's a risk which is certainly not worth taking - all of the singletons currently function just fine, rewriting them will likely just cause more issues, and we also just don't need the extra work. However, hopefully the outcome of this discussion will serve as a good reference for any future developments."},"comment":{"url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/issues/comments/394928600","html_url":"https://github.com/Cog-Creators/Red-DiscordBot/issues/1800#issuecomment-394928600","issue_url":"https://api.github.com/repos/Cog-Creators/Red-DiscordBot/issues/1800","id":394928600,"node_id":"MDEyOklzc3VlQ29tbWVudDM5NDkyODYwMA==","user":{"login":"Tobotimus","id":7438501,"node_id":"MDQ6VXNlcjc0Mzg1MDE=","avatar_url":"https://avatars1.githubusercontent.com/u/7438501?v=4","gravatar_id":"","url":"https://api.github.com/users/Tobotimus","html_url":"https://github.com/Tobotimus","followers_url":"https://api.github.com/users/Tobotimus/followers","following_url":"https://api.github.com/users/Tobotimus/following{/other_user}","gists_url":"https://api.github.com/users/Tobotimus/gists{/gist_id}","starred_url":"https://api.github.com/users/Tobotimus/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Tobotimus/subscriptions","organizations_url":"https://api.github.com/users/Tobotimus/orgs","repos_url":"https://api.github.com/users/Tobotimus/repos","events_url":"https://api.github.com/users/Tobotimus/events{/privacy}","received_events_url":"https://api.github.com/users/Tobotimus/received_events","type":"User","site_admin":false},"created_at":"2018-06-06T03:27:41Z","updated_at":"2018-06-06T03:27:41Z","author_association":"NONE","body":"I'm not sold on having a module containing global references to singletons. I don't particularly see a downside in requiring a reference to the bot object in order to access them.\r\n\r\nSingletons act as interfaces to the application's shared state. This state is largely held in the bot object. I think that holding a reference to the bot object is a simple, intuitive (and I might say \"pythonic\") way of mutating that shared state."}}
{"actor":{"display_login":"Tobotimus"}}