xod icon indicating copy to clipboard operation
xod copied to clipboard

Simplify function compositions

Open nkrkv opened this issue 8 years ago • 0 comments
trafficstars

There are places where composition depth makes code awkward and hard to understand. E.g.

// TODO: Try to simplify this mess :-D
export const validatePatchContents = R.curry(
  (patch, project) => {
    // :: patch -> Either
    const checkNodeTypes = R.compose(
      R.ifElse(
        R.equals(false),
        Tools.err(CONST.ERROR.TYPE_NOT_FOUND),
        R.always(Either.of(patch))
      ),
      R.all(Maybe.isJust),
      R.chain(
        R.compose(
          getPatchByPath(R.__, project),
          Node.getNodeType
        )
      ),
      Patch.listNodes
    );
    // :: patch -> Either
    const checkLinks = R.compose(
      R.ifElse(
        R.compose(
          R.gt(R.__, 0),
          R.length
        ),
        R.compose(
          R.prop(0),
          R.chain(R.partialRight(checkPinKeys, [patch, project]))
        ),
        R.always(
          Either.of(patch)
        )
      ),
      Patch.listLinks
    );

    return checkNodeTypes(patch).chain(checkLinks);
  }
);

It could be simplified by extracting out nested compositions into top-level functions with clear name and signature.

ESLint xod-fp/max-composition-depth from our plugin should help. The target of max depth is 6.

nkrkv avatar Jan 23 '17 13:01 nkrkv