user2741109
user2741109

Reputation: 121

use multiple files with same code or only one file and distinguish?

The first thing, I have already searched the site for a long time now, but found no solution for me.

I have a homepage where the user can look at some pictures. Each picture will displayed separately and has different informations like tags, a description, a name and so on. Also the picture has comments, which contain replies. For that I have some code that consisting of several segments. Everything works with ajax.

If the user want to get more comments or would like to edit the informations of the picture or would like to see more pictures, I always have to use a view segments of the code (also ajax).

The question I have is, whether it's better to use multiple php and javascript files for and require them, like:

or only that one file and differentiate what code i need? In the second solution, javascript would send a variable, so that I can distinguish.

My worry is that when I use multiple files, I also have several times the same code. And isn't this bad for the performance?

Thanks for helping!

Upvotes: 0

Views: 164

Answers (2)

Patrick
Patrick

Reputation: 922

Try to keep everything DRY (don't repeat yourself), but also split things into files so that each one has their own responsibility. If some code is shared by multiple things, then put that into a different file and include it before the other code is executed.

Like you would with a class. You have your class Job for example and then your classes Doctor, Programmer etc extend that class. That way you can share code across different classes without having duplicate code.

Try to apply the same idea to your files.

Upvotes: 4

Kaarel
Kaarel

Reputation: 170

I would suggest that every time you have code that needs to be used more than once, don't rewrite it. Instead write it as a function or method of a class and use parameters to make it do "slightly different things"

Upvotes: 0

Related Questions