Sitemap
Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.

Clean Code: Objects and Data Structures

In the world of software development, writing clean and maintainable code is a paramount goal. The “Clean Code: Objects and Data Structures” chapter provides a set of foundational principles that can guide developers toward crafting code that is not only functional but also comprehensible and adaptable.

3 min readAug 29, 2023

--

Press enter or click to view image in full size
To understand the basics of programming, it is important to know the difference between a class and an object.

What is a class?

A class is a blueprint or a template for creating objects. It defines the properties (attributes/fields) and behaviours (methods/functions) that objects of that class will have. It encapsulates the data and operations related to a specific concept.

What is an object?

An object is an instance of a class. It represents a specific occurrence of the concept defined by the class. Objects hold data (attributes) and can perform actions (methods) defined in the class.

1. Hide Internal Structure:

This principle encourages encapsulation. The internal details of a class or object should be hidden from the outside world. Only the necessary and well-defined interfaces should be exposed, reducing dependencies and making it easier to modify the internals without affecting external code.

2. Prefer Data Structures:

This principle advocates for creating classes that primarily hold data, with minimal or no behaviors. These are often called “data structures.” By separating data from behaviour, it becomes easier to reason about the code and make changes without inadvertently affecting behaviour.

3. Avoid Hybrid Structures:

This principle discourages mixing data structures and behaviours in the same class. Such hybrids can lead to confusion, making it challenging to maintain and understand the codebase. It’s better to have classes focused on either data or behaviour, not both.

4. Class Size:

Classes should be small and focused. A class should have a single responsibility, encapsulating only one concept or behaviour. Smaller classes are easier to understand, test, and maintain.

5. Single Responsibility Principle (SRP):

This principle emphasizes that a class should have only one reason to change. In other words, a class should have a single responsibility or function. This leads to more modular and maintainable code.

6. Limited Instance Variables:

A class should have a limited number of instance variables (attributes) to avoid unnecessary complexity. Too many variables can make a class harder to understand and manage.

7. Base Class and Derivatives:

Base classes (parent classes) should be independent of their derived classes (child classes). This allows for better decoupling and ensures that changes to derived classes do not affect the base class.

8. Favour Functions Over Behavior Selection:

It’s better to have separate functions for different behaviours rather than passing a behaviour selector as an argument. This makes the code clearer and easier to follow.

9. Non-Static vs. Static Methods:

Non-static methods (instance methods) operate on specific instances of a class and can access instance variables. Static methods are shared across all instances and cannot access instance-specific data. Favour non-static methods when working with instance-specific behaviour and data.

Get Pabashani Herath’s stories in your inbox

Join Medium for free to get updates from this writer.

These principles collectively aim to create clean, maintainable, and understandable code by promoting modularization, separation of concerns, and adherence to best practices in object-oriented design.

The “Clean Code: Objects and Data Structures” chapter lays the foundation for crafting maintainable code by emphasizing principles such as encapsulation, data-structure preference, and focused class design. These guidelines promote modularization, separation of concerns, and clarity, resulting in code that is comprehensible, adaptable, and aligned with best practices in object-oriented design.

Thank you for reading until the end. Please consider following the writer and this publication. Visit Stackademic to find out more about how we are democratizing free programming education around the world.

--

--

Written by Pabashani Herath

Senior Software Engineer at Rootcode Labs ❤️ Sri Lankan🇱🇰 LinkedIn: https://www.linkedin.com/in/pabashani-herath-a1204b86/

Published in Stackademic

Stackademic is a learning hub for programmers, devs, coders, and engineers. Our goal is to democratize free coding education for the world.