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?

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.

  • Thanks. Yes, I'm new to this, so I want to learn the best, or at least, a sensible approach, before my project gets too large. Right now, these methods really do belong with the class and the class properties. The only problem is that the class is getting larger and there's far more to go. Eventually, it will become a very large class, which I thought it was best to avoid? – Farflame Jun 25 '15 at 2:09
  • @Farflame usually if you feel the class grows and you are unhappy about it, you are due to design review :) The better you get at it, the more you will be able to asses when large class is a sign of bad design(99% of the time really) and when it's acceptable. – Alexus Jun 25 '15 at 4:34
  • Thanks Alexus. Right now, I'm happy with the refactoring I've done (making the methods smaller) and the project is still small and manageable. I've spent about 5 days just reading and not really moving forward and I'm still not sure what's best for me and my project. It might be better if I just get on with the project and get a feel for things as I go along. For now, I'm going to keep the methods in the classes and let the classes grow... the only 'problem' I see at the moment is that the class is getting larger, but it's not bothering me. – Farflame Jun 25 '15 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.