Variable duplicate as static and non-static [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter. When calling the StartReceiving function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.



//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);


This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:



void OnAddedToChat(Chat chat) 
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);



Then when actually receiving updates I filter for each module according to which update type they wish to receive.



void OnMessageReceived(Message message) 
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;

foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);




As already commented in the code once the UpdateTypeFilter has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.



Any ideas how to make it nicer are welcome :)







share|improve this question











closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
    – t3chb0t
    Jun 20 at 6:12

















up vote
3
down vote

favorite












So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter. When calling the StartReceiving function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.



//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);


This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:



void OnAddedToChat(Chat chat) 
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);



Then when actually receiving updates I filter for each module according to which update type they wish to receive.



void OnMessageReceived(Message message) 
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;

foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);




As already commented in the code once the UpdateTypeFilter has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.



Any ideas how to make it nicer are welcome :)







share|improve this question











closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
If this question can be reworded to fit the rules in the help center, please edit the question.








  • 3




    There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
    – t3chb0t
    Jun 20 at 6:12













up vote
3
down vote

favorite









up vote
3
down vote

favorite











So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter. When calling the StartReceiving function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.



//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);


This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:



void OnAddedToChat(Chat chat) 
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);



Then when actually receiving updates I filter for each module according to which update type they wish to receive.



void OnMessageReceived(Message message) 
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;

foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);




As already commented in the code once the UpdateTypeFilter has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.



Any ideas how to make it nicer are welcome :)







share|improve this question











So I have implemented an application that acts as a chatbot for telegram in C#. I use a library that allows to set a filter of updates that the application whishes to receive. I assume the filter makes it more performant. This filter is passed in the StartReceiving method. My bot consists of multiple modules and each of these may require different types of updates. So I have in each module a field public IImmutableSet<UpdateType> UpdateTypeFilter. When calling the StartReceiving function at the start of the application I union the sets of all modules to see what kind of updates I need to receive and then pass that.



//need to convert back to mutable set
ISet updateTypes = Module1.UpdateTypeFilter.ToHashSet();
//NOTE: UpdateTypeFilter is static
updateTypes.Union(Module2.UpdateTypeFilter);
telegramClient.StartReceiving(updateTypes);


This requires a static field since at this point the modules have not yet been created as the bot is not part of a chat yet. The modules are created when the bot is added to a chat:



void OnAddedToChat(Chat chat) 
IList<Module> modules = new List()
new Module1(),
new Module2()
;
//chats is of type Dictionary<long, IList<Module>>
chats.Add(chat.Id, modules);



Then when actually receiving updates I filter for each module according to which update type they wish to receive.



void OnMessageReceived(Message message) 
IList<Module> modules = chats.GetValueOrDefault(message.Chat.Id);
if(modules == null)
//The bot has just been added to the chat.
OnAddedToChat(message.Chat);
return;

foreach(Module module in modules)
//Note UpdateTypeFilter cannot be static
if(module.UpdateTypeFilter.Contains(message.Type)
module.ProcessMessage(message);




As already commented in the code once the UpdateTypeFilter has to be static and once not. How can I solve this nicely? So far I just have two variables where one is called 'UpdateTypeFilterStatic' but that is not nice. I could move the filtering to the modules themselves. Then I can use the static variable both times but then it would be duplicate code in each module since I cannot generalize it into the interface. And duplicate code is not nice either.



Any ideas how to make it nicer are welcome :)









share|improve this question










share|improve this question




share|improve this question









asked Jun 19 at 22:30









findusl

1161




1161




closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
If this question can be reworded to fit the rules in the help center, please edit the question.




closed as off-topic by t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo Jun 20 at 10:31


This question appears to be off-topic. The users who voted to close gave this specific reason:


  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – t3chb0t, Billal BEGUERADJ, Toby Speight, Graipher, paparazzo
If this question can be reworded to fit the rules in the help center, please edit the question.







  • 3




    There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
    – t3chb0t
    Jun 20 at 6:12













  • 3




    There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
    – t3chb0t
    Jun 20 at 6:12








3




3




There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
– t3chb0t
Jun 20 at 6:12





There is so much missing that I doubt anyone will be able to give you any good advice. It'd be best if you posted the complete class(es) so that we can see the big-picture too.
– t3chb0t
Jun 20 at 6:12
















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Greedy Best First Search implementation in Rust

Function to Return a JSON Like Objects Using VBA Collections and Arrays

C++11 CLH Lock Implementation