What is a Ledger and Why Are Names Harder?
Tags: webdev, javascript, programming
Published: 2024-06-26 16:20:54

[Portugues Version](https://daniloab.substack.com/p/o-que-e-ledger-e-por-que-nomes-sao)

## What is Ledger Series
1. [What is a Ledger and why you need to learn about it?](https://dev.to/woovi/what-is-ledger-and-why-does-it-need-idempotence-18n9)
2. [What is Ledger and why does it need Idempotence?](https://dev.to/woovi/what-is-ledger-and-why-does-it-need-idempotence-18n9)
3. [What is a Ledger and Why Floating Points Are Not Recommended?](https://dev.to/woovi/what-is-a-ledger-and-why-floating-points-are-not-recommended-1f4l)
4. [What is a Ledger and Why Are Names Harder?](https://dev.to/woovi/what-is-a-ledger-and-why-are-names-harder-25jn)

In software development, choosing appropriate names for variables, functions, and data structures is a crucial skill. Clear and consistent names are essential for code maintainability and for promoting a healthy development culture. Let's explore why names are important and how we can improve the readability and maintenance of our ledger example.

## The Importance of Names
1. **Clarity**: Well-chosen names make the code easier to understand, both for the author and for other developers who will work on the project in the future.
2. **Maintainability**: Readable and clear code facilitates maintenance and updates, reducing the time needed to fix bugs or add new features.
3. **Communication**: Good names act as implicit documentation, helping to communicate the code's intent more effectively.
4. **Consistency**: Consistent names help establish patterns within the codebase, making it more predictable and less prone to errors.

## Example Improvement: Changing transactionId to idempotencyId
In our previous example, we used transactionId to ensure transaction idempotency. While this name is clear, idempotencyId can be a more descriptive and precise name, reflecting its exact purpose.

## Updating the Code
Let's update the code example to use idempotencyId instead of transactionId:

Defining the ledger structure in MongoDB:
```ts
{
  _id: ObjectId("60c72b2f9b1d8e4d2f507d3a"),
  date: ISODate("2023-06-13T12:00:00Z"),
  description: "Deposit",
  amount: 100000, // Value in cents
  balance: 100000, // Balance in cents
  idempotencyId: "abc123" // Idempotency ID
}
```

Function to add a new entry to the ledger and calculate the balance using idempotency:
```ts
const { MongoClient } = require('mongodb');

async function addTransaction(description, amount, idempotencyId) {
  const url = 'mongodb://localhost:27017';
  const client = new MongoClient(url);
  
  try {
    await client.connect();
    const database = client.db('finance');
    const ledger = database.collection('ledger');

    const existingTransaction = await ledger.findOne({ idempotencyId: idempotencyId });

    if (existingTransaction) {
      console.log('Transaction already exists:', existingTransaction);
      return;
    }

    const lastEntry = await ledger.find().sort({ date: -1 }).limit(1).toArray();

    const lastBalance = lastEntry.length > 0 ? lastEntry[0].balance : 0;

    const newBalance = lastBalance + amount;

    const newEntry = {
      date: new Date(),
      description: description,
      amount: amount,
      balance: newBalance,
      idempotencyId: idempotencyId
    };

    await ledger.insertOne(newEntry);

    console.log('Transaction successfully added:', newEntry);
  } finally {
    await client.close();
  }
}

addTransaction('Deposit', 50000, 'unique-idempotency-id-001'); // Value in cents
```

## Conclusion
Choosing appropriate names is an essential part of software development. Clear, descriptive, and consistent names facilitate code readability, maintenance, and communication. In the example above, we changed transactionId to idempotencyId, making the variable's purpose clearer.

In our next post, we'll explore more about the importance of naming best practices and how they can positively impact code quality and development team efficiency.

Stay tuned for more insights on building reliable financial systems and development best practices!

---

Visit us [Woovi](https://woovi.com/)!

---

Follow me on [Twitter](https://x.com/daniloab_)

If you like and want to support my work, become my [Patreon](https://www.patreon.com/daniloab)

Want to boost your career? Start now with my mentorship through the link

https://mentor.daniloassis.dev

See more at https://linktr.ee/daniloab

Photo of <a href="https://unsplash.com/pt-br/@lucaonniboni?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Luca Onniboni</a> in <a href="https://unsplash.com/pt-br/fotografias/maquina-de-escrever-teal-e-preta-4v9Kk01mEbY?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>