<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.mogosanu.ro &#187; coding</title>
	<atom:link href="http://blog.mogosanu.ro/tag/coding/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.mogosanu.ro</link>
	<description>the tutorial blog</description>
	<lastBuildDate>Sat, 03 Apr 2010 07:26:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cod PHP scris corect</title>
		<link>http://blog.mogosanu.ro/general/cod-php-scris-corect/</link>
		<comments>http://blog.mogosanu.ro/general/cod-php-scris-corect/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 10:50:16 +0000</pubDate>
		<dc:creator>bogdanmogo</dc:creator>
				<category><![CDATA[$var = "PHP/MySQL"]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.mogosanu.ro/?p=176</guid>
		<description><![CDATA[1. Adaugarea de comentarii la cod
Pentru o buna intelegere a scriptului trebuie adaugat un comentariu principal (care contine diverse informatii despre script) si comentarii la partile de cod unde este necesar.
Iata un sample dintr-un script din CodeIgniter:

/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package      [...]]]></description>
			<content:encoded><![CDATA[<h2>1. Adaugarea de comentarii la cod</h2>
<p>Pentru o buna intelegere a scriptului trebuie adaugat un comentariu principal (care contine diverse informatii despre script) si comentarii la partile de cod unde este necesar.</p>
<p>Iata un sample dintr-un script din CodeIgniter:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package         CodeIgniter
* @author          ExpressionEngine Dev Team
* @copyright       Copyright (c) 2006, EllisLab, Inc.
* @license         http://codeigniter.com/user_guide/license.html
* @link            http://codeigniter.com
* @since           Version 1.0
* @filesource
*/</span>
&nbsp;
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CI_VERSION'</span><span style="color: #339933;">,</span>    <span style="color: #0000ff;">'1.6.3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
* ------------------------------------------------------
*  Load the global functions
* ------------------------------------------------------
*/</span>
&nbsp;
<span style="color: #b1b100;">require</span><span style="color: #009900;">&#40;</span>BASEPATH<span style="color: #339933;">.</span><span style="color: #0000ff;">'codeigniter/Common'</span><span style="color: #339933;">.</span>EXT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
* ------------------------------------------------------
*    Is there a valid cache file?  If so, we're done...
* ------------------------------------------------------
*/</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$EXT</span><span style="color: #339933;">-&gt;</span>_call_hook<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_override'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$OUT</span><span style="color: #339933;">-&gt;</span>_display_cache<span style="color: #009900;">&#40;</span><span style="color: #000088;">$CFG</span><span style="color: #339933;">,</span> <span style="color: #000088;">$URI</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
* ------------------------------------------------------
*  Close the DB connection if one exists
* ------------------------------------------------------
*/</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CI_DB'</span><span style="color: #009900;">&#41;</span> AND <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Un alt stil de a scrie codul este (desi unii spun ca aceasta varianta nu este corecta):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$EXT</span><span style="color: #339933;">-&gt;</span>_call_hook<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cache_override'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">FALSE</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$OUT</span><span style="color: #339933;">-&gt;</span>_display_cache<span style="color: #009900;">&#40;</span><span style="color: #000088;">$CFG</span><span style="color: #339933;">,</span> <span style="color: #000088;">$URI</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #009900; font-weight: bold;">TRUE</span><span style="color: #009900;">&#41;</span>
         <span style="color: #990000;">exit</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'CI_DB'</span><span style="color: #009900;">&#41;</span> AND <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$CI</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>2. Denumirea variabilelor si a functiilor</h2>
<p>O buna practica este denumirea variabilelor si a functiilor cu <em>underscore</em> intre cuvinte sau cuvintele sa inceapa cu litera mare (CamelCase). Nu este indicat ca variabilele sa aiba denumiri foarte lungi. De asemenea numele unei variabile sau a unei functii trebuie sa fie sugestiv.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> ShowTitle<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>sau</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> show_title<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">strip_tags</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$name</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Asa nu:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$k</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// variabilele dintr-un singur caracter</span>
         <span style="color: #666666; font-style: italic;">//sunt folosite de obicei pentru loop-uri</span>
&nbsp;
<span style="color: #000088;">$variabilatitlu234</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// nu este sugestiv</span>
                     <span style="color: #666666; font-style: italic;">// nu sunt folosite underscores sau stilul CamelCase</span></pre></div></div>

<h2>3. Indentarea si gruparea codului</h2>
<p>Intotdeauna este necesara o indentare si o grupare corecta, pentru a distinge inceputul unei functii, a unui ciclu sau a altor pasi din script.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"> 	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$zip_data</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$directory</span> 	<span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$entries</span> 	<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
 	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$file_num</span> 	<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$offset</span>	<span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> CI_Zip<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log_message<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'debug'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Zip Compression Class Initialized&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Desigur nu este indicat sa abuzam de tab-uri sau de linii goale. Nu facem decat sa incarcam scriptul cu blank-uri care cresc dimensiunea fisierului.</p>
<h2>4. Limitarea numarului de caractere de pe o linie</h2>
<p>De ce sa avem ecrane laterale, cand putem avea doar ecrane pe verticala? De exemplu: este mult mai usor de citit un query SQL care nu se intinde mult pe o singura linie.</p>
<h2>5. Capitalizarea sintaxei SQL</h2>
<p>Datorita faptului ca unele query-urile SQL sunt mai lungi, ne este greu sa gasim anumite elemente din query. Pentru a usura operatiunea de cautare este bine sa capitalizam cuvintele specifice limbajului SQL.</p>
<p>Asa nu:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">select</span> id<span style="color: #000033;">,</span> name<span style="color: #000033;">,</span> title<span style="color: #000033;">,</span> slug<span style="color: #000033;">,</span> author<span style="color: #000033;">,</span> category<span style="color: #000033;">,</span> views<span style="color: #000033;">,</span> <span style="color: #000099;">date_add</span><span style="color: #000033;">,</span> date_mod <span style="color: #990099; font-weight: bold;">from</span> news <span style="color: #990099; font-weight: bold;">where</span> category <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #990099; font-weight: bold;">order by</span> <span style="color: #000099;">date_add</span> <span style="color: #990099; font-weight: bold;">desc</span></pre></div></div>

<p>Iata varianta corecta:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span>  id<span style="color: #000033;">,</span> name<span style="color: #000033;">,</span> title<span style="color: #000033;">,</span> slug<span style="color: #000033;">,</span> author<span style="color: #000033;">,</span>
        category<span style="color: #000033;">,</span> views<span style="color: #000033;">,</span> <span style="color: #000099;">date_add</span><span style="color: #000033;">,</span> date_mod
    <span style="color: #990099; font-weight: bold;">FROM</span> news
    <span style="color: #990099; font-weight: bold;">WHERE</span> category <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span>
    <span style="color: #990099; font-weight: bold;">ORDER BY</span> <span style="color: #000099;">date_add</span> <span style="color: #990099; font-weight: bold;">DESC</span></pre></div></div>

<h2>6. Repetarea unor bucati de cod</h2>
<p>Unul din principalele motive pentru care folosim functii este acela de a nu repeta bucati de cod. Datorita functiilor vom face mai usor anumite modificari asupra unor parti din cod, dar vom avea si o dimensiune mai mica a scriptului.</p>
<p>&#8212;&#8212;&#8212;&#8212;</p>
<p>Resurse:</p>
<p>1. <a href="http://codeigniter.com/user_guide/general/styleguide.html" target="_blank">CodeIgniter User Guide</a></p>
<p>2. <a href="http://net.tutsplus.com/tutorials/html-css-techniques/top-15-best-practices-for-writing-super-readable-code/" target="_blank">Top 15+ Best Practices for Writing Super Readable Code</a></p>
<p>3. <a href="http://www.devshed.com/c/a/PHP/Writing-Clean-and-Efficient-PHP-Code/" target="_blank">Writing Clean and Efficient PHP Code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.mogosanu.ro/general/cod-php-scris-corect/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

