How server-logic-heavy should a view be? How server-logic-heavy should a view be? codeigniter codeigniter

How server-logic-heavy should a view be?


Firstly, it's not a question of how readable the template code is - there's a strong correlation between complexity and the presence of bugs. Cyclomatic complexity is one way to measure complexity; from what you've posted, this template has a cyclomatic complexity of at least 60. In all our projects, I aim for a cyclomatic complexity of < 8, and get panicky when we get to 12.

I wouldn't be surprised if your view code contains a bug - not just a time-out thing, but some bizarre route through the logic tree that you can't quite picture by looking at the code.

The Williams template library gives you several tools to do this - for instance, you can define alternate views to deal with the "if question, render like this, if post render like that" logic. You can also use regions to your benefit here.

There's an assumption that because views deal with the user interface, they are less important - but that's simply not true.

So, let's assume you refactor your view into a simpler structure. Will this affect processor load? Well - uh - depends.

Usually, "maintainability" and "performance" have to be traded off against eachother. The classic example is assembler code versus interpreted languages - for most people, assembler is hard to maintain, whereas interpreted scripting languages are far more tractable; on the other hand, there's no doubt which is faster.

In your case, I think the total amount of code will increase - "extract method" as a refactoring, for instance, would increase the amount of code by a few lines - but it should be easier to cache. It should also be easier to identify exactly where the bug occurs.


What's your take on this issue?

THere is a bottleneck where you pinpointed it. Right now the view performance is unacceptable if it is 500 (timing out). It needs to be fixed if it is to become usable.

Do you typically avoid logic heavy views

Always. This is what MVC pattern is designed to do. Keep as much logic to your controllers as possible, Views should be presentation only.

It seems too much is trying to be put into one view. If you want to post some code it would be easier to make suggestions as to what needs to be looked at. To me the biggest offender is conditionals testing for post parameters. I would think that this almost certainly belongs in your controller. What are these post params being used for??

If the post params dictate the output type, it would be cleaner to seperate into different views, instead of handling multiple different output types in your one super view.


First, I'd first check the execution time limit in php settings. The situation you described looks very like that issue: sometimes script manages to get there in time, and sometimes not. ) More about that here.

Second, yes, you should avoid logic heavy views. Honestly speaking, I don't know how to do it in CodeIgniter, but in Zend Framework there are so called view helpers, that can be used to separate the view-related (but heavy) logic from templates.

Finally, have you already profiled your script - with XDebug or some other similar tool?

UPDATE: A case when "conditionals testing for post parameters" is sitting cosily within the view (and just resists all attempts to be moved into Controller) is... interesting, to say the least. I'm sorry, but what's the point of using MVC framework then?