Skip to content

Code Quality improvement / Refactoring: introduce DrawTreeOptions dataclass and extract _try_pdf_to_png helper#53

Open
sruju333 wants to merge 1 commit intogambitproject:mainfrom
sruju333:refactor_draw_tree
Open

Code Quality improvement / Refactoring: introduce DrawTreeOptions dataclass and extract _try_pdf_to_png helper#53
sruju333 wants to merge 1 commit intogambitproject:mainfrom
sruju333:refactor_draw_tree

Conversation

@sruju333
Copy link

Refactoring

This PR introduces two code quality improvements to core.py based on standard refactoring techniques, submitted as part of my GSoC '26 application.

  1. Introduce Parameter Object - DrawTreeOptions dataclass
    generate_tikz, generate_pdf, generate_tex, generate_png, and draw_tree all share an identical 11-parameter signature, meaning any future addition (such as a new display option) requires the same change in five places. This PR introduces a DrawTreeOptions dataclass to consolidate those parameters in one place. Existing function signatures are untouched - this is additive only, laying the groundwork for a cleaner API going forward.

  2. Extract Method - _try_pdf_to_png helper
    generate_png contained ~50 lines of nested try/except blocks inline, making it hard to follow the function's overall logic. The three conversion attempts (ImageMagick → Ghostscript → pdftoppm) have been extracted into a private _try_pdf_to_png helper with a clear return value (True/False), reducing the call site to a single readable line. No logic has changed.

…_png helper

- Add DrawTreeOptions dataclass to consolidate the 11-parameter signature
  shared across generate_tikz, generate_pdf, generate_tex, generate_png,
  and draw_tree (Introduce Parameter Object technique)
- Extract PDF-to-PNG conversion logic from generate_png into a private
  _try_pdf_to_png helper, replacing ~50 lines of nested try/except with
  a single readable call (Extract Method technique)
@sruju333
Copy link
Author

Hello @edwardchalstrey1 - I just wanted to contribute a little more while waiting, so I decided to work on some code quality improvements in draw_tree repo!

This one has two small refactors to core.py:

  • a parameter object to consolidate the repeated 11-parameter signature
  • an extract method to clean up the nested try/except blocks in generate_png.

Both are additive and don't touch any existing functionality.
Happy to keep going on code quality improvements if there are other areas worth cleaning up! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant