• Home
  • Contact
blog.biernacki.ca

Kuba's Online Workshop

  • Home
  • Contact

Write your code correct the first time around

06/28/2012 How To Thoughts & Rants No Comments

After hanging around on StackOverflow.com for so long, I’ve started seeing bad programming patterns in the way new posters go about creating code, specifically using CodeIgniter, an MVC Framework. The code they write almost always shows a general lack of understanding of MVC design.

A typical ‘bad programmer’ will create a controller, which would just call a view:

function index()
{
	$this->load->view('process_something_page');
}

and then their view would be jam packed with business logic, database queries, and very much a glob of code that defeats the whole purpose of utilizing an MVC framework.

WAKE UP!

  • Keep your Controllers skinny, and free of business logic
  • Stick your business logic, into your MODELS
  • Your VIEW should output results/output generated from your MODELS
  • As a programmer, do it RIGHT from the START, don’t say “I’ll fix it later”, later never comes!
  • If you don’t know where something should go, ask (StackOverflow is a great place to ask, make sure to properly define your question tho)

I’ve created a brief and somewhat comical view of what you should do to keep your MVC pattern clean. Just remember, don’t put it all in the VIEW, even if you are just doing a ‘test’, be smart about where you put things.

Controller

  • Control the flow of logic, when business logic is needed, utilize a model, once output is gathered, call a view
  • Don’t add business logic here that results in a if/else check and logic, leave that for the model, return the result to the controller
  • Keep your controller skinny, if you are calculating something, it shouldn’t be in there.

Model

  • Where your business logic goes, if you have conditions that need to be met, and data pulled on those conditions, let the model handle that lifting.
  • Most people associate a model with a database interaction, while true, that’s not all its used for. Keep that in mind.
  • Don’t format your data output for HTML, or anything specific, a models data object should be generic. Styling data should be done in the view.

View

  • Free of logic, should not have any validation, error checking, or database queries.
  • Focus on displaying data provided by the model, style the output here.
  • A view should be flexible enough that output works for command line output, web app output, or mobile output without much work.

You might ask yourself, “Why does it matter that I use proper MVC patterns?”, and in truth, it doesn’t matter unless you work in a group, or if you are the type of person that wants to maintain code in the future. As always, if I misrepresented something, or am flat out wrong, let me know. Post your comments below.

clean codecodeigniterframeworklogicmvcproper programming

The iPhone Is a Nightmare For Carriers

Adding a non-standard WIFI card into an IBM T61 laptop

Categories
  • Android
  • Apple
  • Coldfusion
  • Cool Apps
  • CSS
  • GitHub
  • Hardware
  • How To
  • In the News
  • Javascript
  • Linux
  • PHP
  • RC Hobby
  • Reviews
  • SysAdmin
  • Thoughts & Rants
  • Tinkering
Recent Comments
  • Jim on Fixing ONKYO RC-710m Remote volume issues
  • Tony on Fixing ONKYO RC-710m Remote volume issues
  • Robert Lawrence on Fixing ONKYO RC-710m Remote volume issues
  • Dasen on Fixing ONKYO RC-710m Remote volume issues
  • Ahmed on Fixing ONKYO RC-710m Remote volume issues
RollmyBlog
  • Twitter
  • WebMojo Design
  • Windsor Ultimate
Archives
  • June 2020
  • January 2015
  • June 2014
  • February 2014
  • January 2014
  • December 2013
  • May 2013
  • February 2013
  • July 2012
  • June 2012
  • February 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • November 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • June 2008
  • October 2007
  • September 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • January 2007
Proudly powered by WordPress | Theme: Doo by ThemeVS.