What is Ledger Series
- What is a Ledger and why you need to learn about it?
- What is Ledger and why does it need Idempotence?
- What is a Ledger and Why Floating Points Are Not Recommended?
- What is a Ledger and Why Are Names Harder?
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
- 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.
- Maintainability: Readable and clear code facilitates maintenance and updates, reducing the time needed to fix bugs or add new features.
- Communication: Good names act as implicit documentation, helping to communicate the code's intent more effectively.
- 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:
{
_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:
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!
Follow me on Twitter
If you like and want to support my work, become my Patreon
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 Luca Onniboni in Unsplash
Top comments (0)