Skip to content

Commit f14b2d9

Browse files
authored
flesh out Enumeration
1 parent 2d76757 commit f14b2d9

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

doc/phet-software-design-patterns.md

+5-11
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,15 @@ Note that the `Property` is unlinked before the child is removed from the `Node`
7373

7474
## Enumerations
7575

76-
`PHET_CORE/Enumeration`, 'rich' enumerations, `{string[]}` vs `numbers + Object.freeze`
76+
This is a standard pattern described in https://en.wikipedia.org/wiki/Enumerated_type.
7777

78-
Rich enums: https://github.com/phetsims/phet-core/issues/50
78+
PhET’s preferred implementation of this pattern can be found in [Enumeration.js](https://github.com/phetsims/phet-core/blob/master/js/Enumeration.js). Examples and coding conventions are in the comment header of that file. See the wave-interference repository for exemplars of Enumeration use.
7979

80-
```js
81-
const Justfications = new Enumeration( [ 'left', 'right', 'center' ] );
82-
const JUSTIFICATIONS = [ 'left', 'right', 'center' ];
83-
const Justiciations = { ... }; Object.freeze( Justiciations );
84-
```
85-
86-
Whether to use Enumeration exclusively is undecided, create an issue to discuss.
80+
You’ll find a couple of other patterns commonly used in PhET code. These are good to know about, but should be avoided in new code.
8781

88-
Interested developers: MK, DB, CK
82+
(1) A set of string values. For example, [Slider.js](https://github.com/phetsims/sun/blob/master/js/Slider.js) uses `’horizontal’` and `’vertical’` as the values for its `orientation` option. This approach results in the duplication of string literals throughout the code.
8983

90-
TODO: CM will flesh out for 1/14/19
84+
(2) Idiomatic JavaScript implementation, as described in [StackOverflow](https://stackoverflow.com/questions/287903/what-is-the-preferred-syntax-for-defining-enums-in-javascript). The typical implementation associates named keys with numeric values. PhET’s implementation uses string values (to facilitate debugging) and `Object.freeze` to prevent unintentional modification. See for example [SolutionType.js](https://github.com/phetsims/acid-base-solutions/blob/master/js/common/enum/SolutionType.js).
9185

9286
## Mixin & Traits
9387

0 commit comments

Comments
 (0)