Ī» pure functions by Per Arneng 2015-02-08
pure
functions
pure
functions
Ī» pure functions by Per Arneng 2015-02-08
pure functions
ā— It always evaluates the same result value given
the same argument value(s)
ā— The result value cannot depend on any hidden
information or state that may change as
program execution proceeds or between
different executions of the program
ā— It can not depend on any external input from I/O
devices
pj f ŋk nsʊɚ ə ŹƒÉ™
Ī» pure functions by Per Arneng 2015-02-08
pure functions
ā— It always evaluates the same result value given
the same argument value(s)
ā— The result value cannot depend on any hidden
information or state that may change as
program execution proceeds or between
different executions of the program
ā— It can not depend on any external input from I/O
devices
pj f ŋk nsʊɚ ə ŹƒÉ™
Ī» pure functions by Per Arneng 2015-02-08
pure functions
ā— It always evaluates the same result value given
the same argument value(s)
ā— The result value cannot depend on any hidden
information or state that may change as
program execution proceeds or between
different executions of the program
ā— It can not depend on any external input from I/O
devices
pj f ŋk nsʊɚ ə ŹƒÉ™
Ī» pure functions by Per Arneng 2015-02-08
ex:
int addOne(int value) {
return value + 1;
}
Ī» pure functions by Per Arneng 2015-02-08
impure:
void addOne(int *value) {
*value = *value = 1;
}
Ī» pure functions by Per Arneng 2015-02-08
impure:
void addOne(int *value) {
*value = *value = 1;
}
The argument is mutated
Ī» pure functions by Per Arneng 2015-02-08
impure 2:
int addOne(int value) {
int newValue = value + 1;
file.write(newValue);
return newValue;
}
Ī» pure functions by Per Arneng 2015-02-08
impure 2:
int addOne(int value) {
int newValue = value + 1;
file.write(newValue);
return newValue;
}
Could fail making the function
non deterministic and impure
Ī» pure functions by Per Arneng 2015-02-08
impure 2:
int addOne(int value) {
int newValue = value + 1;
file.write(newValue);
return newValue;
}
Using a hidden state
Ī» pure functions by Per Arneng 2015-02-08
advantages
ā— Easy to test
ā— Easy to reuse
ā— Easy to reason about (read, understand)
ā— Perfect for use in multithreading
Ī» pure functions by Per Arneng 2015-02-08
testability
ā— You do not need to mock hidden state resulting
in smaller and faster tests
ā— You only need to focus and arguments and
return values
ā— Tests that are not mocked are executed faster
than mocked ones. For hundreds of tests it will
matter
Ī» pure functions by Per Arneng 2015-02-08
maintainability
ā— Usually smaller blocks of code that can just be
copied out and reused in another context
ā— Easy to understand and reason about so you
are less likely to feel that you need to write your
own
ā— Easy to use as building blocks for other
functions and methods as inline or passed as
parameters
Ī» pure functions by Per Arneng 2015-02-08
readability
ā— When a pure function is used you know that it
will not modify anything making it easier to read
that code
ā— When you read a pure function your brain does
not have to keep external state in mind
Ī» pure functions by Per Arneng 2015-02-08
multithreading
ā— A pure function does not mutate it's argument
or make use of any hidden state so its easier to
use in a multithreaded context without having to
resort to synchronization
Ī» pure functions by Per Arneng 2015-02-08
more
ā— https://github.com/PerArneng/pfnomock
A repo showing a pure and non pure function
for handling transfers between accounts and
what that means in terms of testing with and
without mocking

Pure functions

  • 1.
    Ī» pure functionsby Per Arneng 2015-02-08 pure functions pure functions
  • 2.
    Ī» pure functionsby Per Arneng 2015-02-08 pure functions ā— It always evaluates the same result value given the same argument value(s) ā— The result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program ā— It can not depend on any external input from I/O devices pj f ŋk nsʊɚ ə ŹƒÉ™
  • 3.
    Ī» pure functionsby Per Arneng 2015-02-08 pure functions ā— It always evaluates the same result value given the same argument value(s) ā— The result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program ā— It can not depend on any external input from I/O devices pj f ŋk nsʊɚ ə ŹƒÉ™
  • 4.
    Ī» pure functionsby Per Arneng 2015-02-08 pure functions ā— It always evaluates the same result value given the same argument value(s) ā— The result value cannot depend on any hidden information or state that may change as program execution proceeds or between different executions of the program ā— It can not depend on any external input from I/O devices pj f ŋk nsʊɚ ə ŹƒÉ™
  • 5.
    Ī» pure functionsby Per Arneng 2015-02-08 ex: int addOne(int value) { return value + 1; }
  • 6.
    Ī» pure functionsby Per Arneng 2015-02-08 impure: void addOne(int *value) { *value = *value = 1; }
  • 7.
    Ī» pure functionsby Per Arneng 2015-02-08 impure: void addOne(int *value) { *value = *value = 1; } The argument is mutated
  • 8.
    Ī» pure functionsby Per Arneng 2015-02-08 impure 2: int addOne(int value) { int newValue = value + 1; file.write(newValue); return newValue; }
  • 9.
    Ī» pure functionsby Per Arneng 2015-02-08 impure 2: int addOne(int value) { int newValue = value + 1; file.write(newValue); return newValue; } Could fail making the function non deterministic and impure
  • 10.
    Ī» pure functionsby Per Arneng 2015-02-08 impure 2: int addOne(int value) { int newValue = value + 1; file.write(newValue); return newValue; } Using a hidden state
  • 11.
    Ī» pure functionsby Per Arneng 2015-02-08 advantages ā— Easy to test ā— Easy to reuse ā— Easy to reason about (read, understand) ā— Perfect for use in multithreading
  • 12.
    Ī» pure functionsby Per Arneng 2015-02-08 testability ā— You do not need to mock hidden state resulting in smaller and faster tests ā— You only need to focus and arguments and return values ā— Tests that are not mocked are executed faster than mocked ones. For hundreds of tests it will matter
  • 13.
    Ī» pure functionsby Per Arneng 2015-02-08 maintainability ā— Usually smaller blocks of code that can just be copied out and reused in another context ā— Easy to understand and reason about so you are less likely to feel that you need to write your own ā— Easy to use as building blocks for other functions and methods as inline or passed as parameters
  • 14.
    Ī» pure functionsby Per Arneng 2015-02-08 readability ā— When a pure function is used you know that it will not modify anything making it easier to read that code ā— When you read a pure function your brain does not have to keep external state in mind
  • 15.
    Ī» pure functionsby Per Arneng 2015-02-08 multithreading ā— A pure function does not mutate it's argument or make use of any hidden state so its easier to use in a multithreaded context without having to resort to synchronization
  • 16.
    Ī» pure functionsby Per Arneng 2015-02-08 more ā— https://github.com/PerArneng/pfnomock A repo showing a pure and non pure function for handling transfers between accounts and what that means in terms of testing with and without mocking