Reputation: 91
first off, I am a trainee as an IT specialist in a large company. We are working with PHP and MySQL the most. I have several years of experience in webdesign, so I have developed my own style of coding, of course.
Yesterday, I was blamed by my boss for having a bad coding style. Since I am working with a custom CMS here, I have to adapt to their coding style a little, of course. But sometimes, I just refuse to do just that, because I do not see any advantage in particular code parts. This is a short example of what I did:
<div id="news">
<?php
foreach ($news as $k=>$v) {
$v["Datum"]=strftime("%d.%m.%Y", strtotime($v["Datum"]));
?>
<div class="news_entry">
<div class="news_title"><span><?php echo $v["Headline"];?></span><span class="news_date"><?php echo $v["Datum"];?></span></div>
<div class="news_content"><?php echo $v["Detail"];?></div>
</div>
<?
}
?>
</div>
It is a small widget to display the recent news, nothing special, really. Since my boss had never seen this style of coding yet, he said it was really bad to code like this. I should rather adapt to his style:
<?php
$table.="<table class='sn' border=1 cellspacing=0>";
$table.="<tr><td class='ueberschrift' colspan='4'>Detail Informationen</td></tr>";
$table.="<tr><td class='us_blau_l'>Material Nr.</td><td>".$erg[1][komponente]."</td>
<td class='us_blau_l'>Sollzeit</td><td>".$erg[1][response_at]." Tage</td></tr>";
$table.="<tr><td class='us_blau_l'>iKLE:</td><td>".$erg[1][ikle]."</td><td class='us_blau_l'>Netzplan Std.:</td>
<td>".$erg[1][np_std]." h</td></tr>";
echo $table;
?>
He did not tell me why this is better (and I don't see it for myself either), so I am asking you. These examples are not the same, of course, but it is a whole different style (for me, at least) on how to output HTML within a PHP file.
Thanks you for reading so far, I will appreciate any comment of you.
Upvotes: 0
Views: 1521
Reputation: 2009
I can relate to your question, because for a considerable amount of time, I felt the same way about my code. To put it simply, the idea was "Why should anyone care about the code, if the final result is as intended?". The problem with that concept was that at the time, I was the only developer in the company, so there really wasn't anyone to compare to, or learn from, so it made sense to develop a "personal style" of coding to balance rapid development and flexibility, at least in my own opinion.
However, the problem with this approach, to learning and growing, is that we develop a tendency to exclude widely accepted concepts and create our own Golden Hammers. Why is this a problem? It might not be that obvious if you're thinking about one developer, but think about what happens when a team of "golden hammer" developers tries to unite its efforts. There will be no consensus about how to write the code because each of them is convinced he has the best approach.
Now getting back to your question. Why is the second style better than the first one? The main reason is the fact that intertwining PHP and HTML code is not a good idea. Unless you have a fairly code-friendly editor (i.e. not using a default editor like Notepad), that code will be very difficult to read, much less debug and/or alter.
As you can see, in the second example, the HTML code is composed by concatenating strings, and then displayed. Why is this version better? The first thing that comes to my mind is better responsibility segregation. You have one chunk of the code focusing on creating the table, and then another part that displays it. This way, if you want to alter the part that handles the creation, the display part remains intact, or you can change the display code without touching the creation code. In the first version, the two are one and the same, which in turn removes some flexibility from your code, along with readability. If this is not so obvious, imagine the code that creates the table in a function that returns a string. You can then echo its result in any way you want.
It might not be that easy to see here, since the code examples above have a very little scope, but we can extrapolate.
So, in closing, you should always strive for flexibility on the code part, as well as clarity. Try asking yourself "how will this code support changes?". If changing things implies that you need to rewrite code, it is the first sign of an inflexible design. Another question that is worth asking is "how will another developer use my code?". If it requires changes and/or a lot of setting up, it won't of too much use to anyone.
I hope this has been helpful in explaining the difference between the two approaches we discussed here. Have a great day and good luck with your boss! :)
Upvotes: 0
Reputation: 11437
I think one of the biggest advantages server side scripting has brought is that fact that can now mix html with our code fragments like in the first example. The second style of concatenating html into a variable and outputting the variable in one shot is reminiscent of cgi programming days. I'm afraid it's archaic and has been superseded the the first method. Look at any modern server side scripting language and that should give you enough evidence.
I'm not particularly sure about what your boss's arguments against the first are, but in general, I would tend to side with your boss if the second style is the "company style". When working in large organizations, with many developers contributing to the same code base, it is imperative that everyone follow a common coding standard. Even if we find it less intuitive (initially) and readable. This goes a long way in maintainability of the software in general and less frustrating for someone else reading your code.
In the end, I think, there is no right or wrong "style". There is the more efficient and less efficient. In your example, I'm not sure if the former is more efficient, but it certainly is more readable.
Just remember one thing, coding style is not about ego.
Upvotes: 0
Reputation: 2777
your style is a bit bad, since it doesn't separate logic from front end (like templating would). on the other hand, your bosses style is even worse.
if he said "bad coding style", then he probably meant to continue saying "you need to use my even worse coding style, since that's the way our code looks now".
well, even so, he's still right. it's easier for you to adopt their old style, than it is to change all their existing code into your style.
Upvotes: 0
Reputation: 11929
As with PHP, HTML indentation should always reflect logical structure. Use tabs (or 4 spaces).
When mixing PHP and HTML together, indent PHP blocks to match the surrounding HTML code. Closing PHP blocks should match the same indentation level as the opening block. I always use alternative braceless syntax.
http://php.net/manual/en/control-structures.alternative-syntax.php
Upvotes: 0
Reputation: 14782
I'm a fan of your solution too and i use it very often as it is just plainly much better to read on html longer than 1-2 lines than packaging it into a variable.
You might want to consider a middle ground by using heredoc syntax:
$table = <<<DELIMITER
<div class="news_entry">
<div class="news_title"><span>{$v["Headline"]}</span><span class="news_date">{$v["Datum"]}</span></div>
<div class="news_content">{$v["Detail"]}</div>
</div>
DELIMITER;
Upvotes: 0
Reputation: 46178
I think it's alway a good thing to have code norms.
I think none of the examples are really good.
You should use templates or heredoc syntax here anyway.
But for me the 1st example is more maintainable than the 2nd one.
Upvotes: 0
Reputation: 46559
In general, adopting the company's style - if they have a consistent style at all - is the better approach.
When several people are working on the same project, having consistency overall is preferable to having different styles in the different files of the project.
However, if your boss can't tell you what the actual style is that he wants you to use, that's difficult. Can't you ask him?
In this particular case, I can imagine him not liking the dancing in and out of php instead of using echo for short html snippets. Or the opening { in the first php block and the closing } in the last one. Some people hate that. But at least he should tell you.
Upvotes: 2