Page 1 of 1

I want to improve my coding style, any suggestions??

Posted: Fri Mar 02, 2007 12:36 pm
by PHPycho
Hello forums,
Here i am mentioning the style of my coding.
This is for the admin section,
Suppose i had any section(say portfolio in this case) which i had manage
ie
list | add | edit | delete
For that I always make the four files for every section
ie

Code: Select all

listSection.php (for listing)
addSection.php (for add section)
editSection.php (for edit section)
deleteSection.php (for delete section)

and separate .tpl files for HTML section
ie

Code: Select all

listSection.tpl
addSection.tpl
editSection.tpl

And i perform operations ie list | add | edit | delete in listSection.php
My listSection.php (say listPortfolio.php in this case)

Code: Select all

<?php
/********************************************************
* PORTFOLIO OPERATIONS
********************************************************/
/* DELETE */
if(isset($_GET['mode']) &&
$_GET['mode'] == "deletePortfolio" &&
isset($_GET['deleteID']))
{
$deleteID = $_GET['deleteID'];
$portfolioObj->delete($deleteID);
}
/* ADD */
if(isset($_POST['mode']) &&
$_POST['mode'] == "addPortfolio")
{
$portfolio_name = $_POST['portfolio_name'];
//print_r($_POST);
$portfolioObj->insert($portfolio_name);
}
/* EDIT */
if(isset($_POST['mode']) &&
$_POST['mode'] == "editPortfolio" &&
isset($_GET['editID']))
{
$editID = $_GET['editID'];
$portfolio_name = $_POST['portfolio_name'];
$portfolioObj->update($editID,$portfolio_name);
}
?>

<?PHP
/* FETCH ALL THE PORTFOLIOS */
$listPortfolio = $portfolioObj->selectAll();
//echo $listPortfolio[0];
//print_r($listPortfolio[1]);
if($listPortfolio[0] == 0)
{
$smarty->assign("error","No existing portfolio !!");
$smarty->display("errorDisplay.tpl");
$smarty->display("addPortfolio.tpl");
}
else
{
$listPortfolio = $listPortfolio[1];
$smarty->assign("listPortfolio",$listPortfolio);
$smarty->display("listPortfolio.tpl");
}
?>
addPortfolio.php:

Code: Select all

<?php
$smarty->display("addPortfolio.tpl");
?>
editPortfolio.php:

Code: Select all

<?php
/* FETCH THE REQUIRED PORTFOLIO */
if(isset($_GET['editID']))
{	
 $editID = $_GET['editID'];
 $getPortfolio = $portfolioObj->selectOne($editID);
 $smarty->assign("getPortfolio",$getPortfolio);
 $smarty->display("editPortfolio.tpl");
}
else
{
$smarty->assign("error","Error Occurred");
$smarty->display("errorDisplay.tpl");
}
?>
(here i didnt mention the .tpl files to reduce bulkiness)
----------------------------------------------------------------------------------------------------------------
My this style seems easy to maintain but its time consuming and doesnt look pro type coding..
Is there any suggestion ie any style for managing sections with ease creating less file ??

Thanks in advance to all of you !!

Posted: Fri Mar 02, 2007 1:46 pm
by feyd
Proper indentation is the first step of many.

Posted: Fri Mar 02, 2007 2:04 pm
by infolock
1 line php files urks me more than almost any thing else in the php world. but that's just me (=

Posted: Fri Mar 02, 2007 2:08 pm
by PHPycho
Proper indentation is the first step of many
Pardon me. what does that mean ?

Posted: Fri Mar 02, 2007 2:12 pm
by Christopher
I would go in the direction of all-in-one CRUD controllers. So the controller would have methods for the different operations and the URL would be

http://www.mysite.com/portfolio/list
http://www.mysite.com/portfolio/list/add
http://www.mysite.com/portfolio/list/edit/42
http://www.mysite.com/portfolio/list/delete/42

There is more code reuse in CRUD than there first appears and it certainly cuts down on all the sets of controllers.

Posted: Fri Mar 02, 2007 3:36 pm
by Begby
One thing is that your code needs to be more secure. Never trust any input from $_POST or $_GET or from cookies. Always double check that stuff to make sure its the correct type and length etc. Also make sure and use mysql_real_escape_string() before you use anything from $_GET or $_POST in a query or use prepared statements with PDO or Pear.

By proper indentation he means the following.

Code: Select all

// this is bad

