# Service: Code review

**URL:** <https://www.phphelp.com/t/service-code-review/23875>\
**Category:** General Chat\
**Created:** [January 9, 2015, 10:05pm UTC](https://www.phphelp.com/t/service-code-review/23875 "2015-01-09T22:05:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [January 9, 2015, 10:05pm UTC](https://www.phphelp.com/t/service-code-review/23875/1 "2015-01-09T22:05:27Z")

</div>

Should perhaps get its own forum. But let’s try it out in a single thread to see if there is any interest.

Any code can be posted, any user can review.

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [February 15, 2015, 6:26am UTC](https://www.phphelp.com/t/service-code-review/23875/2 "2015-02-15T06:26:41Z")

</div>

So, JimL, what do you mean Code review? A discussion about PHP code? Or Services?

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [February 15, 2015, 6:46am UTC](https://www.phphelp.com/t/service-code-review/23875/3 "2015-02-15T06:46:09Z")

</div>

Code review is normally a step between development and testing where we have code that works but should be checked by another developer to see if it conforms to code styles, or if there are improvements that should be done

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [February 15, 2015, 4:24pm UTC](https://www.phphelp.com/t/service-code-review/23875/4 "2015-02-15T16:24:58Z")

</div>

I see. Yes, that step is important. I think the issue of “code styles” can open a huge door into a thread  
that can go on for years! LOL I find that most programmers have their own styles. I also noticed that  
most programmers who work in an organizations have to abide by the company’s rules.

This can also bring up various other interesting issues. Such as MySQL vs MySQLi vs PDO. I get really  
very tired of some people trying to push others to use THEIR version of this. I always use MySQL because  
my personal programming is fun stuff and I normally do not need to publish it for a company. Therefore,  
I now use MySQLi as it is the latest for that code. PDO is best if you use OOP. For anyone who does not  
know OOP is Object Orientated Programming. I do feel that OOP is the better way to go and therefore  
that PDO is better, too. BUT, most people on this site are learning something. So, mention OOP might be  
better in the long run, but, let them use MySQLi if they already have the code set up like that.

I do feel that there are a ton of simple things that should be or even MUST be done in PHP programming.  
Some things are simple, like adding comments! Why do people learn code and see comments in every  
single example they learn from and then put no comments into their own code? Crazy!!! And, like when  
they add in functions. Most people do not understand that PHP searches the current file when it needs to  
call a function. Place your functions at the top of the page. Most experienced programmers load all of their  
functions into a separate file and use and INCLUDE to add it at the top of the page. I find a lot of newbies  
put functions inside of IF clauses. Crazy flow to do that. You want IF’s, FOR’s, While’s and all loops to be  
as short as possible.

Well, there is a bit to get the thread started… Hope that is what you were looking for…

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [February 15, 2015, 5:32pm UTC](https://www.phphelp.com/t/service-code-review/23875/5 "2015-02-15T17:32:50Z")

</div>

> [@ErnieAlex](#):
>
> I see. Yes, that step is important.

Absolutely 🙂

> [@ErnieAlex](#):
>
> I think the issue of “code styles” can open a huge door into a thread that can go on for years! LOL I find that most rogrammers have their own styles. I also noticed that most programmers who work in an organizations have to abide by the company’s rules.

Yeah in an organization you kind of have to stick to a common convention.

> [@ErnieAlex](#):
>
> This can also bring up various other interesting issues. Such as MySQL vs MySQLi vs PDO. I get really  
> very tired of some people trying to push others to use THEIR version of this.

Sure, as long as you don’t need to support different types of storages then mysqli and pdo are pretty much equal. Then we can argue over the differences later. Like PDO silently falling back to “fake” parameterized queries, and mysqli having a much more verbose bindParam syntax.

> [@ErnieAlex](#):
>
> PDO is best if you use OOP. For anyone who does not know OOP is Object Orientated Programming. I do feel that OOP is the better way to go and therefore that PDO is better, too. BUT, most people on this site are learning something. So, mention OOP might be better in the long run, but, let them use MySQLi if they already have the code set up like that.

Why is PDO better for OOP?

> [@ErnieAlex](#):
>
> Some things are simple, like adding comments! Why do people learn code and see comments in every  
> single example they learn from and then put no comments into their own code? Crazy!!!

I rarely write comments, I actually get really frustrated when I see code where people have commented everything they do. I suggest you read [Clean Code](http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882).

Basic jist of the comment issue is, if you have to comment it, then you can probably refactor the code so it’s easier for the next guy to understand it. If you can’t immediately see what is going on, then you are probably doing it wrong. Good code structure, good function and variable names, etc help you with not having to write comments. Of course there are those special cases where you can’t get by without explaining what the \*\*\* you are doing. But that’s rare.

> [@ErnieAlex](#):
>
> And, like when they add in functions. Most people do not understand that PHP searches the current file when it needs to call a function. Place your functions at the top of the page. Most experienced programmers load all of their functions into a separate file and use and INCLUDE to add it at the top of the page. I find a lot of newbies put functions inside of IF clauses. Crazy flow to do that.

With inexperienced/hobby programmers you see all kinds of crazy. Hopefully most professional programmers today structure their code in some oop/mvc type of fashion.

> [@ErnieAlex](#):
>
> You want IF’s, FOR’s, While’s and all loops to be as short as possible.

Definitly, actually many guidelines have a strict “max x lines per method” rule 🙂

> [@ErnieAlex](#):
>
> Hope that is what you were looking for…

Was actually suggesting people post code they were wondering if could/should be improved ^^

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [February 15, 2015, 6:09pm UTC](https://www.phphelp.com/t/service-code-review/23875/6 "2015-02-15T18:09:19Z")

</div>

I agree with your comments. The PDO is like OOP comment is the that the general layout with =\>'s and  
all make it feel a little like OOP’s. I get that comment from many newbies that ask if that is OOP or not,  
then have to explain, well sort of in the “look” area, but…

And, I disagree with comments. Yes, a professional does not need much of them. Unless several different  
programmers share the code and are at different levels of experience. On this site, I ALWAYS add many  
comments. Here, we are mostly helping new programmers or weak ones. They need as much help as they  
can get. Therefore, short comments explaining what you and I would think of as simple and straightforward  
will help them. Then can delete the comments once they understand the code.

Hmmmmm, want code samples to tear apart?

In one of the posts that I just replied to from Princess, not sure, Web Princess? She, had code where she  
was altering a tab system inside of a WP template. I had this same problem before and manually figured  
out a work-around. But, her code was, loosely, like this in many places:

 \<?PHP some code; ?\> \<?PHP more code; ?\> \<?PHP some more complex code; code; code; code; ?\>

```
<?PHP last code; ?>
<html code>

```

I just can not see any reason in the world for that type of layout. Even if she was using some sort of  
canned programming tool that sticks in code snippets, why would you want to do that? It slows down  
the PHP parser. The parse stops, restarts, stops, etc. It adds unneeded chars and slows down loading.  
Just makes no sense to me. Any reason you can think of to have that type of formatting?

---

<div class="post-metadata">

**Author:** ![JimL](https://www.phphelp.com/user_avatar/www.phphelp.com/jiml/32/1842_2.png) [@JimL](https://www.phphelp.com/u/JimL)\
**Post date:** [February 16, 2015, 3:10pm UTC](https://www.phphelp.com/t/service-code-review/23875/7 "2015-02-16T15:10:11Z")

</div>

Hm, I wrote a response to this but it seems to be gone… -\_-

> [@ErnieAlex](#):
>
> The PDO is like OOP comment is the that the general layout with =\>'s and  
> all make it feel a little like OOP’s. I get that comment from many newbies that ask if that is OOP or not,  
> then have to explain, well sort of in the “look” area, but…

I’m not so sure, I think mysqli looks pretty much the same. note that the comparison below isn’t really fair, to make it fair you would have to change the pdo one to use bindParam and bindResult, like the mysqli example. But they all in all look very much the same

[php]$mysqli = new mysqli(“localhost”, “my\_user”, “my\_password”, “world”);

if (mysqli\_connect\_errno()) {  
printf(“Connect failed: %s\n”, mysqli\_connect\_error());  
exit();  
}

$city = “Amersfoort”;

if ($stmt = $mysqli-\>prepare(“SELECT District FROM City WHERE Name=?”)) {

```
$stmt->bind_param("s", $city);
$stmt->execute();

$stmt->bind_result($district);
$stmt->fetch();

printf("%s is in district %s\n", $city, $district);

$stmt->close();

```

}

$mysqli-\>close();[/php]

[php]$dbh = new PDO(‘mysql:host=localhost;dbname=test’, $user, $pass);

$sql = ‘SELECT name, colour, calories  
FROM fruit  
WHERE calories \< :calories AND colour = :colour’;  
$sth = $dbh-\>prepare($sql);  
$sth-\>execute(array(‘:calories’ =\> 150, ‘:colour’ =\> ‘red’));  
$red = $sth-\>fetchAll();[/php]

> [@ErnieAlex](#):
>
> And, I disagree with comments. Yes, a professional does not need much of them. Unless several different  
> programmers share the code and are at different levels of experience. On this site, I ALWAYS add many  
> comments. Here, we are mostly helping new programmers or weak ones. They need as much help as they  
> can get. Therefore, short comments explaining what you and I would think of as simple and straightforward  
> will help them. Then can delete the comments once they understand the code.

I agree, comments here where you show people how code works makes perfect sense. I was talking about when you normally code/implement stuff though, that’s when you shouldn’t have to use comments.

> [@ErnieAlex](#):
>
> I just can not see any reason in the world for that type of layout. Even if she was using some sort of  
> canned programming tool that sticks in code snippets, why would you want to do that? It slows down  
> the PHP parser. The parse stops, restarts, stops, etc. It adds unneeded chars and slows down loading.  
> Just makes no sense to me. Any reason you can think of to have that type of formatting?

No

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [February 16, 2015, 3:50pm UTC](https://www.phphelp.com/t/service-code-review/23875/8 "2015-02-16T15:50:51Z")

</div>

Yes, I agree on all your comments. I think of OOP as creating an object and then using -\> to access sub  
parts of it. A simple way to look at it, but… So, PREPARED statements remind me of that and PDO, too.

I guess since I do not really use PDO much as my projects are simple ones not for mass consumption, I just  
like the simpler looking MySQLi. (And, easier for newbies to follows as most of them use it.) So, I guess I am  
not explaining myself very clearly in those comments…

For anyone reading this thread, everyone that I talk to who is a professional programmer have all mentioned  
that they feel PDO is the way to go. It is very interesting to note that I am an old fart and have seen about  
500+ web programming languages that are long gone and for some reason I really like my PHP and MySQLi.  
My 2 cents… That combo does just about anything I want done. Well, with some fancy CSS and maybe  
even a little JS/JQuery for animations thrown in… LOL

I will try to come up with some code samples to ask if there are better ways to do for you and the thread.  
There is one question about arrays, but, not ready to ask right now… Have to find the code first…
