Backward Compatibility: Practice

Welcome back! Today, we'll master what we learned about backward compatibility in practice. Get prepared to apply all the knowledge on practice tasks, but first let's look at two examples and analyze them.

Task 1: Enhancing a Complex Data Processing Function with Default Parameters

Let's say that initially, we have a complex data processing function designed to operate on a list of dictionaries, applying a transformation that converts all string values within the dictionaries to uppercase. Here's the initial version:

Python
def process_data(items):
    processed_items = []
    for item in items:
        processed_item = {key: value.upper() if type(value) is str else value for key, value in item.items()}
        processed_items.append(processed_item)
    for item in processed_items[:3]:  # Display the first 3 items for brevity
        print(f"Processed Item: {item}")

We intend to expand this function, adding capabilities to filter the items based on a condition and allow for custom transformations. The aim is to retain backward compatibility while introducing these enhancements. Here's the updated approach:

def process_data(items, condition=lambda x: True, transform=None):
    processed_items = []
    for item in items:
        if condition(item):  # Apply condition to filter items
            if transform:
                processed_item = transform(item)  # Apply custom transformation if provided
            else:
                # Default transformation: Convert string values to uppercase
                processed_item = {key: value.upper() if type(value) is str else value for key, value in item.items()}
            processed_items.append(processed_item)
    for item in processed_items[:3]:  # Display the first 3 items for brevity
        print(f"Processed Item: {item}")

# Default behavior - convert string values to uppercase
process_data([{"name": "apple", "quantity": 10}, {"name": "orange", "quantity": 5}])

# Custom filter - select items with a quantity greater than 5
process_data([{"name": "apple", "quantity": 10}, {"name": "orange", "quantity": 5}], condition=lambda x: x["quantity"] > 5)

# Custom transformation - convert names to uppercase and multiply the quantity by 2
custom_transform = lambda item: {key: value.upper() if type(key) == "name" else value * 2 for key, value in item.items()}
process_data([{"name": "apple", "quantity": 10}, {"name": "orange", "quantity": 5}], transform=custom_transform)

In the evolved version, we've introduced two optional parameters: condition, a lambda function to filter the input list based on a given condition, and transform, a lambda function for custom transformations of the filtered items. The default behavior processes all items, converting string values to uppercase, which ensures that the original function's behavior is maintained for existing code paths. This robust enhancement strategy facilitates adding new features to a function with significant complexity while preserving backward compatibility, showcasing an advanced application of evolving software capabilities responsively and responsibly.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal