Skip to content

Commit c13bb37

Browse files
committed
refactor: remove dead code and optimize sayHello function
1 parent 3372770 commit c13bb37

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

Sprint-3/3-dead-code/exercise-1.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,40 @@
11
// Find the instances of unreachable and redundant code - remove them!
22
// The sayHello function should continue to work for any reasonable input it's given.
33

4-
let testName = "Jerry";
4+
5+
// DEAD CODE
6+
// let testName = "Jerry";
7+
// const greeting = "hello";
8+
9+
// function sayHello(greeting, name) {
10+
// // const greetingStr = greeting + ", " + name + "!";
11+
// return `${greeting}, ${name}!`;
12+
// console.log(greetingStr);
13+
// }
14+
15+
// // testName = "Aman";
16+
17+
// const greetingMessage = sayHello(greeting, testName);
18+
19+
// console.log(greetingMessage); // 'hello, Aman!'
20+
21+
/**
22+
"Clean Code Refactoring: SayHello Function"
23+
24+
Removed Dead Data: Eliminated the unreachable variable value "Jerry" to keep memory clean.
25+
Enforced Immutability: Switched from let to const for variables that do not change, preventing accidental reassignments.
26+
Optimized Variable Scope: Reorganized the declaration order to ensure variables are defined before they are called.
27+
Simplified Function Logic: Removed redundant intermediate variables (greetingStr) inside the function to return the result directly.
28+
Streamlined Output: Removed internal console.log statements after testing to keep the console clean and focused on the final output.
29+
Execution: Properly invoked the function by passing the correct parameters for the final print.
30+
*/
31+
532
const greeting = "hello";
33+
const testName = "Aman";
634

735
function sayHello(greeting, name) {
8-
const greetingStr = greeting + ", " + name + "!";
936
return `${greeting}, ${name}!`;
10-
console.log(greetingStr);
37+
1138
}
1239

13-
testName = "Aman";
14-
15-
const greetingMessage = sayHello(greeting, testName);
16-
17-
console.log(greetingMessage); // 'hello, Aman!'
40+
console.log(sayHello(greeting, testName)); // 'hello, Aman!'

0 commit comments

Comments
 (0)