Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor remove_city to reduce complexity #2481

Open
XHawk87 opened this issue Jan 2, 2025 · 3 comments
Open

Refactor remove_city to reduce complexity #2481

XHawk87 opened this issue Jan 2, 2025 · 3 comments
Labels
enhancement New feature or request refactoring This issue requires code refactoring

Comments

@XHawk87
Copy link
Contributor

XHawk87 commented Jan 2, 2025

Is your feature request related to a problem? Please describe.
267 lines long, this function carries out a large number of different tasks in sequence, and it's making it difficult to read and follow the logic, and writing a unit test would be all but impossible.

Describe the solution you'd like
Refactor so that this function orchestrates and all tasks are carried out in their own functions, each of which having a single responsibility.

Describe alternatives you've considered
It could be worth looking into a declarative style, looking at what needs to happen when a city is removed in terms of a set of rules rather than a sequence of steps. This could make it more composable, and allow better separation of concerns. For example, a module responsible for keeping the great wonder info up to date can be listening for city removal and various other events in order to do so.

Additional context

void remove_city(struct city *pcity)

@lmoureaux
Copy link
Contributor

300 lines is long but it's not the worst function out there.

It could be worth looking into a declarative style, looking at what needs to happen when a city is removed in terms of a set of rules rather than a sequence of steps.

Pushing to the extreme, this could even be exposed to the ruleset (how do we rehome and disband units? do we lose part of our gold? do borders change? do we get some gold for remaining buildings?...).

For example, a module responsible for keeping the great wonder info up to date can be listening for city removal and various other events in order to do so.

This sounds like a great use case for the Qt signals/slots mechanism. We would need one QObject to emit the signal and another to receive it.

@lmoureaux lmoureaux added refactoring This issue requires code refactoring and removed Untriaged This issue or PR needs triaging labels Jan 3, 2025
@XHawk87
Copy link
Contributor Author

XHawk87 commented Jan 3, 2025

300 lines is long but it's not the worst function out there.

Well, that's true, but I don't think I should start out with attempting any big refactors. Something on the smaller end sounds about right to me.

Pushing to the extreme, this could even be exposed to the ruleset (how do we rehome and disband units? do we lose part of our gold? do borders change? do we get some gold for remaining buildings?...).

Yes! That would be fantastic.

This sounds like a great use case for the Qt signals/slots mechanism. We would need one QObject to emit the signal and another to receive it.

Very interesting. I guess it doesn't really matter if it was designed for the front-end, we can use it in server code too.

Should things like this be discussed at a higher level so we can follow a consistent approach across the project?

@lmoureaux
Copy link
Contributor

Very interesting. I guess it doesn't really matter if it was designed for the front-end, we can use it in server code too.

Actually, the server already uses it for its networking and event loop.

Should things like this be discussed at a higher level so we can follow a consistent approach across the project?

Definitely.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request refactoring This issue requires code refactoring
Projects
None yet
Development

No branches or pull requests

2 participants