DEV Community

mohamed Tayel
mohamed Tayel

Posted on

1

Batching Iteration: A User-Friendly Way to Display Data

When working with large datasets, displaying everything at once can overwhelm users and create a poor user experience. Instead, batching the display allows users to view a manageable portion of the data at a time, giving them the option to load more when ready.

In this article, we’ll explore the concept of batching in iteration and implement it with an interactive example using a Product Inventory system.


Why Batching?

Batching improves usability by:

  • Avoiding information overload.
  • Providing control over how much data is displayed at once.
  • Optimizing performance, especially with large datasets.

A for loop is particularly suited for this task because it gives us precise control over the iteration process, unlike a foreach loop, which processes the entire collection in one go.


Scenario: Product Inventory

Imagine we have a list of products in an inventory system, and we want to display a few products at a time. After showing a batch, the user decides whether to see more or stop.


Implementation

Here’s how we can implement batching with a Product Inventory example in C#.

Step 1: Define the Product Class

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }

    public override string ToString()
    {
        return $"{Id}. {Name} - ${Price:F2}";
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a List of Products

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Sample product list
        var products = new List<Product>
        {
            new Product { Id = 1, Name = "Laptop", Price = 999.99M },
            new Product { Id = 2, Name = "Smartphone", Price = 799.99M },
            new Product { Id = 3, Name = "Tablet", Price = 499.99M },
            new Product { Id = 4, Name = "Headphones", Price = 199.99M },
            new Product { Id = 5, Name = "Smartwatch", Price = 249.99M },
            new Product { Id = 6, Name = "Camera", Price = 599.99M },
            new Product { Id = 7, Name = "Printer", Price = 149.99M },
            new Product { Id = 8, Name = "Monitor", Price = 299.99M },
            new Product { Id = 9, Name = "Keyboard", Price = 49.99M },
            new Product { Id = 10, Name = "Mouse", Price = 29.99M }
        };
Enter fullscreen mode Exit fullscreen mode

Step 3: Implement Batching Logic

        // Ask user for batch size
        Console.Write("How many products would you like to display at a time? ");
        int batchSize = int.Parse(Console.ReadLine() ?? "3");

        // Display products in batches
        for (int i = 0; i < products.Count; i++)
        {
            Console.WriteLine(products[i]);

            // Check if it's the end of a batch
            if ((i + 1) % batchSize == 0 && i < products.Count - 1)
            {
                Console.Write("Press Enter to see more, or type 'stop' to quit: ");
                string input = Console.ReadLine();
                if (!string.IsNullOrEmpty(input)) break; // Exit if user types anything
            }
        }

        Console.WriteLine("End of product list.");
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation of the Code

  1. Batch Size Input: The user specifies how many items they want to see at a time (batchSize).
  2. Iteration Control: A for loop iterates through the products.
  3. Batch Check: (i + 1) % batchSize == 0 ensures the user is prompted after displaying batchSize products.
  4. User Interaction:
    • If the user presses Enter, the loop continues to the next batch.
    • If the user types anything else, the loop breaks, and the program exits.

Sample Output

Input: Batch size = 3

1. Laptop - $999.99
2. Smartphone - $799.99
3. Tablet - $499.99
Press Enter to see more, or type 'stop' to quit: 
Enter fullscreen mode Exit fullscreen mode

(User presses Enter)

4. Headphones - $199.99
5. Smartwatch - $249.99
6. Camera - $599.99
Press Enter to see more, or type 'stop' to quit: stop
End of product list.
Enter fullscreen mode Exit fullscreen mode

Benefits of Batching

  1. Improved User Experience: Users interact with the data at their own pace.
  2. Scalability: Handles large datasets effectively.
  3. Flexibility: Easily customizable for different use cases, like search results or paginated APIs.

Assignments

  1. Easy: Modify the code to display product names only (omit prices).
  2. Medium: Allow the user to go back to the previous batch.
  3. Difficult: Implement a search feature that filters products by name before displaying them in batches.

Conclusion

Batching in iteration is a simple yet powerful technique to manage large datasets effectively. By using a for loop and adding user interaction, you can provide a much friendlier and more responsive experience.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more