The International Simutrans Forum

 

Author Topic: Code maintainability  (Read 144 times)

0 Members and 1 Guest are viewing this topic.

Offline Freahk

  • Devotee
  • *
  • Posts: 1504
  • Languages: DE, EN
Code maintainability
« on: June 13, 2021, 11:55:37 AM »
I just started to refactor way construction and destruction related code to improve maintainability in the future.
Please do not yet pull this, but feel free to note any eventual issues with the code.
https://github.com/irgend42/simutrans-extended/pull/new/way-construction-refactoring
« Last Edit: June 13, 2021, 12:22:03 PM by Freahk »

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Administrator
  • *
  • Posts: 20776
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Code maintainability
« Reply #1 on: June 13, 2021, 12:39:53 PM »
I just started to refactor way construction and destruction related code to improve maintainability in the future.
Please do not yet pull this, but feel free to note any eventual issues with the code.
https://github.com/irgend42/simutrans-extended/pull/new/way-construction-refactoring

Thank you for this. Reading a code refactoring without an explanation is generally very difficult - can you give an overview of what you are doing here to give some assistance?

Offline Freahk

  • Devotee
  • *
  • Posts: 1504
  • Languages: DE, EN
Re: Code maintainability
« Reply #2 on: June 13, 2021, 12:48:34 PM »
A brief description can be found in the commit history

Quote
32fb582
 way_builder::calc_costs refactoring:
 - reuse forge cost calculation
 - reuse tree/groundobj removal cost calculation
 - improve tree/groundobj removal code (do not iterate the obj list from the start for each single tree/groundobj)

Quote
2baafff
removed code duplication of get_tree_remove_costs and remove_trees

That's, besides minor changes, all it does so far.
1. There had been a function to calculate forge cost, but way_builder::calc_costs used a copy of that code instead of calling the function.
2. There had been a function to calculate the costs of groundobj and tree removal and actually removing those.
  Further, there was different code in way_builder::calc_costs to calculate these costs without removing the obects.
  I used the visitor pattern to share that code.
  That code is based on the code from way_builder::calc_costs instead of the code from remove_trees() because I considered it to be the better implementation.

Minor changes are replacing NULL by nullptr, moving curly braces fromm its own line to the previous one, setting actually const parameters const as well as changes in karte_t::get_forge_cost.
These are:
- moving a condition out of the loop that does not depend on the iteration
- removing an unneccessary is_within_grid_limits check (lookup_kartenboden has its own check for limits and will return null if not in limits)
« Last Edit: June 13, 2021, 01:19:33 PM by Freahk »

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Administrator
  • *
  • Posts: 20776
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Code maintainability
« Reply #3 on: June 13, 2021, 01:23:03 PM »
I worry that this sort of refactoring ends up making the Extended codebase differ from the Standard codebase in places where the functionality is identical or very similar, something which I have always tried to avoid because of how much more difficult that it makes merging desirable changes from the Standard codebase into Extended.

Can I check the extent to which this is likely to be an issue with this patch? If so, it is probably worth submitting it for Standard first. If we are refactoring Extended specific code, we have much more freedom.

Offline Freahk

  • Devotee
  • *
  • Posts: 1504
  • Languages: DE, EN
Re: Code maintainability
« Reply #4 on: June 13, 2021, 02:13:01 PM »
The forge cost part shouldn't be an issue at all. As far as I know forge costs are an extended feature.
The different approach of remove_trees could be backported to standard. Then, the code difference around tree/groundobj removal shouldn't be an issue.

My intention was to first prepare the rafactoring in extended, then prepare a related refactoring in standard.
That's because some of those refactorisations are quite useful in extended but might not be needed in standard.

Offline jamespetts

  • Simutrans-Extended project coordinator
  • Administrator
  • *
  • Posts: 20776
  • Cake baker
    • Bridgewater-Brunel
  • Languages: EN
Re: Code maintainability
« Reply #5 on: June 13, 2021, 02:23:31 PM »
The forge cost part shouldn't be an issue at all. As far as I know forge costs are an extended feature.
The different approach of remove_trees could be backported to standard. Then, the code difference around tree/groundobj removal shouldn't be an issue.

My intention was to first prepare the rafactoring in extended, then prepare a related refactoring in standard.
That's because some of those refactorisations are quite useful in extended but might not be needed in standard.

Ahh, I see, yes. As long as we don't drift further out of sync over time, all should be good, and, yes, indeed, the forge cost is an Extended feature.