0

I'm in the process of trying to better my approach to programming, and part of this approach is to read a recommended (and already hugely influential to me) book 'Clean Code' by Robert C Martin.

In the book, it says how important it is that each function or class should be doing one thing only, or at least as little as it should within reason.

I've been using Codeigniter for a recent project and my code doesn't do this at all, but I'm unsure of the right way to do this in Codeigniter.

For instance, in some of my controllers I have methods that do rather a lot, or more than 'Clean Code' would recommend anyway. But I'm not sure where is best to break up the code to...

  1. Do I use private methods within my controllers to tidy things up, but then run the possibility of having lots of methods in my class when I have a few private methods for each public method/page.

  2. Do I split each public method/page to it's own file and avoid too many methods and private methods as stated above.

  3. Do I make new classes of my own and put them in the libraries folder, and have a library for any times when my methods or classes are doing too much in the controllers.

If anyone has an example of a well written CodeIgniter script that'd help a lot too so i can look at how a more experienced developer goes about problems.

tereško
  • 58,060
  • 25
  • 98
  • 150
john
  • 1,280
  • 1
  • 18
  • 48
  • 1
    If you want to better your approach to programming, you should start by using a real PHP framework like [Symfony2](http://symfony.com/). ;-) – nietonfir Oct 22 '13 at 12:43
  • That's gutting. Is SRP suitable in any PHP framework, or in PHP for that matter? Really want to improve my programming, so I'm willing to learn a new Framework to do so. – john Oct 22 '13 at 12:44
  • Is Zend 2 of any use? A lot of businesses use that so it'd be of benefit for future jobs too for me. – john Oct 22 '13 at 12:45
  • 1
    SRP is an OOP principle. It can be applied to PHP applications. If you really want to learn SOLID practices, you should ditch the frameworks (at least for some time) and try writing some native code, till you have some understanding of said practices. [This](http://stackoverflow.com/a/16356866/727208) list of materials might be of some help. – tereško Oct 22 '13 at 12:54
  • So CI is for ease of use but forces my code to be worse as a result? Excellent answer in the answer you linked to btw, I'll work through them first i think. As a matter of interest, do you use a framework, and which one would you/do you use? – john Oct 22 '13 at 14:14
  • CodeIgniter is a little bit dated and you may be better off with a more modern framework in general. But as far as layout in your CI app, try an keep the controllers slim and simple when possible. You can move the majority of your logic into your Models and Libraries whenever it makes sense to do so... and consider implementing Interfaces and Abstract classes to inherit from. This can help reduce the need to repeat your code in multiple Models. – Petar Zivkovic Oct 23 '13 at 18:36

0 Answers0