2

Hi I'm writing my own high level API of Amazon's PHP SDK which is used to manipulate DynamoDB, a nosql database.

I find myself writing roughly 150 lines of code for a single function that does one operation, including error checking, request building and of course, comments. If I keep going this way the API class can easily exceed a thousand lines, which I think is a bit hard to maintain.

Thus I'm thinking of breaking my class down into several smaller classes, each handling a set of operations. Say like, table operations, item operations, and batch operations. But I'm not an expert with software design patterns, so is this really a good way to modularize? Or is there any design pattern that I should comply and make my code easier to maintain?

Xavier_Ex
  • 8,432
  • 11
  • 39
  • 55
  • 1
    The first thing you should do is to modularize your program by writing classes that handle specific tasks. Design patterns is a more advanced issue that facilitates code reusing and integration. So you should take it carefully according to your software architecture. You can read more about it here: www.oodesign.com – mostar Aug 28 '12 at 18:29

2 Answers2

3

Your question is a bit subjective, since you do not provide the code so that we can see what you're trying to accomplish in fact.

But yes, you need to separate your classes into smaller components, each one with its own responsibility. There isn't a single pattern that tells how exactly to do that, but the Single Responsibility Principle is a good one to start with. It's about separation of concerns.

You said:

[...] including error checking, request building [...]

There you go, separating error checking, from request building, and then separating it again into smaller components, each with just one responsibility. You must have just one reason to change each component, never more then one. You must have a clear domain model defined, with dedicated services for each one of these domain objects.

Take for example, the answer of @teresko here:

How should a model be structured in MVC?

So, bear in mind that you need to separate your application into layers. The request is part of the presentation tier. Which in modern MVC-alike apllications is composed by the Controller Layer and the View Layer, which interacts with the Model layer in order to prepare the presentation for a given request.

See: http://martinfowler.com/eaaCatalog/

Community
  • 1
  • 1
Keyne Viana
  • 6,194
  • 2
  • 24
  • 55
  • Thanks for the answer! Yes I already have a class for checking errors, but there are still 20+ lines of code for each function to invoke the appropriate error checking method, since there are just a lot of parameters (6~8). And for comments... Probably no one is responsible for the comments here LOL. – Xavier_Ex Aug 28 '12 at 20:34
  • That MVC question you provided looks very interesting, and the answer is very helpful, I read a bit but not all yet... Will definitely read it through when I have time. It really helped me see the big picture clearer now. – Xavier_Ex Aug 28 '12 at 20:35
  • I think how I separate my class somehow depends on how I can factor out the similar codes in each function here. – Xavier_Ex Aug 28 '12 at 20:37
  • Uh!? You may be better with exception handlers in order to treat each of the possible type of errors. What do you mean by `no one is responsible for the comments here`? – Keyne Viana Aug 28 '12 at 20:38
  • Oh by comments I meant the comments I inserted in between lines for human reader, not functional statements haha. So there is no need to separate them I think. :D – Xavier_Ex Aug 28 '12 at 20:40
  • Ah! I thought it was a comments entity. Ops! :) Btw, make sure you're using exceptions correctly. – Keyne Viana Aug 28 '12 at 20:43
  • And after reading your answer, I realize that it may not be a good way to simply separate the class based on the database operations, but based on different layers of process that each function does. For example request building. Good that I learn something today :) – Xavier_Ex Aug 28 '12 at 20:44
  • 1
    Just updated the answer with a few more infos... good luck with your researches. – Keyne Viana Aug 28 '12 at 21:05
2

I think I am in the same boat as you. I have lots of classes to maintain that have too many purposes. Have a look at the following web page, which helped me: http://www.dofactory.com/Patterns/Patterns.aspx. Also Google: SOLID (http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29). These are targeted towards .NET and Object Oriented programming, but I know that PHP has classes and objects.

w0051977
  • 15,099
  • 32
  • 152
  • 329