Easy right? Well, first, if a class or an interface, or trait got a name, why would we need another name when actually using them?
Then, what is the purpose of namespaces again? Secondly, there are the special case of mock classes, that are meant to replace entirely one class with mocked values or methods. This may be one exception. Anyhow, it happens often that several classes ends up with the same name in a project. Those would need aliasing, and probably some review anyway.
In PHP 7, use the grouped expression, to make the prefix obvious. When a file start with a too long list of use, it shows that the following code is actually coupled to many other classes. Drupal , octobercms and shopware have up to 30 use in one file, thelia has up to 69 see here. Truthfully, average is between 5 to 8 per file, with higher numbers for Test files. Use expression only impact the current namespace, or file. This makes it quite easy for static analysis tools to review it file by file, and provides feedback on uses based on all the rules above : missing alias, dead alias or aliases that may be reused.
Rules of thumb are created for a first cut analysis, or to constrain the choices of those who are not capable of making good choices.
I think most programmers would like to believe they are capable of making good decisions. The Smalltalk answer is: you should not have files for programming. They make versioning and navigation painful. One class per file is simpler to maintain and much more clear for anyone else looking at your code.
It is also mandatory, or very restricted in some languages. In Java for instance, you cannot create multiple top level classes per file, they have to be in separate files where the classname and filename are the same.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Is it a bad practice to have multiple classes in the same file? Ask Question. Asked 13 years, 1 month ago. Active 3 years, 11 months ago. Viewed 48k times. Improve this question. ThunderBird 6 6 silver badges 13 13 bronze badges. Pokus Pokus Add a comment.
Active Oldest Votes. I think you should try to keep your code to 1 class per file. Improve this answer. Zac B 3, 3 3 gold badges 30 30 silver badges 51 51 bronze badges. Patrick Desjardins Patrick Desjardins k 82 82 gold badges silver badges bronze badges. After seeing what kotlin can do in one file I start to believe that some things like DTOs could be in one file. To add to this, the classes you'd have in the same file are typically closely related, which can make editing difficult because it's not as easy to view multiple classes simultaneously.
You have to scroll around a lot. What about autoloading? When defining multiple classes in one source file the autoloading function will become more complex. This gives Python an extra level of grouping between class and package. There is no one right answer that is language-agnostic. It varies with the language. Lott S. Lott k 75 75 gold badges silver badges bronze badges. Ryan Lundy Ryan Lundy k 36 36 gold badges silver badges bronze badges.
For instance, what is the cohesiveness of the classes in question? Are they tightly coupled? Are they completely orthogonal? Are they related in functionality? It would not be out of line for a web framework to supply a general purpose widgets. When the classes are orthogonal, and have nothing to do with each other, the grouping into a single file would indeed be artificial.
Assume an application to manage a robotic factory that builds cars. A file called parts containing CarParts and RobotParts would be senseless Such a joining would add no information or knowledge about the system you are designing. Perhaps the best rule of thumb is don't constrain your choices by a rule of thumb. Rules of thumb are created for a first cut analysis, or to constrain the choices of those who are not capable of making good choices.
I think most programmers would like to believe they are capable of making good decisions. The Smalltalk answer is: you should not have files for programming. They make versioning and navigation painful. One class per file is simpler to maintain and much more clear for anyone else looking at your code. It is also mandatory, or very restricted in some languages. In Java for instance, you cannot create multiple top level classes per file, they have to be in separate files where the classname and filename are the same.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.
Learn more. Is it a bad practice to have multiple classes in the same file? Ask Question. Asked 13 years, 1 month ago. Active 3 years, 11 months ago. Viewed 48k times. Improve this question. ThunderBird 6 6 silver badges 13 13 bronze badges. Pokus Pokus Add a comment. Active Oldest Votes. I think you should try to keep your code to 1 class per file.
Improve this answer. Zac B 3, 3 3 gold badges 30 30 silver badges 51 51 bronze badges. Patrick Desjardins Patrick Desjardins k 82 82 gold badges silver badges bronze badges. After seeing what kotlin can do in one file I start to believe that some things like DTOs could be in one file.
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:. Report Error. Your message has been sent to W3Schools. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
0コメント