How to name variables and organize them in code in what goes beyond PEP 8?

1

Where can I find tips for naming variables, organizing code, etc.?

For example, I have a class and it has several attributes, some depend on that another attribute has been declared before, but others are independent, how could I order which one to put first and then? Example:

class Button(object):
    defs __init__(self):
        self.image = pygame.image.load()
        self.rect = self.image.get_rect()
        self.draw = True
        self.moved = False
        self.text = font.render(text, True, color)
        ...

I've never read anything that says how to sort the attributes alphabetically, perhaps? And another thing I tried to use all the names in English but I ended up bumping into some that I could not translate for example tricks, trick ...

Where can I find things like this?

    
asked by anonymous 12.06.2018 / 01:02

1 answer

1

You could start calling fields instead of attributes , after which the language officially calls another attribute thing .

There is no rule about the order of the fields, they should generally be placed in order that makes sense, which helps to read the code more easily, so those that are dependencies of others should come before. It should have a certain grouping, and consider the importance of them. Only with the requirement can you know how to do it. Alphabetical ordering is usually terrible and may denote another problem.

Note that there are variables used locally. I particularly do not like this form of initialization, but it's my thing. And so dependency needs to be respected.

If you have difficulty with English, do not use it. Better to do in Portuguese than to do something bad in English. Even in Portuguese I do not know what you're talking about. You can search English words in the dictionary. But it does not help much because it is not the term used effectively in most cases. Names should be given to express well what they really are. If the domain that you are solving is in Portuguese, do it in Portuguese. If the domain is in English and does not know the terms it is the case to ask for help for another person who knows, not only to translate.

Understanding the problem deeply and how to solve it is much more important, and so the solution comes naturally. Not done, nothing will save the code.

Note that the question has nothing to do with Python, nomenclature or conventions per se. It has something coding style and something that is not programming.

    
12.06.2018 / 01:42