phetsims / natural-selection

"Natural Selection" is an educational simulation in HTML5, by PhET Interactive Simulations
GNU General Public License v3.0
3 stars 7 forks source link

code review #203

Closed pixelzoom closed 4 years ago

pixelzoom commented 4 years ago

PhET Code-Review Checklist (a.k.a "CRC")

GitHub Issues

The following standard GitHub issues should exist. If any of these issues is missing, or have not been completed, pause code review until the issues have been created and addressed by the responsible dev.

Build and Run Checks

If any of these items fail, pause code review.

Memory Leaks

Performance

Usability

Internationalization

Repository Structure

Coding Conventions

This section deals with PhET coding conventions. You do not need to exhaustively check every item in this section, nor do you necessarily need to check these items one at a time. The goal is to determine whether the code generally meets PhET standards.

For example, both of these are acceptable:

Property.multilink(
  [ styleProperty, activeProperty, colorProperty ],
  ( style, active, color ) => {
    // some algorithm that uses style and active
} );

Property.multilink(
  [ styleProperty, activeProperty, colorProperty ],
  ( style, active ) => {
    // some algorithm that uses style and active
} );

This is not acceptable, because the 3rd parameter is incorrect.

Property.multilink(
  [ styleProperty, activeProperty, colorProperty ],
  ( style, active, lineWidth ) => {
    // some algorithm that uses style and active
} );

Documentation

This section deals with PhET documention conventions. You do not need to exhaustively check every item in this section, nor do you necessarily need to check these items one at a time. The goal is to determine whether the code generally meets PhET standards.

Type Expressions

This section deals with PhET conventions for type expressions. You do not need to exhaustively check every item in this section, nor do you necessarily need to check these items one at a time. The goal is to determine whether the code generally meets PhET standards.

Visibility Annotations

This section deals with PhET conventions for visibility annotations. You do not need to exhaustively check every item in this section, nor do you necessarily need to check these items one at a time. The goal is to determine whether the code generally meets PhET standards.

Because JavaScript lacks visibility modifiers (public, protected, private), PhET uses JSdoc visibility annotations to document the intent of the programmer, and define the public API. Visibility annotations are required for anything that JavaScript makes public. Information about these annotations can be found here. (Note that other documentation systems like the Google Closure Compiler use slightly different syntax in some cases. Where there are differences, JSDoc is authoritative. For example, use Array.<Object> or Object[] instead of Array<Object>). PhET guidelines for visibility annotations are as follows:

Math Libraries

IE11

Organization, Readability, and Maintainability

Accessibility

N/A

PhET-iO

pixelzoom commented 4 years ago

Briefly reviewed with @chrisklus via Slack.

A couple of query parameters that you might find useful:

You can also fast-forward the sim by pressing and holding the fast-forward button.

Let me know if you have any questions!

pixelzoom commented 4 years ago

@chrisklus isn't going to be able to do this, so assigning to @ariel-phet for reassignment.

pixelzoom commented 4 years ago

@jonathanolson volunteered in 8/27/2020 dev meeting (thanks!)

See the notes in https://github.com/phetsims/natural-selection/issues/203#issuecomment-677927522.

Definitely read model.md and implementation-notes.md first. They are rather extensive for this sim, and essential to understanding the implementation.

I've added notes in bold text to the CRC, with info that is specific to the sim. I've also added relevant issue numbers where I thought they'd be helpful.

pixelzoom commented 4 years ago

In https://github.com/phetsims/natural-selection/issues/208#issue-687505513, we said that code review can be completed after the 10/1 deadline for client deliverable. After code review is completed, then we'll do another dev test before starting RC testing. Code review should still be done asap. But if any major changes are recommended, they may be deferred until after the 10/1 deadline.

pixelzoom commented 4 years ago

@jonathanolson Re documentation changes like this one in ProportionsModel.js:

-     // @public counts for 'Start of Generation'
+     // @public {Property.<BunnyCounts>} counts for 'Start of Generation'
       this.startCountsProperty = new Property( BunnyCounts.withZero(), {
         valueType: BunnyCounts,
         tandem: Tandem.OPT_OUT
       } );

I'm OK leaving with changes that you've added. But my understanding of PhET convention is that if valueType or phetioType is included in options, that is considered to be documentation of the type. And we decided not to duplicate that same info with a type expression in the comment above the field definition.

Similarly, if phetioDocumentation is included in the options, there is no need for a duplicate comment describing the field.

pixelzoom commented 4 years ago

Re this review comment in Phenotype.js (which I assume applies to all IO Type definitions :)

 * REVIEW: Is there really this much boilerplate required for simple serialization?

Unfortunately, yes -- what I'm doing in NS is currently the recommended pattern. And it used to be even worse. In https://github.com/phetsims/tandem/issues/188, the iO team considered some different approaches, and got us to where we currently are. https://github.com/phetsims/tandem/issues/211 is looking at yet-another approach that will reduce the boilerplate a bit more. But that's unlikely to be something that is completed (or integrated) before our 10/1 deliverable deadline.

pixelzoom commented 4 years ago

About this review comment related to Genotype toAbbreviation:

REVIEW: Curious about why we're handling untranslated abbreviations, is it for phet-io? I never see this called with an option here

The doc for toAbbreviation says:

This is intended for debugging only. Do not rely on the format!

I've used this occasionally during development to inspect the Genotype for individual PhET-iO elements (bunnies), mainly via its use in Bunny toString. I've don't recall ever using it to get the untranslated genotype string, so I'll remove that to eliminate confusion.

