How many classes should a programmer put in one fi

2020-02-12 01:18发布

In your object-oriented language, what guidelines do you follow for grouping classes into a single file? Do you always give each class a seperate file? Do you put tightly coupled classes together? Have you ever specified a couple of implementations of an interface in one file? Do you do it based on how many lines of code the implementation might be or how "cluttered" it might look to the user of the class? Or would the user prefer to have everything on one place?

11条回答
贼婆χ
2楼-- · 2020-02-12 01:53

One per a file is our standard. The only exception is that for a class and it's typed collection we put those together.

Over time I've come to relize, that "small class" always tend to grow. And then you'll want to split them up, confusing everyone else on the team (and your self).

查看更多
Melony?
3楼-- · 2020-02-12 01:54

There is no hard and fast rule that must always be followed (unless a particular language enforces it). There are good reasons for having just one class, or having multiple classes in a file. And it does depend on the language.

In C# and Java people tend to stick to one file per class.

I'd say in C++ though I often put multiple classes in one file. Often those classes are small and very related. Eg. One class for each message in some communications protocol. In this case a file for each would mean a lot of files and actually make maintenance and reading of the code more difficult than if they were in one file.

In C++ the implementation of a class is separate from the class definition, so each class { /body/ } is smaller than in other language and that means classes are more conveniently sized for grouping together in one file.

In C++ if you're writing a library (eg the standard template library) , you can put all the classes in one file. Users only need to include the one header file and they get all the classes then, so its easier for them to work with.

There's a balance. The answer is whatever is most easy to comprehend and maintain. By default it makes sense to have one class per file, but there are plenty of cases when it's more practical to work with a related set of classes if they are defined in one file.

查看更多
手持菜刀,她持情操
4楼-- · 2020-02-12 01:57

1 class = 2 files. An .h and a .c, you kids are so lucky :)

查看更多
不美不萌又怎样
5楼-- · 2020-02-12 02:00

One class = one file. Always. Apart from when one class = multiple files in C#, or a class contains inner classes etc of course ;)

查看更多
家丑人穷心不美
6楼-- · 2020-02-12 02:04

Personally, I suggest one class per file unless the secondary classes are private to the primary class in the file. For example, a nested class in C# would remain in the parent classes file, but utility classes that might be useful elsewhere get broken into their own file or even namespace.

The key is to understand your environment and where people will look for things. If there is an established methodology in place, think carefully before you upset it. If your coworkers expect that related, tightly bound classes will be in a single document, having to search for them could be annoying (although with modern IDEs it shouldn't be a problem).

An additional reason for breaking things into more files rather than less is version control. If you make a small change, it should change only a small file where possible. If you make a sweeping change, it is obvious looking at the logs because of all the files (and indirectly, classes) that were affected are noted.

查看更多
forever°为你锁心
7楼-- · 2020-02-12 02:04

One class per file seems to be the standard. This is the way that I usually do it as well.

There have been a few times where I've strayed away from this. Particularly when a smaller class is a member of another class. For example, when designing a data structure, I would likely implement a "node" class within the same file as the "bigstructure" class.

查看更多
登录 后发表回答