3

I'm working on a Soccer management game. Project is fairly new and small and I'm new to PHP. The project was starting to get a bit messy so I've been looking into refactoring. The first thing I've done is to make my functions smaller, which helps a lot. I also added my functions to my classes, since they were originally a bunch of small functions, included in the project. The problem is, now my classes are starting to get large and I don't want to go in the wrong direction with my coding/learning.

I'm thinking of removing some of the methods into their own class but I'm not sure of how this works. For example, my Team class currently does two things (which is one too many?). Firstly it does database work (load/save/create a team) and I'm now adding functions to allow the AI to select a squad. This is already six small methods. Could I make a second class, just for this second group of methods, with no properties? If so, how would this work?

For example, say that I already have my 'Team' object. Do I create a 'Team Select' object, just to use those methods, then pass my 'Team' object into the methods, so something like teamselectobj->SelectTeam($Teamobject). Although this makes my classes smaller, it does seem like extra unnecessary steps. Is this good refactoring practice?

Farflame
  • 131

1 Answers1

2

Sounds like you are not very good just yet with the responsibility assignment in programming. Have a look at General Responsibility Assignment Software Patterns. They will tell you exactly where to place each method and property.

You want to have a class that defines an object with it's own properties and methods to act upon it. You don't want to just create a class because it would be 6 methods there because they don't belong anywhere else.

Alexus
  • 2,398