3

I encountered a scenario where I cannot decide on which is the best (or worst) naming strategy. The context is the following: a bracket (as in a tournament) made up of nodes, where is node is made up of two abstract elements which can be a seeded player, a bye, or a reference to another node.

We clearly see a hierarchy of inclusion, conceptually speaking:

Bracket > Node > Element = Seed, Bye, Reference

I cannot decide whether name them with composed names and declare each class at the same level (no nesting):

class Bracket:
    pass

class BracketNode:
    pass

class BracketNodeElement:
    pass

class BracketNodeElementSeed(BracketNodeElement):
    pass

class BracketNodeElementBye(BracketNodeElement):
    pass

class BracketNodeElementReference(BracketNodeElement):
    pass

Or wheter be a bit more DRY with the names but making the name arguably uglier:

class Bracket:
    class Node:
        class Element:
           pass

        class Seed(Element):
           pass

        class Bye(Element):
           pass

        class Reference(Element):
           pass

Accessing a seed or a bye must be expressed this way: Bracket.Node.Seed and Bracket.Node.Bye.

What is the usual way to go?

6

Option C: simpler is better. Make each class at the global level, and remove the prefix:

class Bracket:
    pass

class Node:
    pass

class Element:
    pass

class Seed(Element):
    pass

class Bye(Element):
    pass

class Reference(Element):
    pass

The fact that these all exist in a module gives you the namespacing you need without having to have large, cumbersome class names. For example, assuming you've put these in a file named tournament.py, then from anywhere else you would use tournament.Bracket, tournament.Node, etc.

  • Should BracketNodeElement say 'Element'? – Brandin Jun 30 '17 at 13:00
  • @Brandin: yes. Thanks for pointing that out. I've fixed the answer. – Bryan Oakley Jun 30 '17 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.