Skip to content

Suggestions for Enhancing Code Organization and Quality #804

@alexlwn123

Description

@alexlwn123

Hi everyone!

I'm Alex, I've recently started diving into the Stacker News codebase. As I've been getting more familiar with the repo, I've identified a few opportunities that I believe could help us improve the codebase's organization, maintainability, and overall development experience.

As these ideas carry architectural significance, I'm sharing these now as an RFC to ensure we reach consensus before moving forward with substantial implementation efforts. Also, I'm happy to contribute or lead development on any of these that community deems appropriate.

Code Organization & Quality Ideas

1. Component Directory Organization

The current flat structure of the components directory seems a bit challenging to navigate due to its size. By organizing components into feature-based directories, we could enhance discoverability and maintainability of our components.

As a rule of thumb, components that appear close to each other on screen should have files close to each other in the codebase. (components/item, components/profile, components/common, etc.)

2. Breaking up Component Files

Currently, many component files actually contain 5+ composite components that contribute to a larger component see here. I suggest that as a practice we should shoot for one component per file (with reasonable exceptions). While this may introduce file clutter, by first implementing the proposed feature-based directory structure, we can keep the repository organized and navigable.

3. Type Safety

The project's reliance on vanilla JavaScript brings some challenges in understanding and extending the codebase. To help DX and reduce the overhead of investigating features or bugs, I suggest we incrementally introduce type safety.

While migrating to TypeScript is an option, a more immediate and less disruptive approach could be to incrementally introduce JSDocs. JSDocs inherit most of the features of TypeScript that most devs care about, namely editor type inference and better linting.

This might seem like a monster, but JSDocs can be added incrementally and have zero effect/risk on runtime. Also, we can make significant progress quickly by starting with the GraphQL hooks/fragments and using a tool like GraphQL Code Generator's JSDoc plugin.

4. Test Coverage

While complete coverage is certainly overkill, I think the project would benefit from a modest amount of frontend integration testing being added to the CI pipeline. A ton of tricky bugs can be caught through snapshot testing (via the React Testing Library.

With frontend testing, I think a little goes a long way. Just adding a check that each page mounts to the CI will catch a ton of bugs. In addition, this really helps with confidence that new features won’t break the site, especially as more contributors join.


I'm looking forward to your thoughts, feedback, and any additional ideas you might have! As mentioned, I'm happy to contribute or lead development on any of these ideas community deems appropriate.

Cheers.


edit: see additions

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementimprovements to existing features

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions