Jump to content

Advice for cleaner coding


ShaolinF

Recommended Posts

Hey Guys,

 

I have noticed that since coding in much larger projects my coding tends to be all over the place, esp. db queries and sessions. Do you guys have any advice/tips on how I can keep my code clean ? It gets fustrating because I like to keep my code as clean as possible but right now all the php/htm is mixed up and sometimes have a hard time working out what I did, even with comments! So any tips from more experienced coders will be taken in with open arms.  ;D

 

Link to comment
Share on other sites

Keep your coding style as consistent as psossible. ie; Pick your style and stick to it for the duration of your project.

 

As for mixing html with php there are many different ways to avoid this. I prefer to use the mvc approuch where all my bussiness logic is kept completely seperate from my display logic. others prefer template engines or a mix of both.

Link to comment
Share on other sites

i use a lot of

includes, classes, and functions

also be sure to use tabs in

your coding, it makes it

easier to read.

The reason I use includes,

classes, and functions a lot

is because I notice that most

of the things that i code, i re-use

so its easier to define them once

and then just call them when I

need them  ;)

Link to comment
Share on other sites

Personally I jsut work it so that a lot or my reused code is included in wrapper functions:

 

eg - my database link file has wrappers for a lot of the mysqli_/ mysql_ functions - and it have a variable at the top of the document that defines 4.1 - or 5.2 or somethign like that.  So that if i have to changce servers ( which i have in teh past) - its easy to port across because it simply checks the version and then acts on it.

 

as for clean code - simply think abut ways to save coding lines - and comment sections:

 

eg - if you have to set a cookie with the same name - but a different value then its easier to read :

 

if(condition) {$val = "";}

if(condition) {$val = "";}

if(condition) {$val = "";}

if(condition) {$val = "";}

...

 

setcookie("name", $val)....;

 

 

 

than it is to read:

if(condition) {setcookie('name', 'val1')...;}

if(condition) {setcookie('name', 'val2')...;}

if(condition) {setcookie('name', 'val3')...;}

... etc etc - becaquse its a shorter line therefore it seems like theres less to read.

 

also - comment the more complecated stuff like regex and stuff liek that because they arent really human readable.  as prefiosly mentioned - have a style and stick to it.  youll get your own eventually and youll just be used to it.

 

eg i like:

 

if() {

  ..............

}

 

as oposed to:

 

if()

{

      .......;

}

 

 

 

gdlk - ( also - coding editors help you read code because you can easily tell where strings end and the variables are etc.)

 

 

gdlk

 

Link to comment
Share on other sites

I never write a piece of code twice. The second I find a piece of code needs to be rewritten somewhere else, I pull it out of the first document I wrote it in, and turn it into a function. I have one php file full of all thesefunctions that I include into the top of any script that uses those functions.

 

I also have one header file that I use across any document that outputs to the screen. I have a lot of php in this file that decides what javascript links to output (based on what page it is), what CSS style sheet links to output, and what title to output. It also includes my doctypes, and the opening body tag of my documents. That way I can include this header file into any document, and I just start doing the content that goes in the header. This means that if I need to change any information in the header, I only have to change it in the one spot. I also have another file for menus, and another one for a footer. So each php document usually consists of an include with a header, a menu, then has content done for that document, and finally ends up with an include for the footer. Keeps it nice and clean inside the document.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.