phetsims / griddle

Dynamic charting library built with Scenery
MIT License
2 stars 4 forks source link

XYCursorChartNode code review and getDataExists bug #70

Closed pixelzoom closed 3 years ago

pixelzoom commented 4 years ago

Added to XYCursorPlot in by @jessegreenberg in 7fe14e2063bb6b3d3897b5794f012e211b00c276:

  /**
   * Returns true if any data is attached to this plot.
   * @returns {boolean}
   * @public
   */
  getDataExists() {
    let dataExists = false;
    this.valueSeriesListenerMap.forEach( ( listener, dataSeries, map ) => {
      dataExists = dataSeries.hasData();

      // break early
      if ( dataExists ) {
219     return;
      }
    } );

    return dataExists;
  }

This function returns boolean, and line 219 is violating that -- and probably returning falsy!

pixelzoom commented 4 years ago

Also note:

(1) Functions with multiple return points are (in general) an anti-pattern to be avoided.

(2) If you are going to use multiple return points, there's no need for dataExists:

  getDataExists() {
    this.valueSeriesListenerMap.forEach( ( listener, dataSeries, map ) => {
      if ( dataSeries.hasData() ) {
        return true;
      }
    }
    return false;
  }
pixelzoom commented 4 years ago

Since you're only looking at dataSeries (the keys), you should probably be iterating over this.valueSeriesListenerMap.keys, like (untested):

  getDataExists() {
    this.valueSeriesListenerMap.keys().forEach( dataSeries => {
      if ( dataSeries.hasData() ) {
        return true;
      }
    } );
    return false;
  }

Also...

    // @private - Keep track of listeners for each series so the listeners can be removed when the series is removed
    this.valueSeriesListenerMap = new Map();
pixelzoom commented 4 years ago

Sorry to turn this into a code review, but I'm in here because of https://github.com/phetsims/fourier-making-waves/issues/3.

There is confusing inconsistency with how you refer to the Map keys. The name this.valueSeriesListenerMap leads me to believe that the keys are "valueSeries". They are also named as parameters dataSeries and dynamicSeries, and put in this.dynamicSeriesList. They are of type DynamicSeries, so dynamicSeries is correct and should be used throughout.

After I untangled that naming...

getDataExists() {
  return _.some( this.dynamicSeries.list, dynamicSeries => dynamicSeries.hasData() );
}

EDIT: ... and renamed to hasData.

pixelzoom commented 4 years ago

EDIT: I made it @protected (read-only) and renamed to dynamicSeriesArray (an array is not a list, they are different data structures).

pixelzoom commented 4 years ago
pixelzoom commented 4 years ago
pixelzoom commented 4 years ago

I've converted actionable things in the above comments to checklist items.

pixelzoom commented 4 years ago

Probably faster if I make these changes and have @jessegreenberg review. Self assigning.

pixelzoom commented 4 years ago

Co-locate addDynamicSeries and removeDynamicSeries in the source file. Why are they separated by almost 100 lines?

pixelzoom commented 4 years ago
pixelzoom commented 4 years ago
pixelzoom commented 4 years ago

More...

pixelzoom commented 4 years ago

An issue, throughout griddle and spilling over into sims, is terminology. I see "plot", "chart", and "graph" being used interchangeably. A plot is not a chart. A plot displays a data set (aka data series). A chart displays one or more plots. Names like XYCursorPlot and ESP.EnergyPlot are incorrect and misleading - they are charts, not plots. I'll create a separate issue for terminology.

So... Stopping here with changes to XYCursorPlot. @jessegreenberg please review.

jessegreenberg commented 4 years ago

Thanks for all of this cleanup, changes are a big improvement. Since we were are discussing terminology in #71, I noticed a few things got changed from "plot" -> "chart", like chartWidth, chartHeight, chartPanel. But we decided that XYCursorPlotNode is a "plot" in #71. Should these all be renamed to plot for consistency?

pixelzoom commented 4 years ago

No, those names should definitely be "chart". See https://github.com/phetsims/griddle/issues/71#issuecomment-697106346.

pixelzoom commented 3 years ago

Note that XYCursorPlot is now named XYCursorChartNode.

samreid commented 3 years ago

It seems everything is checked off here except for things that were moved to other issues. I reviewed the commit messages and didn't see any trouble. Closing.