Skip to content

Refactor Suggestion: Improve Readability and Maintainability of meshGenerationScript.js #27

@ferrari212

Description

@ferrari212

I was inspecting the code as part of my self-learning and found the way FEAScript handles mesh generation in meshGenerationScript.js a bit problematic, making the code hard to read and extend. I can summarize the concerns into two main issues:

  1. Misuse of class convention:
    The meshGeneration class appears to be structured more like a function than a proper class. It seems the class construct is used mainly to allow attribute sharing across different methods. The core logic resides in the generateMesh method, which could easily be a standalone function based on the current design. Furthermore, the use of a non-capitalized class name deviates from JavaScript conventions for class naming. Although technically allowed, this reinforces the impression that meshGeneration was originally intended as a function:

    export class meshGeneration {

  2. Deep if-else chains:
    The script contains more than three levels of nested if-else statements, making the logic difficult to follow. This complexity results from handling multiple conditions in a single method—for example, whether the mesh is "1D" or "2D", whether it is pre-parsed in GMSH format or not, and whether elements are linear or quadratic. This design will become even harder to manage if the mesh needs to support 3D elements or other formats such as VTK in the future. Moreover, this leads to a very large generateMesh function that violates the Single Responsibility Principle.


Proposal:
I suggest refactoring this logic by adhering to standard JavaScript class conventions. This includes using proper class inheritance—defining a base class with shared functionality and then extending it for specific cases such as 1D or 2D mesh types. This structure would make the modules more self-contained, reduce if-else complexity, and prevent code duplication in line with the DRY principle.

This refactor should not alter the behavior or output of the code from the user’s perspective. However, I believe it will significantly improve maintainability and make it easier to accommodate future extensions, such as the one discussed in issue #24.


Further question: Usage of loaders and separation between mesh parsing and data structures

I believe a more scalable approach for supporting different mesh formats would be to adopt a "loader" architecture. A good reference is how three.js handles multiple file formats—such as STL, GLTF, and VTK—through dedicated loaders. For instance, their VTK loader example demonstrates this pattern well. You can also explore the source code for the loader here:
https://github.com/mrdoob/three.js/blob/250c5f6d93b946f18103feb5a8b3dce70d1f4acd/examples/jsm/loaders/VTKLoader.js

Would such a modular approach be a good fit for the long-term architecture of FEAScript? It could promote better separation between the parsing logic and the internal mesh data structures. I’d love to hear your thoughts on this direction, @nikoscham.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or requestquestionFurther information is requested

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions