Skip to content

Code Quality: JNexusSwing.java is too large (1446 lines) - needs refactoring #27

@sfloess

Description

@sfloess

Code Quality - LOW Priority

Problem

JNexusSwing.java is 1446 lines long, making it difficult to maintain, test, and understand. This violates the Single Responsibility Principle.

File Size Comparison

1446 lines - JNexusSwing.java    ⚠️ TOO LARGE
 830 lines - CredentialsTest.java (test file - OK)
 811 lines - NexusService.java   
 797 lines - JNexusAWT.java
 786 lines - NexusClient.java
 783 lines - Credentials.java
 666 lines - JNexusUI.java
 627 lines - JNexus.java

Industry standard: Classes should typically be <500 lines

What's in JNexusSwing.java?

Based on the code structure:

  1. Main application logic (~100 lines)
  2. UI initialization (~200 lines)
  3. Event handlers (~300 lines)
  4. Table management (~150 lines)
  5. Advanced filters panel (~150 lines)
  6. Statistics dialog (~250 lines)
  7. Delete confirmation (~100 lines)
  8. Credential dialog (~200 lines)
  9. Helper methods (~100 lines)

Total: ~1450 lines - consistent with actual size

Problems with Large Classes

  1. Hard to navigate - Developers spend time scrolling
  2. Hard to test - Too many responsibilities
  3. High coupling - Everything depends on everything
  4. Merge conflicts - Multiple developers editing same file
  5. Hard to reuse - Can't extract components
  6. Violates SRP - UI, logic, events all mixed

Recommended Refactoring

Option 1: Extract Dialogs

Create separate classes for dialogs:

JNexusSwing.java (800 lines)
├── CredentialsDialog.java (200 lines) - NEW
├── StatisticsDialog.java (250 lines) - NEW
├── AdvancedFiltersPanel.java (200 lines) - NEW
└── DeleteConfirmationDialog.java (100 lines) - NEW

Option 2: Extract Event Handlers

Separate UI from logic:

JNexusSwing.java (600 lines)
├── SwingEventHandlers.java (400 lines) - NEW
└── SwingTableModel.java (200 lines) - NEW

Option 3: Extract Table Management

JNexusSwing.java (900 lines)
├── ComponentTablePanel.java (300 lines) - NEW
├── TableModelFactory.java (150 lines) - NEW
└── TableRenderers.java (100 lines) - NEW

Option 4: Full MVC Refactoring (Recommended)

JNexusSwing.java (300 lines) - Main window only
├── model/
│   └── SwingComponentModel.java (150 lines)
├── view/
│   ├── ComponentTableView.java (200 lines)
│   ├── FiltersPanelView.java (200 lines)
│   └── StatisticsDialogView.java (250 lines)
└── controller/
    └── SwingController.java (300 lines)

Example Extraction

Before (all in JNexusSwing.java):

public class JNexusSwing {
    // 1446 lines of everything
    
    private static Credentials showCredentialDialog() {
        JPanel panel = new JPanel();
        // 200 lines of dialog code
    }
    
    private void showStatisticsDialog() {
        JDialog dialog = new JDialog();
        // 250 lines of stats code
    }
}

After:

// JNexusSwing.java - 800 lines
public class JNexusSwing {
    private void collectCredentials() {
        CredentialsDialog dialog = new CredentialsDialog(frame);
        Credentials creds = dialog.show();
    }
    
    private void showStatistics() {
        StatisticsDialog dialog = new StatisticsDialog(frame, stats);
        dialog.setVisible(true);
    }
}

// CredentialsDialog.java - 200 lines
public class CredentialsDialog extends JDialog {
    public Credentials show() {
        // Focused dialog logic only
    }
}

// StatisticsDialog.java - 250 lines
public class StatisticsDialog extends JDialog {
    public StatisticsDialog(JFrame parent, RepositoryStats stats) {
        // Focused statistics display
    }
}

Benefits

  1. Easier to understand - Each class has one job
  2. Easier to test - Test dialogs independently
  3. Easier to reuse - Dialogs can be used elsewhere
  4. Easier to modify - Changes isolated to one class
  5. Better organization - Clear separation of concerns
  6. Fewer merge conflicts - Developers work on different files

Implementation Steps

Phase 1: Extract Dialogs (Low Risk)

  1. Extract CredentialsDialog (200 lines)
  2. Extract StatisticsDialog (250 lines)
  3. Extract AdvancedFiltersPanel (200 lines)
  4. Update JNexusSwing to use new classes
  5. Test thoroughly

Result: JNexusSwing reduced to ~800 lines

Phase 2: Extract Table Management (Medium Risk)

  1. Create ComponentTableModel
  2. Create TableRenderers
  3. Move table logic to new classes
  4. Test table functionality

Result: JNexusSwing reduced to ~500 lines

Phase 3: Extract Event Handlers (Higher Risk)

  1. Create SwingController
  2. Move event handlers
  3. Wire up events
  4. Test all interactions

Result: JNexusSwing reduced to ~300 lines

Testing Strategy

  • Extract one class at a time
  • Keep original code commented out temporarily
  • Run full GUI test after each extraction
  • Compare before/after screenshots

Priority

Low - Code works fine, this is a maintainability improvement

Related

  • Modern Swing apps use cleaner architecture
  • Consider looking at MVC frameworks for Swing
  • AWT (797 lines) and Terminal UI (666 lines) could benefit too

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions