Refactor the code
Have a look at this piece of code. It has a myriad of issues that reduce its readability. Refactor the code to make it more readable.
#include <iostream>
int main() {
int l = 5;
int w = 10;
int a = l * w;
std::cout << "Area of rectangle: " << a << std::endl;
int r = 7;
double c = 2 * 3.14159 * r;
std::cout << "Circumference of circle: " << c << std::endl;
return 0;
}
Hint
You can consider the following:
- Use meaningful variable names.
- Use comments to explain the purpose of the code.
- Use constants to remove magic numbers.
- Use functions to encapsulate code.
Answer
Here you can see an example of how the code could be refactored:
#include <iostream>
// Constants
const double PI = 3.14159;
int main() {
// Rectangle dimensions
int length = 5, width = 10;
int area = length * width;
std::cout << "Area of rectangle: " << area << std::endl;
// Circle radius
int radius = 7;
double circumference = 2 * PI * radius;
std::cout << "Circumference of circle: " << circumference << std::endl;
return 0;
}