Code quality refers to the overall quality of software code, including its readability, maintainability, efficiency, and adherence to best practices. High-quality code is essential for long-term project success and team collaboration.
// Poor Quality Code
function calc(a,b,t) {
var r;
if(t==1) r = a+b;
if(t==2) r = a-b;
if(t==3) r = a*b;
if(t==4) {
if(b!=0) r = a/b;
else r = "error";
}
return r;
}
// High Quality Code
enum Operation {
ADD = 1,
SUBTRACT = 2,
MULTIPLY = 3,
DIVIDE = 4
}
function calculate(a: number, b: number, operation: Operation): number {
switch (operation) {
case Operation.ADD:
return a + b;
case Operation.SUBTRACT:
return a - b;
case Operation.MULTIPLY:
return a * b;
case Operation.DIVIDE:
if (b === 0) {
throw new Error('Division by zero');
}
return a / b;
default:
throw new Error('Invalid operation');
}
}