Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
Tutorial -3
Question:
1. Define the pure function/method. Compare it to impure function/method.
2. Appraise the concept of “side effects”. Explain TWO (2) side effects in any programming
language with code examples.
Side effects in programming refer to any changes or interactions that a function or expression
has with its environment outside of its return value. These changes can include modifying
global variables, reading from or writing to files or databases, printing to the console, or even
altering the state of the program itself.
Ex1. I/O
Level 3 Asia Pacific University of Technology and Innovation Page 1 of 2
Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
Ex2. Add an Old Value to a New One
3. Given the Java code as follows. Explain the effect of code design.
public class Demo {
public static void main(String[] args) {
//demo
Circle c1 = new Circle();
[Link]( 10 );
[Link]( "=== Area, Circumference, Diameter ===" );
[Link]();
[Link]( "Area: " + [Link]);
[Link]();
[Link]( "Circumference: " + [Link]);
[Link]();
[Link]( "Diameter: " + [Link]);
}
class Circle{
int radius;
double area;
double circumference;
double diameter;
//getter
public void setRadius(int radius) {
[Link] = radius;
}
public void area() {
if( radius > 0 ) {
[Link] = [Link] * [Link](radius, 2);
}
}
public void circumference() {
if( radius > 0 ) {
[Link] = 2 * [Link] * radius;
}
}
public void diameter() {
if( radius > 0 ) {
[Link] = 2 * radius;
Level 3 Asia Pacific University of Technology and Innovation Page 2 of 2
Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
}
}
The code design lacks proper encapsulation, directly modifying instance variables within
the Circle class. This can lead to unexpected behavior when methods are called multiple
times with different radii. An improved design would encapsulate properties and return
them from methods ("Getter" pattern), promoting better control and reuse.
4. Justify the [Link]() from the perspective of functional programming paradigm.
[Link]() is not actually purely randomised, it relies on an external factor, which is the
current state of the random number generator to produce different outputs. Therefore, Math.
random() will introduce impurity and non-determinism into the code. However,
encapsulating the impure codes and separating them from the rest of the codebase can still
allow [Link]() to be used while maintaining the benefits of the functional
programming paradigm.
5. Improve this code snippet in Java/Javascript.
let PI = 3.14;
const calculateArea = (radius) => radius * radius * PI;
[Link]( calculateArea(10) );
Level 3 Asia Pacific University of Technology and Innovation Page 3 of 2
Advanced Programming Language Concepts (CT006-3-3) Purity and Side Effects
6. Discuss in group the impact of purity in computer programming.
Readability: Pure functions enhance code clarity by avoiding state changes and relying solely
on input.
Concurrency: Immutability and no side effects simplify concurrent programming, reducing
errors.
Predictability: Debugging and testing become easier with consistent, unchangeable output from
pure functions.
Reusability: Higher-order functions enable code components to be reused across different
scenarios, improving efficiency.
Given an array list as follows. Write a Javascript statement for the following requirements.
[12,34,21,4,56,77,88,44,885,2,5,7,98,54]
7. Iterate and display through all elements of array list.
8. Search and display an element, ie., 885.
9. Double up each array element by 2 and store them in another array list.
Level 3 Asia Pacific University of Technology and Innovation Page 4 of 2