pmotschmann / Evolve

An incremental game about evolving a civilization
Mozilla Public License 2.0
832 stars 355 forks source link

Typing the codebase #973

Open alexjurkiewicz opened 11 months ago

alexjurkiewicz commented 11 months ago

Hi, I'm a fan of the game! I wanted to contribute. I looked into adding production tooltips for manufactured goods (plywood etc). But I'm struggling to understand the code. So instead... would you be interested in a series of patches to add type information to the codebase?

These changes would:

  1. Be non-functional. No behaviour changes for end users
  2. Avoid implementation changes. For example, vars.js modifies the global object incrementally in-place. This is hard to model with types. But these patches won't change the code, only add types. If typing vars.js proves too complex, I'd skip typing it.

Types can be added with JSDoc and checked with the Typescript compiler. Type issues would show up to developers in their editor. Example:

// Minimal typing
/**
 * @param {number} min
 * @param {number} max
 * @returns {number}
 */
Math.rand = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Optionally, write comments
/**
 * Generate a random number between `min` and `max` inclusive
 * @param {number} min Minimum value to generate
 * @param {number} max Maximum value to generate
 * @returns {number} Random number generated
 */
Math.rand = function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

Let me know if this interests you. These patches would be hundreds+ lines, so I don't want to start work unless you're interested 🙏