Search code examples
pythonmodel-view-controllerflaskconventionspep8

What is best naming conventions for long class name?


Now, I'm writing Python-flask codes to MVC Pattern. But, I consider myself, I think declarator of controller class is too long

For example, I have

class GoogleQuestionController:
    def __init__(self):

        (.....)

and

class YahooQuestionController:
    def __init__(self):

        (.....)

two of very similar class name, so I can't define declarator like

question_controller = GoogleQuestionController()

above.

What is best suggestion to define class declarator?

google_question_controller = GoogleQuestionController()
yahoo_question_controller = YahooQuestionController()

This code is too long,

So, I'm using like this now.

yq_con = YahooQuestionController()
gq_con = GoogleQuestionController()

or

gqc = GoogleQuestionController()
yqc = YahooQuestionController()

Solution

  • Context is important here:

    google_question_controller = GoogleQuestionController()
    yahoo_question_controller = YahooQuestionController()
    

    might be ok, but if we were able to refactor so that the noun wasn't ambiguous i.e. if there were only one google object I might use:

    google = GoogleQuestionController()
    yahoo = YahooQuestionController()
    

    Similarly, if the verb wasn't ambiguous, e.g. if I was inside a Google object/module, I'd use question (or perhaps controller):

    class GoogleSomething(object):
        def __init__(self):
            self.question = GoogleQuestionController()  # or perhaps `controller`
    
    class YahooSomething(object):
        def __init__(self):
            self.question = YahooQuestionController()
    

    In this way there might be a way in which you could rewrite your code to improve readability.

    Often, in cases like this, grouping together similar behaviors can lead to better abstractions, for example we might be able to define a mixin/base class which makes use of self.controller using a shared API between GoogleQuestionController and YahooQuestionController etc.