Code quality concepts must be understood well by software developers to write a good quality code. Also, technical lead and technical architects must understand the code quality (or coding) concepts very well in order to review the code (termed as code review) and ensure that good quality software is delivered to the customer.
Pictorially, the following represents some of the experiences that software developers, tech leads or technical architects go through while developing software or reviewing software code quality. Enjoy the pictures :).
Often code quality review of poorly written code results in lot of frustrations among project stakeholders. Thus, it is of utmost important to understand different nuances of writing code of good quality.
Who would not want a good code review sessions with very less number of review comments? Good for both code reviewers and the software developers who developed that code.
In this post, you would learn about some of the following:
- A quick introduction to object-oriented coding principles, code smells etc.
- Sample interview questions
- Practice test
Object-oriented Coding Principles vis-a-vis Code Quality
- One can write good quality code by following SOLID principles
- S: Single Responsibility Principle (SRP) – A block of code must have just one reason to change or perform single functionality.
- O: Open-closed Principle (OCP) – A block of code such as class is open for extension but closed for modification.
- L: Liskov substitution principle (LSP) – A reference of derived class can be used (substituted) in place of base class.
- I: Interface segregation principle (ISP) – Large interfaces can be broken down into small interfaces representing cohesive contracts.
- D: Dependency inversion principle (DIP) – A high-level module should not depend on low-level modules. Rather, both should depend on abstractions.
- DRY is another principle which states that code must not be duplicated. DRY stands for Don’t Repeat Yourself. Following DRY principle would enhance code maintainability.
- A code following SRP will have higher cohesiveness and reusability.
- A code having high cohesiveness will have high reusability.
- Code quality can be measured as a function of the cyclomatic complexity of the code; Cyclomatic complexity is a count of linearly independent paths through the program source code. One can determine the cyclomatic complexity of code by counting the number of conditional expressions (such as if-then-else, do-while, switch-else, for etc) in the code.
- Higher the cyclomatic complexity of the source code, lower is the possibility of achieving high unit test coverage of code which could impact the code maintainability.
- The following are some of the code smells which could be found with code violating single responsibility principle:
- Long class
- Long method
- Large number of conditional expressions
- The following are some of the code refactoring techniques which could fix the violation of single responsibility principle:
- Extract class
- Extract method
- Move method
Sample Interview Questions
- What are SOLID principles?
- What is Single Responsibility Principle (SRP)? How is it related to reusability and testability of the code?
- What is Liskov Substitution Principle (LSP)? Explain it using Rectangle and Square inheritance example?
- What code refactoring techniques could be applied to the code which violates SRP?
- What are some of the code smells that can be found with code violating SRP?
- What is the cyclomatic complexity of the code?
- How is cyclomatic complexity related to unit test coverage?
- What is the open-closed principle? Explain with an example.
- Explain interface segregation principle with an example.
- What are some of the code refactoring techniques which could help fix single responsibility principle violations?
Practice Questions to Test Your Code Quality Concepts
A class with ______ cohesiveness will have _____ reusability?
A block of code such as class or method serving multiple different functionality tends to violate which of the following?
Which of the following refactring technique can be helpful in increasing the cohesiveness of the code?
A class having high maintainability will tend to have which of the following characteristics?
Which of the following principle states that a class should be closed for modification and open for extension?
Which of the following tends to decrease code maintainability?
The code smell such as long classes or long methods tend to violate which of the following principle?
Which of the following principle is related with inheritance concept?
Which of the following principle states that the reference of derived class can be used in place of base class?
DRY principle is related to which of the following?
"Don't call us, we will call you" is related to which of the following principle?
Large interfaces can be related with which of the following principle?
Which of the following principle is related with polymorphism concept?
In this post, you learned about some of the following:
- Quick introduction to code quality principles
- Practice tests on code quality concepts
- Sample interview questions
Did you find this article useful? Do you have any questions about this article or understanding code quality principles? Leave a comment and ask your questions and I shall do my best to address your queries.
He has also authored the book, Building Web Apps with Spring 5 and Angular.
Latest posts by Ajitesh Kumar (see all)
- QA – Metamorphic Testing for Machine Learning Models - August 19, 2018
- QA – Why Machine Learning Systems are Non-testable - August 17, 2018
- QA – Testing Features of Machine Learning Models - August 12, 2018