Multiple classes in one file, Ruby Style question

I am writing a script that takes data from a database and creates GoogleChart URLs from the parsed data. I just need to create two types of charts, Pie and Bar, is it wrong if I stick to both of these classes in one file to keep the number of files that I have low?

thanks

+4
source share
3 answers

If you are asking for a “ruby” way, then you should put your classes in separate files. As some others mentioned, placing your classes in separate files scales better. If you put several classes in the same file and they begin to grow, you will have to separate them later.

So why not separate them from the start?

UPDATE

I should also mention that autoload works, expecting the classes to be in their files. For example, if you are in a Rails environment and do not split classes into different files, you must explicitly specify the require file. A better place for this would be in the application.rb file. I know that you are not in a Rails environment, but for others who may find this answer, this information may be useful.

UPDATE2

By “autoload,” I meant Rails autoload. If you set up your own autoload, you can put the classes in the same file; but then again, why? Typically, the Ruby and Java communities expect classes to be in separate files. The only exceptions are nested classes, but for more advanced design patterns.

+14
source

Generally, less complex files are better than less complex ones. Especially if you need to share the code with others.

+3
source

This is not true. If your code is simple enough *, then by all means put it all in one file.

On the other hand, if you think your code will be more complex later, or if you plan to use the automated testing tools later, you will do a lot of good if you deal with the structure of everything that is now.

(* My personal rule: about 200 lines.)

0
source

All Articles