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

Asked

Viewed 99 times

1

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

For example, I have a class and it has several attributes, some depend on whether 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)
        ...

Have I never read anything that says attributes, alphabetical order maybe? 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 vazas, truco...

Where I can find things like that?

1 answer

1


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

There is no rule about the order of the fields, in general they should be placed in order that makes sense, that help 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. Alphabetical order is usually terrible and may denote another problem.

Note that there are variables used locally. I don’t particularly like this form of initialization, but it’s my thing. And so dependence needs to be respected.

If you have difficulty with English, don’t use it. Better to do it in Portuguese than to do something bad in English. Nor in Portuguese do I know what you’re talking about. You can search for English words in the dictionary. But it doesn’t help much because it’s not the term used effectively in most cases. The names must be given to express well what they really are. If the domain you are solving is in Portuguese, do it in Portuguese. If the domain is in English and you don’t know what the terms are, it’s a case of asking someone else who does, not just to translate.

Deeply understanding the problem and how to solve it is much more important, and once this solution comes naturally. Not done, nothing will save the code.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.