Sitemap
Chetan Badgujar

Chetan Badgujar

Technology evangelist and coding enthusiast. Exploring the basic coding concepts and sharing insights on the future of programming.

Primitive Obsession

2 min readMar 29, 2023

Primitive Obsession is an anti-pattern in software development that occurs when developers excessively use primitive data types (such as integers, strings, or booleans) instead of creating custom classes or structures to represent complex concepts.

Here’s an example of primitive obsession in C#:

Get Chetan Badgujar’s stories in your inbox

Join Medium for free to get updates from this writer.

Suppose you are developing a program that manages bank accounts. You need to keep track of the account balance, and you decide to represent it as a decimal variable:

decimal accountBalance;

Later on, you need to add functionality to deduct a fee from the account balance. You could simply subtract the fee from the decimal variable:

accountBalance -= 10.0m;

However, this approach is prone to errors and doesn’t encapsulate the concept of a bank account balance. A better approach would be to create a BankAccount class that encapsulates the balance, and provides methods for adding, withdrawing, and deducting fees:

class BankAccount
{
private decimal balance;

public void AddMoney(decimal amount)
{
balance += amount;
}

public void WithdrawMoney(decimal amount)
{
balance -= amount;
}

public void DeductFee(decimal fee)
{
balance -= fee;
}

public decimal GetBalance()
{
return balance;
}
}

Using this approach, you can better control the operations that can be performed on the account balance and reduce the risk of errors. Additionally, you can add more functionality to the BankAccount class, such as tracking transaction history or applying interest rates, without cluttering your main code with low-level details.

Benefits of Primitive Obsession Refactoring

  1. Improved Readability: By using custom classes or structures instead of primitive data types, you can make your code more expressive and easier to understand. It becomes clearer what each value represents and how they relate to each other.
  2. Reduced Risk of Errors: Primitive data types are often used in an ad-hoc manner, which can lead to mistakes in how they are used. By creating custom classes or structures, you can define more clearly the operations that can be performed on the data, reducing the risk of errors.
  3. Increased Flexibility: Custom classes or structures can provide a foundation for adding new features or modifying existing ones. They encapsulate the underlying implementation details, which allows you to change how the data is represented or processed without affecting the rest of the code.
  4. Improved Testability: By using custom classes or structures, you can create more targeted tests that focus on the specific behavior of the data. This makes it easier to test your code thoroughly and catch any issues before they are released into production.
  5. Better Maintainability: Refactoring primitive obsession can make your code more modular and easier to maintain. By breaking down complex data structures into smaller, more manageable pieces, you can make changes more easily and with less risk of introducing bugs.

In summary, primitive obsession can lead to code that is harder to maintain and prone to errors. By encapsulating complex concepts in custom classes and structures, you can improve the readability, maintainability, and reliability of your code.

have a nice day…..

--

--

Chetan Badgujar
Chetan Badgujar

Written by Chetan Badgujar

Technology evangelist and coding enthusiast. Exploring the basic coding concepts and sharing insights on the future of programming.