foreach($stuff as $array)
{
foreach($array as $item)
{
echo $item ;
}
}


// this is good
foreach($stuff as $array)
{
  foreach($array as $item)
  {
     echo $item ;
  }
}
Making code that is consistently formatted, well commented, and easy to read is just as important as making code that works.

Posted: Mon Mar 05, 2007 12:13 pm
by PHPycho
Knock Knock !!
Help me in improving my coding style, Any suggestions???

Posted: Mon Mar 05, 2007 12:22 pm
by s.dot
Your coding style will change many, many times over the course of your coding journeys. Everyone's has. So just find something you like, and stick with it.

Not to get into a discussion about personal coding habbits, but readability is the biggest thing for me. I like for things to be pretty

Code: Select all

<?php

/**
** pretty comments
** plenty of space between commands
**/

if($something_is_true)
{
    echo 'I like to have long variable names separated by underscores.  I also like to bring my "{" to it\'s own line.';
}

$variable_name = 'I also like to only use single quotes and '.$concatenate.' when I need to.';

?>

Posted: Mon Mar 05, 2007 12:42 pm
by PHPycho
Thanks Mr. scottayy
for your valueable suggestions

What i actually want is :
some profession way of managing list \ edit \ add \ delete for particular section in one file
either by using classes , functions or any suitable means. If some body has such style of managing
contents Please do share here........
Thanks in advance to forum which has given a lot to me..

Posted: Mon Mar 05, 2007 2:28 pm
by RobertGonzalez
What you are asking for is mostly subjective, and will vary based on peoples opinion of code structure and standards. This is something that you really should look to develop for yourself.

Posted: Tue Mar 06, 2007 1:06 am
by jmut
PHPycho wrote:Thanks Mr. scottayy
for your valueable suggestions

What i actually want is :
some profession way of managing list \ edit \ add \ delete for particular section in one file
either by using classes , functions or any suitable means. If some body has such style of managing
contents Please do share here........
Thanks in advance to forum which has given a lot to me..
My suggestions:

1. Look PEAR coding standard, Zend Framework coding standard to make code more readable and see good example on how code could easily be structured/organized.
2. At first you could try completing your tasks using pseudo code. This way you will abstract yourself from the complexity of the programming language
and just think over the workflow/logic of your program.
3. Another good approach would be...imagine you have the perfect library(classes written for your exact needs)... and just write the code using it(as if there). Of course code will crash.... But when you start building your classes.....just hardcode some values at first. Don't think about how exacly will be implemented, think of input/output (what you give to method and what you get as result). Unit testing comes easily this way too. At some point when all seems clear, implementation will be piece of cake.

Bottom line you should end up thinking ahead of how things will be handled, by the time you solve your problem using pseudo code/diagrams or whatever you will see code duplications (the evil of all evils) occuring, will rethink here and there. Ultimately you will make this update/delete thing without much duplication, pure and understandable workflow and you will feel happy :)

All points mentioned overlap to some extend, and it takes some time to master. Be patient and persistant.
Happy coding.

Posted: Tue Mar 06, 2007 6:04 am
by PHPycho
Thanks for valueable suggestions!!
I think my view and model part are OK..
I am trying to implement a good controller part.
If anybody has good Controller style then please do share
I just wanna get the idea , dont wanna be copy pastier..
Thanks in advance to all of you

Posted: Tue Mar 06, 2007 7:02 am
by CoderGoblin
Some of these I guess are not really coding per se, but

1) I would also go for a PEAR/Zend Framework coding style with correct indenting.
2) Never trust any data from other sources.
3) Code Defensively : users rarely do exactly what you want. What happens if the date the user enters isn't in the format you expect ? What happens if your
database is down etc.
4) Annotate : (you may something like phpDocumenter to be helpful). You often don't know what something does which you coded a couple of years ago.
5) Never hide errors when developing. You can switch errors off when deploying but not before (no using the @ command).
6) Always think about reuse as part of your design.
7) K.I.S - Keep It Simple. Within boundaries of fulfilling requirements of course. I have seen a multitude of occasions where people have tried to code additional bells and whistles and run out of time to complete the basics.

Personally I would also add the following...
Code for no javascript if a site is for the general public. Then insert javascript afterwards (hijacking). This way your site will work even if javascript is off. If you have javascript validation, you need it in PHP anyway (See point 2).
Avoid javascript alerts if possible. Remember they need additional user movement/clicks.