Untranslated abbreviations are used in values for the query parameters that specify mutations. See introMutations and labMutations. It was decided that query parameters would use the English characters, so we don't have to deal with trying to figure out how to parse the translated equivalent of something like labMutations=fTe, and how to limit translations of allele abbreviation to be 1 glyph.

pixelzoom commented 4 years ago

Re this review comment in SelectedBunnyProperty:

REVIEW: Does this subtype exist mainly for assertions of type checking?

It was originally created to encapsulate PhET-iO ugliness like:

phetioType: PropertyIO( NullableIO( ReferenceIO( Bunny.BunnyIO ) ) ),

Then yes, it started getting used in type checking. I could delete it and replace it's definition with:

    this.selectedBunnyProperty = new Property( null, {
      tandem: options.tandem.createTandem( 'selectedBunnyProperty' ),
      phetioType: PropertyIO( NullableIO( ReferenceIO( Bunny.BunnyIO ) ) ),
      phetioDocumentation: 'the selected bunny, null if no bunny is selected'
    } );

But then I'd have to revise (or delete) the type checking. That feels like negative work at this point, so I'm going to just leave it.

EDIT: I added this to the doc:

 * This class exists mainly to hide some PhET-iO details, and to simplify type checking when it's passed around
 * as an argument to other methods.
pixelzoom commented 4 years ago

Re this comment in Shrub.js

REVIEW: Is it best to get rid of constructors that are no-op pass-through? I wouldn't have included this.

We had a discussion in developer meeting awhile back about pass-through (default) constructors. We made all developers aware of how they work in Javacript. And we left it up to developer discretion as to whether to include or omit them. When there are params involved, my preference is to include them, so I don't have to navigate up the class hierarchy searching for constructor signature and parameter documentation.

pixelzoom commented 4 years ago

Re this REVIEW comment about the doc for parseInitialPopulation.js:

 * Parses and validates the values of query parameters that describe the mutations, genotypes, and distribution
 * of the initial population. See NaturalSelectionQueryParameters for the format of the values that are being parsed.
 * See https://github.com/phetsims/natural-selection/issues/9 for design specification and history.
 * REVIEW: Having the finalized spec in the code here would be helpful.

Unfortunately there is no "finalized spec". I should probably not refer to issue #9 as "design specification", it's more like bits and pieces that might explain how we got here. The ground truth is the documentation that can be found with labMutations and labPopulation query parameters. So hopefully my revisions in the above commits are more informative:

 * Parses and validates the values of query parameters that describe the mutations, genotypes, and distribution
 * of the initial population. See NaturalSelectionQueryParameters (labMutations, labPopulation) for details about
 * the values that are being parsed. See https://github.com/phetsims/natural-selection/issues/9 for design history
 * and insight about how this feature evolved.

If that's still not sufficient, then please open an issue and I'll discuss whether the team wants to spend the time to put together a spec that matches reality.

pixelzoom commented 4 years ago

Re these REVIEW comments about Gene's constructor:

   * REVIEW: Curious about the large number of parameters, presumably we'd usually use a `config` parameter with named
   * REVIEW: required fields for this?

Excellent suggestions, done in the above commit. Gene is one of those classes that started out with a small number of fields, and eventually grew.

pixelzoom commented 4 years ago

Re this REVIEW question in Wolf dispose:

    this.disposedEmitter.dispose(); //REVIEW: why dispose this emitter? and its usage in OrganismSprites removes the listener when triggered. Just a sanity check?

I'm doing the same thing in Bunny for its 2 Emitters. I guess I've gotten in the habit of disposing Emitters because of PhET-iO. If they're instrumented, you have to dispose them or they stay registered. It's hard to know which Emitters need to be instrumented and which don't, and it changes and evolves as the designers review, so I just assume they all might be instrumented.

So... Yes, just for sanity - mine :)

pixelzoom commented 4 years ago

Re this REVIEW comment in WolfCollection.js:

//REVIEW: Does GenerationClock.constrainDt mean we can't go from one generation's before-midpoint to the next one's before-midpoint?

In the above commit, I (hopefully) improved the documentation for GenerationClock constrainDt. If I understand the question in the context of WolfCollection, yes - it prevents the clock from advancing so far in one step that we miss the transition where wolves eat. If that doesn't answer the question, please open an issue and we can discuss further.

jonathanolson commented 4 years ago

I'm OK leaving with changes that you've added. But my understanding of PhET convention is that if valueType or phetioType is included in options, that is considered to be documentation of the type. And we decided not to duplicate that same info with a type expression in the comment above the field definition.

That sounds good to me, apologies!

Similarly, if phetioDocumentation is included in the options, there is no need for a duplicate comment describing the field.

Not a need, however having to trace down where a local variable is declared, or what its derived from to find its type added effort, so it seemed easier to doc those types at the field declaration at least for my read-through of the code later.

jonathanolson commented 4 years ago

Above comments, commits and reasoning all sound great (read through and reviewed).

jonathanolson commented 4 years ago

Does implementation-notes.md adequately describe the implementation, with an overview that will be useful to future maintainers? Please provide feedback in #203.

Incredibly useful and perfect! Didn't see any changes to recommend. Thanks!

jonathanolson commented 4 years ago

Review fully done! There are 4 //REVIEW comments still in the code to respond to, but otherwise I believe everything is handled.

pixelzoom commented 4 years ago

Great - thanks for the review!

pixelzoom commented 4 years ago

All REVIEW comments have been addressed or broken out as separate issues. The ones that are separate issues will be addressed after #208 (10/1 deliverable) is done.