How to represent two different types of data being returned by a class method in a UML diagram?

Viewed 71

I have the following method:

    def getInvoiceById(self, id):

        for invoice in self.invoices:
            if invoice.id == id:
                return invoice

        return False

inside a class called User. How do I represent the return type (after the Parenthese) in the UML class diagram? since it can either be a Boolean or another class invoice.

1 Answers

Besides the fact that this is a really bad implementation (you rather should return None than False) you would have the return type of what ever ìnvoice would be (probably Invoice). So assuming that it would just be

+getInvoiceById(id):Invoice

(I intentionally omit self since it is a mandatory parameter in Python.)

The exception can be noted as a constraint like { returns False if no id found }.

Related