0% found this document useful (0 votes)
74 views13 pages

Refactoring Code Snippets in C++

The document provides a series of code refactoring examples across multiple programming languages including C, C++, Java, and Python. Each example presents a question about improving code quality by removing redundancy, enhancing readability, eliminating magic numbers, avoiding memory leaks, and utilizing appropriate programming constructs such as function pointers, classes, and interfaces. Solutions are provided for each question, demonstrating best practices in code refactoring.

Uploaded by

20zerocool2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views13 pages

Refactoring Code Snippets in C++

The document provides a series of code refactoring examples across multiple programming languages including C, C++, Java, and Python. Each example presents a question about improving code quality by removing redundancy, enhancing readability, eliminating magic numbers, avoiding memory leaks, and utilizing appropriate programming constructs such as function pointers, classes, and interfaces. Solutions are provided for each question, demonstrating best practices in code refactoring.

Uploaded by

20zerocool2021
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C:

Simple:

Question:
Refactor the following code snippet to remove redundancy:

int add(int a, int b) {


return a + b;
}
int subtract(int a, int b) {
return a - b;
}

Solution:
int operate(int a, int b, char op) {
switch(op) {
case '+':
return a + b;
case '-':
return a - b;
default:
return 0; // Error
}
}

Question:
Refactor the following code snippet to make it more readable:
for(int i=0;i<10;i++)printf("%d\n",i);

Solution:
for(int i = 0; i < 10; i++) {
printf("%d\n", i);
}

Moderate:
Question:
Refactor the following code snippet to remove magic numbers:
if (x > 100 && x < 200) {
// do something
}

Solution:
#define LOWER_LIMIT 100
#define UPPER_LIMIT 200
if (x > LOWER_LIMIT && x < UPPER_LIMIT) {
// do something
}

Question:
Refactor the following code snippet to avoid memory leaks:
char *str = (char *)malloc(20 * sizeof(char));
// do something with str
free(str);

Solution:
char *str = (char *)malloc(20 * sizeof(char));
if (str != NULL) {
// do something with str
free(str);
}

Hard:
Question:
Refactor the following code snippet to use function pointers:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}

Solution:
int operation(int a, int b, int (*func)(int, int)) {
return func(a, b);
}

Example to prev. Q:

#include <stdio.h>
// Define a function add that takes two integers and returns their sum
int add(int x, int y) {
return x + y;
}
// Define a function subtract that takes two integers and returns their difference
int subtract(int x, int y) {
return x - y;
}
// Define the operation function
int operation(int a, int b, int (*func)(int, int)) {
return func(a, b);
}
int main() {
int result;
// Call the operation function with the add function as the third argument
result = operation(10, 5, add);
printf("Result of addition: %d\n", result);
// Call the operation function with the subtract function as the third argument
result = operation(10, 5, subtract);
printf("Result of subtraction: %d\n", result);
return 0;
}

Question:
Refactor the following code snippet to use structures effectively:
int x, y;
scanf("%d %d", &x, &y);

Solution:
typedef struct {
int x;
int y;
} Point;
Point p;
scanf("%d %d", &p.x, &p.y);

C++:
Simple:
Question:
Refactor the following code snippet to remove redundancy:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}

Solution:
int operate(int a, int b, char op) {
switch(op) {
case '+':
return a + b;
case '-':
return a - b;
default:
return 0; // Error
}
}

Question:
Refactor the following code snippet to make it more readable:

for(int i=0;i<10;i++)cout << i << endl;


Solution:
for(int i = 0; i < 10; i++) {
cout << i << endl;
}

Moderate:
Question:
Refactor the following code snippet to remove magic numbers:
if (x > 100 && x < 200) {
// do something
}

Solution:
#define LOWER_LIMIT 100
#define UPPER_LIMIT 200
if (x > LOWER_LIMIT && x < UPPER_LIMIT) {
// do something
}

Question:
Refactor the following code snippet to avoid memory leaks:
char *str = new char[20];
// do something with str
delete[] str;

Solution:
char *str = new char[20];
if (str != nullptr) {
// do something with str
delete[] str;
}

Hard:
Question:
Refactor the following code snippet to use function pointers:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}

Solution:
int operation(int a, int b, int (*func)(int, int)) {
return func(a, b);
}

Question:
Refactor the following code snippet to use classes effectively:
int x, y;
cin >> x >> y;

Solution:
class Point {
public:
int x;
int y;
};

Point p;
cin >> p.x >> p.y;

Java:
Simple:
Question:
Refactor the following code snippet to remove redundancy:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}

Solution:
int operate(int a, int b, char op) {
switch(op) {
case '+':
return a + b;
case '-':
return a - b;
default:
return 0; // Error
}
}

Question:
Refactor the following code snippet to make it more readable:
for(int i=0;i<10;i++) [Link](i);

Solution:
for(int i = 0; i < 10; i++) {
[Link](i);
}

Moderate:
Question:
Refactor the following code snippet to remove magic numbers:
if (x > 100 && x < 200) {
// do something
}

Solution:
final int LOWER_LIMIT = 100;
final int UPPER_LIMIT = 200;
if (x > LOWER_LIMIT && x < UPPER_LIMIT) {
// do something
}

Question:
Refactor the following code snippet to avoid memory leaks:
String str = new String("Hello");
// do something with str
str = null;

Solution:
String str = new String("Hello");
// do something with str
str = null; // Not necessary in Java due to automatic garbage collection

Hard:
Question:
Refactor the following code snippet to use interfaces effectively:
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}

Solution:
interface Operation {
int operate(int a, int b);
}
class Add implements Operation {
public int operate(int a, int b) {
return a + b;
}
}
class Subtract implements Operation {
public int operate(int a, int b) {
return a - b;
}
}

Question:
Refactor the following code snippet to use object-oriented principles:
int x, y;
Scanner scanner = new Scanner([Link]);
x = [Link]();
y = [Link]();

Solution:
class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
}
Scanner scanner = new Scanner([Link]);
Point p = new Point([Link](), [Link]());

Python:
Simple:
Question:
Refactor the following code snippet to remove redundancy:
def add(a, b):
return a + b
def subtract(a, b):
return a - b

Solution:
def operate(a, b, op):
if op == '+':
return a + b
elif op == '-':
return a - b
else:
return 0 # Error

Question:
Refactor the following code snippet to make it more readable:
for i in range(10): print(i)

Solution:
for i in range(10):
print(i)

Moderate:
Question:
Refactor the following code snippet to remove magic numbers:
if x > 100 and x < 200:
# do something

Solution:
LOWER_LIMIT = 100
UPPER_LIMIT = 200
if x > LOWER_LIMIT and x < UPPER_LIMIT:
# do something

Question:
Refactor the following code snippet to avoid memory leaks:
str = "Hello"
# do something with str
str = None

Solution:
No need to refactor for memory management. Python's garbage collector handles it.

Hard:
Question:
Refactor the following code snippet to use lambda functions effectively:
def add(a, b):
return a + b
def subtract(a, b):
return a - b

Solution:
operate = lambda a, b, func: func(a, b)

Question:
Refactor the following code snippet to use classes effectively:
x, y = map(int, input().split())

Solution:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
x, y = map(int, input().split())
p = Point(x, y)

You might also like