Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumeration conventions #53

Closed
pixelzoom opened this issue Jan 8, 2019 · 4 comments
Closed

Enumeration conventions #53

pixelzoom opened this issue Jan 8, 2019 · 4 comments
Assignees

Comments

@pixelzoom
Copy link
Contributor

pixelzoom commented Jan 8, 2019

Proposal for Enumeration conventions:

  1. Enumerations are named like classes/types. Nothing in the name needs to identify that they are Enumerations. Example:

Orientation, not OrientationEum or OrientatonEnumeration.

  1. Enumeration values are named like constants. Example:

new Enumeration( 'HORIZONTAL', 'VERTICAL' ).

  1. If an Enumeration is closely related to some class, then make it a static field of that class. Example:
// Definition in Layout.js
LayoutBox.Orientation = new Enumeration( [ 'HORIZONTAL', 'VERTICAL' );

// Use in client
const myLayoutBox = new LayoutBox( ..., {
  orientation: LayoutBox.Orientation.HORIZONTAL
} );
  1. If an Enumeration is not closely related to some class, then put the Enumeration in its own .js file. Do not combine multiple Enumerations into one file. Example:
// Justification.js

define( require => {
  'use strict';

  // modules
  const Enumeration = require( 'PHET_CORE/Enumeration' );
  const someNamespace = require( ... );

  const Justification = new Enumeration( [ 'LEFT', 'RIGHT', 'CENTER' ] );

  return someNamespace.register( 'Justification', Justification );
} );
  1. As with other sim code, put Enumeration .js files in the subdirectory that they are related to.
@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jan 8, 2019

Some "rules of thumb" for deciding where to put an Enumeration:

  • If an Enumeration is used for a specific Property, put the Enumeration definition with the class that owns that Property. Example:
// MyModel.js

class MyModel {
  constructor() {
    // @public - the speed at which the simulation is playing
    this.playSpeedProperty = new Property( MyModel.PlaySpeed.NORMAL, {
      validValues: MyModel.PlaySpeed.VALUES
    } );
    ...
  }
}

MyModel.PlaySpeed = new Enumeration( [ 'NORMAL', 'FAST', 'SLOW' ] ); 
  • If an Enumeration is used by Properties owned by 2 or more classes, then the Enumeration should probably be in its own .js file.

  • If an Enumeration is related to a specific view component, then the Enumeration should probably be associated with that view component. Example:

// LayoutBox.js

class LayoutBox extends ... {
  constructor(..., options ) {
    options = _.extend( {
      orientation: LayoutBox.Orientation.HORIZONTAL
      ...
    }, options );
    ...
  }
}

LayoutBox.Orientation = new Enumeration( 'HORIZONTAL', 'VERTICAL' );

// client use

const layoutBox = new LayoutBox( ..., {
  orientation: LayoutBox.Orientation.VERTICAL
  ...
} );

@pixelzoom
Copy link
Contributor Author

pixelzoom commented Jan 10, 2019

1/10/19 dev meeting:

Wave Interference and Gas Properties currently use the above proposal.

Consensus is that this proposal is close enough to add to phet-software-design-patterns.md, which I'll do in phetsims/phet-info#88.

@pixelzoom
Copy link
Contributor Author

Another convention related to Enumeration:

(5) If a Property takes an Enumeration value, its validation typically looks like this:

     const cardinalDirectionProperty = new Property( CardinalDirection.NORTH, {
       validValues: CardinalDirection.VALUES
     } 

pixelzoom added a commit that referenced this issue Jan 11, 2019
@pixelzoom
Copy link
Contributor Author

Conventions have been moved to the header comment of Enumeration.js, and are referred to in phet-software-design-patterns.md.

Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant