Skip to content

Instantly share code, notes, and snippets.

@heimeshoff
Created December 7, 2019 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heimeshoff/7019be2ab8e8e20c3dffc848b47252f0 to your computer and use it in GitHub Desktop.
Save heimeshoff/7019be2ab8e8e20c3dffc848b47252f0 to your computer and use it in GitHub Desktop.
Ubiquituous language tradeoff
Which do you prefer and why?
Alternative ideas?
public Tickettransaction Buy_ticket(Attendee attendee)
{
if (tickets_available)
return Issue_Ticket(attendee);
else
return Not_Issued.Because("Box office should not allow to buy tickets, when there are none available.");
}
public Tickettransaction Buy_ticket(Attendee attendee)
{
return
tickets_available
? Issue_Ticket(attendee)
: Not_Issued.Because("Box office should not allow to buy tickets, when there are none available.");
}
@jbrains
Copy link

jbrains commented Dec 7, 2019

Very clearly a binary decision. Both branches are very simple. For these reasons, ternary operator is perfect.

@bwaidelich
Copy link

bwaidelich commented Dec 7, 2019

I prefer early returns for these things

public Tickettransaction Buy_ticket(Attendee attendee) {
    if (no_tickets_available) {
        return Not_Issued.Because("Box office should not allow to buy tickets, when there are none available.");
    }

    return Issue_Ticket(attendee);
}

...but it's probably a matter of taste

@jbrains
Copy link

jbrains commented Dec 8, 2019

@bwaidelich I would be happy with Guard Clause here, too, but I tend towards ternary operator for clearly a simple binary decision. Similarly, I see it has merely a preference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment