#Reduce cognitive complexity by removing a useless null check

5 messages · Page 1 of 1 (latest)

teal estuary
#

Hello!

I have the following code:

export function useAvailableFilters() {
  const { isLoading, isError, initialData } = useContext(InitialDataContext);
  const searchFiltersContext = useContext(SearchFiltersContext);

  const { t } = useTranslation(["Global"]);

  let group,
    province,
    city,
    timeline = null as Record<string, string> | null;

  if (!isLoading && !isError && initialData !== null) {
    if (
      searchFiltersContext.group === null ||
      searchFiltersContext.province === null ||
      searchFiltersContext.city === null ||
      searchFiltersContext.timeline === null
    ) {
      searchFiltersContext.setGroup(
        Object.entries(initialData["selectable-datapoints"].group).map(
          ([key, label]) => ({ key, label })
        )[0]
      );

      searchFiltersContext.setProvince(
        Object.entries(initialData["selectable-datapoints"].location).map(
          ([key, properties]) => ({ key, label: properties._ })
        )[0]
      );

      searchFiltersContext.setCity(
        Object.entries(
          initialData["selectable-datapoints"].location[CANADA]
        ).map(([_key, label]) => ({ key: ALL_CITIES, label }))[0]
      );

      searchFiltersContext.setTimeline(
        Object.entries(initialData["selectable-datapoints"].timeline).map(
          ([key, label]) => ({ key, label })
        )[0]
      );
    }
#
    if (
      searchFiltersContext.group !== null &&
      searchFiltersContext.province !== null &&
      searchFiltersContext.city !== null &&
      searchFiltersContext.timeline !== null
    ) {
      const selectableDatapoints = initialData["selectable-datapoints"];

      group = selectableDatapoints.group;
      province = Object.fromEntries(
        Object.entries(selectableDatapoints.location).map(([key, val]) => [
          key,
          val._,
        ])
      );
      city = [CANADA, ""].includes(searchFiltersContext.province.key ?? CANADA)
        ? {
            [ALL_CITIES]: t("specialRegions.allCities"),
          }
        : Object.fromEntries(
            Object.entries(
              selectableDatapoints.location[
                searchFiltersContext.province.key ?? CANADA
              ]
            ).map(([key, val]) =>
              key === "_"
                ? [ALL_CITIES, t("specialRegions.allCities")]
                : [key, val]
            )
          );
      timeline = selectableDatapoints.timeline;
    }
  }

  return { group, province, city, timeline };
#

SonarLint is saying that I need to reduce the cognitive complexity of this code, which I can do by removing the null check at the start of the 2nd block of code. I am setting these values to non-null values anyway in the code block right above, why do I need to null check them?

versed bolt
#

@teal estuary Not 100% I understand the context, but calling a setter won't directly affect the local values of a react state variable.

#

It'll trigger a new render that will have the new value, but won't affect the value in the current context.