Sobes.tech
Junior — Senior

Identifying flaws in the design of the View class

livecode

Task condition

Identify the weaknesses in the architecture of the View class and suggest ways to fix or optimize them. The class code is responsible for formatting values considering the specified units of measurement and generates an HTML string.

class View:
    def __init__(self, units):
        self.units = units

    def render(self, data):
        label = self.format(data)
        return f"<p>{label}</p>"

    def format(self, n) -> str:
        if self.units == "$":
            return '${:.2f}'.format(n)
        if self.units == "%":
            return '{:.0f}%'.format(n * 100)
        return str(data)

Pay attention to potential violations of SOLID principles, lack of error handling, hardcoded conditions, and use of global state. Suggest how to improve the flexibility, testability, and extensibility of the component.