Quantcast
Viewing all articles
Browse latest Browse all 9

Code Smells

In recent days I have read an article about “Code Smells”. I was not aware of such a thing since then. I mean I was aware that people was writing bad code but I had not known there was a name for it. Anyway after reading the 3 part of the article at metapundit.net, I was really surprised to see people like me who actually think like me when writing code.

I know that development is about functionality, most often also about scalability and maintainability. Well most of the time you write a class constructor like:


public function Foo($a, $b, $c) {
$this->a = $a;
$this->b = $b;
$this->c = $c;
}

In most of the time you write down this kind of things. The part itself is repeating! Yuu just do the same thing, copy and paste. It’s perfectly understandable to do this. Because we are in the creation of it. How about something like this:


public function($a, $b, $c) {
$args = array("a", "b", "c");
foreach ($args as $arg)
$this->$arg = $$arg;
}

Is it cool? I know that it looks like lame. But isn’t the previous one looked lame too. I have not even reduced the lines of code. But think of adding another parameter to the function, then you have to copy paste another line doing the same thing but you just add another item in the array and lines are decreased. Instead of defining the arguments on the place where you define functions, you may use “func_get_args()” to get them and use the $args array to set them as you know which param is coming. Same thing can be done for most of the set and get functions


public function get($arg) {
return $this->$arg;
}
public function set($arg, $value) {
$this->$arg = $value;
}

Is it lame? Kind of, as you have lost lots of methods about getting and setting. I know that gets and sets are not used by this way. You may add some validations to it or some protections on it. Maybe you want to set to a new date and check if the day is 32 or not or if the month is 13 or something else. But that’s not the point. What I try to mean is, by this way you write less and create a more maintainable code I guess. It’s about functionality I suppose.

As metapundit mentioned, first thing we learn about programming is “loops”. The basic loops and, in most of the cases we pass down this first lesson and keep hard coding. How many of you created a form like:


<table>
<tr>
<td>Field 1</td>
<td><input type="text" name="field1"></td>
</tr>
<tr>
<td>Field 2</td>
<td><input type="text" name="field2"></td>
</tr>
<tr>
<td>Field 3</td>
<td><input type="text" name="field3"></td>
</tr>
</table>

Have you though of:


<table>
<?php
$fields = array("field1" => "Field 1", "field2" => "Field 2", "field3" => "Field 3");
foreach ($fields as $key => $val) {
echo '<tr><td><?=$val?></td><td><input type="text" name="<?=$key?>"></td></tr>';
}
?>
</table>

Isn’t this doing the same thing while giving you an understandable and maintainable code? I know that logic and design must be separated. So in this case our sample will change into something like:


<?php
$fields = array("field1" => "Field 1", "field2" => "Field 2", "field3" => "Field 3");
foreach ($fields as $key => $val) {
$template->setBlock("TEMPLATE1");
$template->setVariable(array("KEY"=>$key, "VAL" => $val));
$template->parseBlock();
}
?>
<table>
<!-- BEGIN TEMPLATE1 -->
<tr><td>{VAL}</td><td><input type="text" name="{KEY}"</td></tr>
<!-- END TEMPLATE1 -->
</table>

Syntax may change but the idea remains. You can also improve the way you define your form field elements in a way that template’s variable setting method accept it as it is.


<?php
$fields = array(array("key" => "field1", "val" => "Field 1"), array("key" => "field2", "val" => "Field 2"),array( "key" => "field3", "val" =>"Field 3");
for( $i = 0 ; $i < count($fields) ; $i++ ) {
$template->setBlock("TEMPLATE1");
$template->setVariable($fields[$i]);
$template->parseBlock();
}
?>
<table>
<!-- BEGIN TEMPLATE1 -->
<tr><td>{VAL}</td><td><input type="text" name="{KEY}"</td></tr>
<!-- END TEMPLATE1 -->
</table>

Remarked something? I repeat the way I define my variables. So:


<?php
$old_fields = array("field1" => "Field 1", "field2" => "Field 2", "field3" => "Field 3");
foreach ( $old_fields as $key => $val) {
$fields[] array("key" => $key, "val" => $val);
}
for( $i = 0 ; $i < count($fields) ; $i++ ) {
$template->setBlock("TEMPLATE1");
$template->setVariable($fields[$i]);
$template->parseBlock();
}
?>
<table>
<!-- BEGIN TEMPLATE1 -->
<tr><td>{VAL}</td><td><input type="text" name="{KEY}"</td></tr>
<!-- END TEMPLATE1 -->
</table>

You may not want to the last part, but I hope that I was able to explain what I meant.

Anyway I really try to write code like this. Whenever I copy paste a part of a code to somewhere else, I think of creating a function or a loop from it. But in most of the time as my time is limited, I note it down to do it in a later time which I don’t. Only in Boop I try to refactor the code I have written as I’m not pressed on releasing it in a mean time.

Anyway I have given some examples from metapundit and also created my owns too. I hope I was able to explain what I thought when I read about his article. I was really happy to find out someone who was thinking like me but still making that mistakes, just like me!


Viewing all articles
Browse latest Browse all 9

Trending Articles