Created
August 19, 2021 00:46
-
-
Save MeetMartin/975978390a90ab6e22319d756d53a1a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pure function | |
// depends only on its input | |
// and produces only its output | |
const pureFunction = input => input + 1; | |
const pureFunction2 = function(input) { | |
return input + 1; | |
}; | |
// impure function | |
// depends on external state or changes it | |
// like databases, cookies, files, console... | |
let someNumber = 0; | |
const impureFunction = () => { | |
someNumber = someNumber + 1; | |
}; // mutates someNumber creating a side effect | |
// pure functions are easier to unit test | |
// because you don't have to mock, proxy, or inject | |
const identity = a => a; | |
test('identity output is the same as input.', () => { | |
expect(identity('7urtle')).toBe('7urtle'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment