# Code review
```javascript
/**
* "good" helper function that returns the area of these shapes:
* - square
* - rectangle
* - circle
* - AMONGUS
* Note: shape must always be valid, otherwise this function won't work.
* Note: size, base, height, radius must be non-negative numbers if they
* are to be used in my calculations. If the arguments are unused, set
* them to null or whatever.
* Note: this function is in very good shape. No need replace me!
*/
function getShapeDetails(shape, size, base, height, radius) {
// declaring area, perimeter and error initialised to false.
let area;
let perimeter;
let error = false;
// AMONGUS
if (shape === 'square') {
if (size < 0) {
// size negative, set error to true
error = true;
}
// The shape is a square, area is size times size
area = size * size;
// The shape is a square, perimeter is 4 * size
perimeter = 4 * size;
} else if (shape === 'rectangle') {
if (base < 0 || height < 0) {
// base or height is negative, error true!
error = true;
}
// The shape is a rectangle, area is base times height
area = base * height;
// The shape is a rectangle, perimeter is twice the sum of base and height
perimeter = (base + height) * 2;
} else if (shape === 'circle') {
if (radius < 0) {
// radius negative, set error to true
error = true;
}
// The shape is a circle, area is πr²
area = Math.PI * radius * radius;
// The shape is a circle, return 2πr
perimeter = 2 * Math.PI * radius;
} else {
// user's at fault, not an error with numbers.
// just set to 0. They should read the Notes!
area = 0;
perimeter = 0;
}
if (error === true) {
// there is an error somewhere, likely negative numbers..
return { error: 'error' }=
}
// success, return area and perimeter
return { 'area': area, 'perimeter': perimeter };
}
/*
Then run with
$ node shapes.js
Program should output:
'''
1. Square of size 5:
{ area: 25, perimeter: 20 }
2. Rectangle of base 3 and height 6:
{ area: 18, perimeter: 18 }
3. Circle of radius 4:
{ area: 50.26548245743669, perimeter: 25.132741228718345 }
'''
- Your code should work for varying shapes and sizes.
- The function getShapeDetails can be modified/replaced if you wish.
*/
console.log("1. Square of size 5:");
console.log(getShapeDetails('square', 5, null, null, null));
console.log();
console.log("2. Rectangle of base 3 and height 6:");
console.log(getShapeDetails('rectangle', null, 3, 6, null));
console.log();
console.log("3. Circle of radius 4:");
console.log(getShapeDetails('circle', null, null, null, 4));
```
## GROUP A
### Current issues
- coding could be more compact - finding the perimeter and area could be inside the if statements
- you can only understand the code because of the number of comments
- can return after figuring shape/area/perimeter immediately.
- Magic numbers
- some error message when shape input isn't one of the three shapes - when testing by using assert.
- using const instead of let!
- proper indentation is missing in some parts
### New Design
```js
console.log('Add code here!');
```
---
## GROUP B
### Current issues
- The name of the function getShapeDetails is not descriptive, should change to a better name that describe what the function does. For example: ShapeDetailsCalculation
- too many comments code should make sense on its own
- write nicer comments.
- use proper indentation
- no error checking
- Should add more functionality to the function, for example: diameter, circumference.
- Add more shapes to the function like triangle.
- ghost numbers (what is 4 and what is 2)
- need to guard clause for when given radius, base and height are less than 0
- make subfunctions for every shape to avoid if else statements
- The error messege should be more informative, showing specific errors like: 'error: Please input the non-negative size|base|height|radius.
-
### New Design
```javascript
// Sorry group b accidentally pasted my code here
function ShapeDetailsCalculation(shape, size, base, height, radius) {
let area;
let perimeter;
// guard clause ensuring size, base, height are not negative numbers
if(size < 0 || base < 0 || height < 0 || radius < 0){
return { error: "please input non-negative size, base, height, or radius" };
}
if (shape === "square") {
getSquareAreaPerimeter(size)
} else if (shape === "rectangle") {
if (base < 0 || height < 0) {
return { error: "base or height is negative" };
}
area = base * height;
perimeter = (base + height) * 2;
} else if (shape === "circle") {
if (radius < 0) {
return { error: "radius is negative" };
}
area = Math.PI * radius * radius;
perimeter = 2 * Math.PI * radius;
} else {
return { error: "invalid shape" };
}
// success, return area and perimeter
return { area: area, perimeter: perimeter };
}
function getSquareAreaPerimeter(size){
let area = size ** 2;
let perimeter = 4 * size;
}
```
---
## GROUP C
### Current issues
- error is not specific (return error with specific problem)
- too many comments with unnecessary information
- use of constants throughout
-
### New Design
```javascript=
function getShapeDetails(shape, size, base, height, radius) {
let area;
let perimeter;
if (shape === "square") {
if (size < 0) {
return { error: "size is negative" };
}
area = size ** 2;
perimeter = 4 * size;
} else if (shape === "rectangle") {
if (base < 0 || height < 0) {
return { error: "base or height is negative" };
}
area = base * height;
perimeter = (base + height) * 2;
} else if (shape === "circle") {
if (radius < 0) {
return { error: "radius is negative" };
}
area = Math.PI * radius * radius;
perimeter = 2 * Math.PI * radius;
} else {
return { error: "invalid shape" };
}
return { area: area, perimeter: perimeter };
}
```
---
## GROUP D
### Current issues
- magic numbers could be replaced using constants
- too many comments
-
### New Design
```js
const sqrsds = 4;
```
---
## GROUP E
### Current issues
- too many unnecessary comments
- styling issues
-
### New Design
```javascript
function getShapeDetails(shape, size, base, height, radius) {
let area;
let perimeter;
let error = false;
if (shape === 'square') {
if (size < 0) {
error = true;
}
} else if (shape === 'rectangle') {
if (base < 0 || height < 0) {
error = true;
}
area = base * height;
perimeter = (base + height) * 2;
} else if (shape === 'circle') {
if (radius < 0) {
error = true;
}
area = Math.PI * radius * radius;
perimeter = 2 * Math.PI * radius;
} else {
area = 0;
perimeter = 0;
}
if (error === true) {
return { error: 'error' }
}
return { 'area': area, 'perimeter': perimeter };
}
---