Skip to content

fix: Global mutable state in fortplot_text module violates state management principles #144

@krystophny

Description

@krystophny

Problem

The fortplot_text module contains global mutable state that violates the mandatory state management principles, creating potential thread-safety issues and making the code harder to test and maintain.

Evidence

Global mutable state found in src/fortplot_text.f90:

! Module state  
type(stb_fontinfo_t) :: global_font
logical :: font_initialized = .false.
real(wp) :: font_scale = 0.0_wp

The architecture principles explicitly state:

  • 'MUTABLE GLOBAL STATE IS THE SOURCE OF ALL EVIL AND MUST BE AVOIDED AT ALL COST'
  • 'NO GLOBAL MUTABLE STATE - All state must be explicitly passed as parameters'
  • 'STATELESS OPERATIONS - Functions should not rely on hidden global state'

Impact

  • Thread-safety issues in concurrent environments
  • Hidden dependencies on global state
  • Difficult to test in isolation
  • Potential for race conditions
  • Violates functional programming principles
  • Makes it impossible to have multiple font configurations

Required Fix

  1. Encapsulate font state in a text_context type
  2. Pass context explicitly to all text functions
  3. Initialize context at figure/backend level
  4. Remove all module-level mutable variables
  5. Make all operations pure functions with explicit parameters

Example Refactoring

type :: text_context_t
    type(stb_fontinfo_t) :: font
    logical :: initialized = .false.
    real(wp) :: scale = 0.0_wp
end type

! Pure function with explicit context
function render_text(context, text, x, y) result(bitmap)
    type(text_context_t), intent(inout) :: context
    ! ...
end function

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions