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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

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

Post 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 !!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Proper indentation is the first step of many.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

1 line php files urks me more than almost any thing else in the php world. but that's just me (=
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Proper indentation is the first step of many
Pardon me. what does that mean ?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Begby
Forum Regular
Posts: 575
Joined: Wed Dec 13, 2006 10:28 am

Post 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.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

Knock Knock !!
Help me in improving my coding style, Any suggestions???
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.';

?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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..
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post 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
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply