Ramda Conventions - ramda/ramda GitHub Wiki
This page gathers rules followed by Ramda developers that have sublimed over the years of creating the library.
Contents
Naming Conventions
With
and By
for a name of a function properly
Determine between suffixes Function with the suffix By
must take a unary function to derive information from data it needs to return a result.
// Bad
minWith(x => mod(x), [-1, 2, 5, -7])
// Good
minBy(x => mod(x), [-1, 2, 5, -7])
On contrary, the suffix With
is used for functions that take a function with arity greater than N where N > 1
to be applied to N items from data above which the function operates.
// Bad
uniqBy((a, b) => length(a) === length(b))([[1, 2], '12', 1, 2])
// Good
uniqWith((a, b) => length(a) === length(b))([[1, 2], '12', 1, 2])
Notes:
- Function sortWith can take list of predicates.
- Neither startsWith or endsWith does apply this convention.
Reference
Designing a function
Always write pure functions
// Bad
log('Hello') // logs to console
// Good
logBy(window.console)('Hello')
Reference
Support currying
// Bad
add(1, 2) !== add(1)(2)
// Good
add(1, 2) === add(1)(2)
Reference
Prefer non-variadic functions over variadic
// bad
sum(1, 2, 3) // 6
// good
sum([1, 2, 3]) // 6
Use imperative constructs to improve performance
// bad
const incAll = (xs) => xs.map(x => x + 1)
// good
const incAll = (xs) => {
let ys = [];
for (let i = 0; i<xs.length; i++) {
ys.push(xs[i] + 1);
}
return ys;
}
Reference
Determine if your function should support transducers
Ramda's often overlooked feature is support for transducers. Take in consideration if your function should support it too.