Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a recommended number of lines of code per file? [closed]

I have a class file that contains all the classes that are needed for a certain web application. Currently I'm at line 7269 and it contains numerous classes. I'm not particularly worried but I've started to notice that when working on this file Visual Studio responds slower. I'm thinking that this may be caused by the size of the file and Visual Studio's re-compile after every enter key press.

Has anyone got any recommendations for limits to lines per file of classes per file or perhaps guidelines as to how/why I should move classes into separate files?

I found this regarding Java but I'm specifically concerned with VB and Visual Studio 2005

Maximum lines of code permitted in a Java class?

Thanks

EDIT There are about 50+ classes in the file, some tiny, some large!

like image 555
David A Gibson Avatar asked Dec 17 '08 11:12

David A Gibson


3 Answers

Egad.

It's highly recommended by just about everyone that classes are contained in a file per class.

Lines isn't very relevant directly, but it can be a symptom that you're creating an overly complex god class and need to break the logic up. Unit test driven development forces this when done correctly.

edit (noticed the "why?"):

from the fact you're asking it should be obvious :)

A few (of many reasons):

  • it's very hard to visualise the actual code layout in large files

  • most IDEs are default set-up to show multiple files at a time, but not multiple views of the same file

  • source control over a single file can be a problem: "can you please check in {godfile} so I can work on the project?"

  • fragmenting code into namespaces/packages/branches is a problem

like image 178
annakata Avatar answered Nov 06 '22 13:11

annakata


One top-level type per file is conventional - and while nested types are okay, don't go overboard. There are exceptions - if I need to declare a number of delegates, I'll often put them all in one file (e.g. Delegates.cs for C#) - that just makes sense as each delegate type just consists of a method signature, basically. Some people do the same for enums, though I prefer not to.

This makes your code easier to navigate, quicker to compile, and shows changes in source control more easily too.

Beyond that - I start to worry if a single class is over 1000 lines of code without a really good reason. There can be good reasons, but you should at least look at whether the class has too many responsibilities. I've seen classes which cover enough functionality to deserve their own namespace before now (with a suitable number of smaller classes providing the required functionality)...

It's not clear from the question whether or not any of your individual classes is that huge, but separating the classes into "one per file" should at least help to start with.

like image 45
Jon Skeet Avatar answered Nov 06 '22 15:11

Jon Skeet


One class per file. I try not to go over 200 lines per file (excluding comments), but it does happen.

Refactor, refactor, refactor!

like image 23
hmcclungiii Avatar answered Nov 06 '22 14:11

hmcclungiii