Real-World Scenario: Using Positional Patterns in a Food Delivery App

A Guide to Cleaner, Smarter Code

DotNet Full Stack Dev
4 min readNov 4, 2024

In the world of C# development, writing clear, efficient, and expressive code is a hallmark of quality. With each update, C# brings tools that simplify our code and make it more intuitive. One such gem is positional patterns — a pattern-matching feature that makes it easy to work with complex structures in a clean, readable way.

In this blog, we’ll explore what positional patterns are, how to use them with examples, and why they’re a fantastic addition to your C# toolbox.

📌Explore more at: DotNet Full Stack Dev
🌟 Clapping would be appreciated! 🚀

What are Positional Patterns?

At a high level, positional patterns allow us to match data based on its position within a tuple or a record type. Think of positional patterns as an elegant way to destructure data into its core values and pattern-match based on the shape of that data. If you’re working with immutable types — types where each value is in a fixed position — positional patterns are perfect for making your code more readable and expressive.

Why Use Positional Patterns?

  1. Concise Code: They reduce the boilerplate and make code compact.
  2. Expressiveness: Code reads like a series of conditions that naturally describe the data.
  3. Perfect for Records and Tuples: Positional patterns shine with records (immutable types introduced in C# 9) and tuples.

Now that you know what they are, let’s dive into some examples!

Imagine you’re building a food delivery app that delivers orders to different parts of the city. Each delivery location is represented by a coordinate on a map. You want to categorize each location based on its quadrant or special points like the city center.

By using positional patterns, you can write code that’s both concise and expressive, allowing you to determine the location of a delivery based on coordinates. This could help with routing, pricing, or simply understanding the delivery zones.

Step 1: Define the Location Coordinates

First, we represent each location as a tuple of coordinates (X, Y), where:

  • X represents the horizontal position (left or right of the city center).
  • Y represents the vertical position (above or below the city center).

For simplicity, we’ll assume:

  • The city center is at (0, 0).
  • Positive X and Y values represent the north-east quadrant.
  • Negative X and Y values represent the south-west quadrant, etc.

Step 2: Write the Code with Positional Patterns

With positional patterns, we can categorize locations based on their coordinates:

public static string GetDeliveryZone((int X, int Y) location) =>
location switch
{
(0, 0) => "City Center - High Priority Zone",
( > 0, > 0) => "North-East Zone",
( < 0, > 0) => "North-West Zone",
( < 0, < 0) => "South-West Zone",
( > 0, < 0) => "South-East Zone",
(0, _) => "Central Y-Axis - Main Route",
(_, 0) => "Central X-Axis - Main Route",
_ => "Unknown Zone"
};

// Usage Example
var location = (5, -3);
var result = GetDeliveryZone(location); // "South-East Zone"

Explanation

Here’s how the code works:

  1. Pattern Matching by Position: Each case in the switch statement matches a specific combination of X and Y values, making it easy to categorize locations by zone.
  2. City Center: If both X and Y are 0, it’s the city center, which might be a high-priority area for deliveries.
  3. Quadrants: Positive and negative values in each position indicate a specific quadrant:
  • ( > 0, > 0) is North-East.
  • ( < 0, > 0) is North-West, and so on.
  1. Axes: If X or Y is 0, the location lies on the main axis, which could be part of a central route for delivery trucks.
  2. Wildcard Match: _ is used to ignore a coordinate when it isn’t relevant (e.g., (_, 0) for the central X-axis).

Step 3: Using the Code in the Real World

Let’s look at some usage examples to see how easy it is to categorize delivery zones:

var zone1 = GetDeliveryZone((0, 0));      // "City Center - High Priority Zone"
var zone2 = GetDeliveryZone((5, 5)); // "North-East Zone"
var zone3 = GetDeliveryZone((-4, 2)); // "North-West Zone"
var zone4 = GetDeliveryZone((0, 8)); // "Central Y-Axis - Main Route"
var zone5 = GetDeliveryZone((-3, -3)); // "South-West Zone"

Each of these examples shows how we can quickly and clearly determine the delivery zone based on location coordinates.

Why Use Positional Patterns Here?

  • Readability: The code is easy to read and understand. Each pattern directly describes the position in a natural way.
  • Efficiency: Instead of writing multiple if-else statements, we use a concise switch expression.
  • Real-World Application: This pattern makes it easy to determine delivery zones, which could influence routing, delivery time estimates, or priority handling in a real application.

Final Thoughts

Using positional patterns in C# not only simplifies code but also makes it more expressive, which is especially useful for cases like geographic categorization, pricing tiers, and zoning logic. By leveraging positional patterns, you can write cleaner, more maintainable code that’s ready for real-world scenarios. Try it out in your projects, and see how much it can simplify your logic!

--

--

DotNet Full Stack Dev
DotNet Full Stack Dev

Written by DotNet Full Stack Dev

Join me to master .NET Full Stack Development & boost your skills by 1% daily with insights, examples, and techniques! https://dotnet-fullstack-dev.blogspot.com

No responses yet