4, February 2012

A tool that helps you write better code - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: A tool that helps you write better code
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: A tool that helps you write better code  (Read 2058 times)
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5772
46229 credits
Members referred : 3



« on: Sep 21, 2006, 10:42:22 am »

Just read here about CodeSniffer, a PEAR package that parses the code of your scripts, and creating a report regarding it.

Typical reports can show up like this :

Quote
[LINE 62] WARNING: Equals sign not aligned with surrounding assignments. Expected 3 spaces, but found 1 space.
[LINE 99] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.
[LINE 101] WARNING: Equals sign not aligned with surrounding assignments. Expected 4 spaces, but found 1 space.
[LINE 145] WARNING: Equals sign not aligned with surrounding assignments. Expected 2 spaces, but found 1 space.
[LINE 156] WARNING: Line exceeds 85 characters. It is recommended that this be shortened.

Seems like a good tool to optimize scripts and learn how to write better code. I think it is time to install PEAR in my development server Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Sep 21, 2006, 12:32:32 pm »

Yes this will help you top writye code fopr pearl Wink
I think that writing clear code is important and I'm using my own style...
I don't like this
Code:
<?php
function some_name() 
{
  
// code here
}

I write it this way
Code:
<?php
function some_name() {
  
// code here
}


Last blog : A new Wordpress theme for our blog
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5772
46229 credits
Members referred : 3



« Reply #2 on: Sep 21, 2006, 12:35:41 pm »

I prefer the opposite because it helps me look the code clearer (after so many years my eyes are a little......)

Also I like having spaces, eg.

Code:
<?php
 $a 
str_replace'Something''Something else'$aVariable );

But I guess having your personal style is ok with php, as there is no standard editor that edits your coding (microsoft's languages automatically change your code to what they want...)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Sep 21, 2006, 12:40:10 pm »

I use spaces too:
Code:
<?php
$var 
100 12;
foreach (
$array as $var => $key) {
  
// code
}
for (
$i 0$i 6$i++) {
  
// code
}

I think your curly breackets are better to close sinds they are on the same position.



Last blog : A new Wordpress theme for our blog
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #4 on: Sep 21, 2006, 01:46:53 pm »

I add a [tab] in front of the curly brackets, I find this method gives a better overview of nested if's...
Code:
<?php
$var 
100 12;
foreach (
$array as $var => $key
   {
   
//code
   
if (== b)
       {
       
// code
       
}
   else
       {
       
// code
       
}
   }
for (
$i 0$i 6$i++) 
   {
   
// code
   
}


Last blog : Are You Stumbling Yet?
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #5 on: Sep 21, 2006, 02:21:51 pm »

I do the same but this way:

Code:
<?php
foreach ($array as $var => $key) {
   
//code
   
if (== b) {
       
// code
   
} else {
       
// code
   
}
}

needs less rows (better to forcome RSI)


Last blog : A new Wordpress theme for our blog
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #6 on: Oct 30, 2006, 11:14:49 pm »

I write my codes like this:

<html>
<head>
<title> </title>
</head>
<body bgcolor="#000000" leftmargin="30" topmargin="30" bottommargin="0" rightmargin="0" link="#FFFF00"

alink="#FFFFFF" vlink="#FFFF00">

<font face=arial size=2 color=white>

<?php

/*
*   obtiene el n&#250;mero de identificaci&#243;n del paciente
*/
$id=$_GET['identificacion'];

/*
*   si el n&#250;mero de identificaci&#243;n fu&#233; asignado mediante
*   la forma en html abajo incluida, entonces ejecutamos..
*/
   if($id)
      {
/*
*   hacemos la conexi&#243;n a la base de datos
*/
$conn = mysql_connect("localhost", "root", "");

if (!$conn) {
   echo "La contrase&#241;a de la base de datos o el usuario pueden no existir, error: " . mysql_error();
   exit;
}
 
if (!mysql_select_db("auto_mantenimiento")) {
   echo "Puede no existir la base de datos, error: " . mysql_error();
   exit;
}

/*
*   este query busca los resultados que tengan el username
*   asignado en la forma en html abajo incluida..
*/
$sql = "SELECT * 
       FROM  auto_mtto
       WHERE  identificacion=$id";

$result = mysql_query($sql);

if (!$result) {
   echo "No se pudo correr el query ($sql) en la base de datos, error: " . mysql_error();
   exit;
}

if (mysql_num_rows($result) == 0) {
   echo "No hubo resultados que mostrar.";
   exit;
}

/*
*   imprime los resultados obtenidos (pudiendo incluso ser m&#225;s de un row)
*/
while ($row = mysql_fetch_assoc($result)) {
   echo "el art&#237;culo n&#250;mero: ";
   echo $row["identificacion"];
   echo "<BR>tiene una hora de ejecuci&#243;n a las: ";
   echo $row["hora_ejecucion"];
}

mysql_free_result($result);
      }
?> 


<!--- la forma que trae los resultados mediante el numero de identificacion --->
   <form name="search" action="show.php" method="GET">
     Elegir un ID:<BR>
   <input type="text" name="identificacion" value="">
   <input type="submit" name="submit" value="Buscar"><BR>
   </form>
</font>
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Oct 30, 2006, 11:26:26 pm »

Quote
I write my codes like this:


NICE!


Last blog : A new Wordpress theme for our blog
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #8 on: Oct 30, 2006, 11:41:47 pm »

thanks man n_n
it takes a little bit more of space in the physical file but oh well.. it's clearer than other formats because of the break lines and everything..
im learning a lot from this forums!! Grin
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6691
34714 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Oct 31, 2006, 07:17:09 am »

thanks man n_n
it takes a little bit more of space in the physical file but oh well.. it's clearer than other formats because of the break lines and everything..
im learning a lot from this forums!! Grin
absolutly code must be easy to read and clear (always think about that you need to change/and understand the code also 2 years later)


Last blog : A new Wordpress theme for our blog
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #10 on: Oct 31, 2006, 08:05:46 am »

There's always that...
I wouldn't need 2 years to forget what a function does.. considering my short memory it would take a couple of weeks haha.. probably way less than 2 week..
commenting it is very important. I just wonder if there's any way to encrypt my scripts via a password or something so no one read my comments and steal my code.
I'm trying to make money of it sometimes. Wink
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5772
46229 credits
Members referred : 3



« Reply #11 on: Oct 31, 2006, 06:45:29 pm »

eliezer, this is good when you just "scripting". If you have an application envirenment it should be more than that, and have modules/template system to help you write code more organized and easy.

Anyway regarding your encode question, you can try a compiler like eAccelerator (which is not so good - many times can crash the system) or the Zend encoder which is a little expensive, but does great job Smiley

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
OMG!I am geek
**
Gender: Male
Posts: 55
366 credits
Members referred : 0


my day will come..


« Reply #12 on: Oct 31, 2006, 10:26:25 pm »

Hey Nikolas!
Yeaaah, I had the chance to work for one month at Woodwing (www.woodwing.net) and everything we programmed had to use templates and everything because the programs were translated into about 10-13 languages. So we always used templates, css, includes, etc..

That's the best way to do programming, you change just one or two files and see how all the scripts change. Very useful huh?

I can't afford right now to buy an encoder, but I'll google for free encoders and see if I'm lucky..
Greetings man!!! Grin
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=4197
Tags : google html zend PEAR Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: A tool that helps you write better code
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Feb 04, 2012, 11:43:54 am





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.