The concept of ‘clean code’ primarily addresses the issue of maintainability and extensibility of code by either the people who originally wrote the code or some other developers who may have joined the team later – sometimes years or decades later. Keeping this in mind, ‘good code’ is defined as code that can be easily read, understood, maintained, and extended by other human beings. This presentation outlines the concept and gives 5 tips for young developers to write clean code.
Extentia® is a global technology and services firm that helps clients transform and realize their digital strategies. With a unique Experience Centric Transformation approach, our ground-breaking solutions are in the space of mobile, cloud, and design. Our team is differentiated by an emphasis on excellent design skills that they bring to every project. Focused on enterprise mobility, cloud computing, and user experiences, we strive to accomplish and surpass their customers’ business goals. Our inclusive work environment and culture inspire team members to be innovative and creative and to provide clients with an exceptional partnership experience.
Expand your digital horizons with us. We will help you transform your business and surpass your goals.
Write to us at inquiries@extentia.com
2. www.extentia.com | Confidential
What is a Clean Code?
• A code that can be easily read,
understood, maintained, and extended
• Indicates the maintainability and
extensibility of the code for years (if not
decades!)
• Ideally, sustainable from the people who
originally wrote it to the developers who
joined in later
3. www.extentia.com | Confidential
The Need for Clean Codes
Addressing issues with the application of SOLID principles or some of the
other design patterns:
• Difficult to apply in practical situations while under pressure
• Subject to multiple interpretations and can be confusing
• Understanding them requires a certain level of experience
• Classroom teaching, alone, may not be sufficient
5. www.extentia.com | Confidential
Think Before You Type!
• Every line of code is a potential cause for a
bug
• Hence, you must think very deeply before:
• Naming variables
• Organizing code into blocks, functions, etc.
• Think from the perspective of another
developer
• So, multiple developers can maintain the code
for years
6. www.extentia.com | Confidential
Avoid ‘Ladder-to-Hell’ Patterns
• ‘Ladder-to-Hell’ pattern
• One of the most used programming patterns;
resembles the code given on the right
• Occurs when lots of nested if-else, for and while
loops are used
• Makes the code untestable and un-maintainable
• Can be avoided by
• Decomposing the blocks to functions till the
leaf(ves) has just ONE ‘if-else’ block
• Using Sonar or other static analysis tools to detect
and eliminate them
7. www.extentia.com | Confidential
Write Computational Logic as ‘Pure’ Functions
• Do NOT mix I/O operations with
computational logic
• I/O operations make computational logic
difficult to test using automation
• Testing such a function requires mocking
the DB and building other scaffolding
• Code on the right is an example of how
it should be written
• To make the processResultSet() function
testable using automated unit tests
8. www.extentia.com | Confidential
Keep the Length of Functions to Less Than 35
• Programming is about decomposing
problems into smaller problems and then
composing them back into a whole
• Smaller functions are easier to understand
• Long functions require scrolling up and down
continuously, causing mental and physical
strain
• A function should be visible on a single
screen in the smallest device
9. www.extentia.com | Confidential
Keep the Conditionals at Less Than 7
• Restrict the cyclomatic complexity (if-
statements, termination of loops, etc.)
to less than 7, because
• Human short-term memory holds up to 7
pieces of information at once
• As the number increases, our ability to
comprehend reduces
• Lesser cyclomatic complexity leads to a
lesser number of unit tests
11. www.extentia.com | Confidential
Thank You!
Write to us at inquiries.extentia.com to learn more!
More about Extentia’s offerings at https://www.extentia.com