Middle+
How do you determine that the code is written with quality?
sobes.tech AI
Answer from AI
High-quality code is concise, readable, and understandable by other developers. It follows generally accepted conventions and coding standards.
Main criteria for code quality:
- Readability and maintainability: Easy to read, understand, and modify.
- Performance: Efficiently uses resources and executes quickly.
- Testability: Written in a way that makes it easy to cover with tests.
- Reliability: Works predictably and contains no critical errors.
- Scalability: Easy to extend and improve functionality.
Tools and practices to ensure quality:
- Linters and formatters: eslint, prettier for automatic code checking and formatting.
- Writing tests: Unit, integration, E2E tests.
- Code review: Peer review of code to identify potential issues.
- Following design patterns and SOLID principles.
- Good documentation: Explanation of complex code sections and APIs.
Example of poorly readable code:
// Bad code
function procData(d) {
let res = [];
for (let i = 0; i < d.length; i++) {
if (d[i].val > 0) {
res.push(d[i].val * 2);
}
}
return res;
}
Example of quality code with the same functions:
// Good code. Using clear names and array methods
function processPositiveValuesDoubled(data) {
return data
.filter(item => item.value > 0) // Filter elements with positive value
.map(item => item.value * 2); // Double the value
}