Today’s a good day to start!

I’m still not sure what I’m going to do with this space, probably share my thoughts about programming stuff, maybe add some cool code examples such as the following code to quick-sort in Erland:

qsort([]) -> [];
qsort([Pivot|Rest]) ->
qsort([Front || Front <- Rest, Front < Pivot])
++ [Pivot] ++
qsort([Back || Back <- Rest, Back >= Pivot]).

Now, here’s a small example of how NOT to write code, the example is in PHP:

public function pdf_read_root(){
   $this->root = $this->pdf_resolve_object( $this->c, $this->pdf_find_root());
}

This line of code is wrong. very wrong. Let’s see why:

    • Even if we don’t know why, the fact that $this-> appears 4 times in the same line should raise a red-flag!
    • Since pdf_resolve_object() is a class method, it has direct access to all the members so we shouldn’t pass class-members in the signature: pdf_resolve_object( $this->c, $this->pdf_find_root()).
  • From the same reason, this method does NOT require a return value, it can update$this->root inside the method and just return.
  • $this->c ? what about a more meaningful name ?
  • And last, why should we wrap this one line that calls to a method with another method that does nothing: pdf_read_root()?

To sum up, we can easily change this line to something much nicer, such as:

$this->pdf_resolve_object();

Ta-ta till next time.

Advertisements
Today’s a good day to start!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s