Maximum number of lines recommended

Asked

Viewed 271 times

3

Is there any PEP or a good practice suggestion setting out the maximum recommended number of lines a module should have? In my case for example, I keep more than one class per module, obviously classes that are within the same context, classes Domains for example. However, I have a module that is already over 450 lines, and I don’t know if it would be time to separate. In other languages such as Java and C# it usually remains a single class per file, but in Python it is common to have more than one, where files are called modules.

  • 4

    Prioritize semantics. Style is one of the last things. In other words: if it makes sense to keep everything in the same file, even passing 1000 lines, leave so. Separating adds complexity and this is only good when it solves some problem.

  • Like @Andersoncarloswoss, I also believe that the most important thing is the context in which each function is inserted, if it makes sense that they are together even creating a large file (class/module) would be correct. But analyzing whether it is possible to break into subclasses or other modules is important to maintain the reusability and readability of your code. PEP-8 talks about line limit size but never seen anything about line limit in module or class.

1 answer

2


The PEP8 is the most followed and well accepted best practice guide within python, in which you find several 'conventions' for writing your code.

Splitting your module into other smaller modules may also vary according to the purpose which you’re programming to. If it is a desktop application it may be more advantageous to have more robust blocks, otherwise web applications tend to have the most well-divided code.
I quote here a passage from the book Thoo Scoops of Django, that brings together conventions and useful tips for the organization of your project:

When in doubt, keep apps small.

Although the book is aimed at web development, personally, I always try to keep this perspective in mind, but it is worth taking into account the semantic factor of the code, as previously said by Anderson.
If it makes sense that all that code block of yours is together in one place, and you judge that breaking it into smaller fragments would just make it harder to read it, there’s no reason why.

The PEP8 is there to assist you, but your doubt, as you can see, seems to be somewhat subjective. I’ll leave you some reading recommendations that I hope will help you with. If you are a solo programmer try to read the PEP and find what suits you best, if you program with a team, try to talk to the other programmers and find a way that is intelligible to everyone.

Complementary Reading

Browser other questions tagged

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