<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns="http://purl.org/rss/1.0/">

  <channel rdf:about="http://www.numiton.com/blog/">
    <title>the Numiton blog</title>
    <link>http://www.numiton.com/blog/</link>
    <description>on automated software migration</description>
    <items>
      <rdf:Seq>
        
        <rdf:li resource="http://www.numiton.com/blog/2008/07/09/1215624902650.html" />
        
        <rdf:li resource="http://www.numiton.com/blog/2008/07/04/1215194429366.html" />
        
        <rdf:li resource="http://www.numiton.com/blog/2008/06/20/1213952553459.html" />
        
      </rdf:Seq>
    </items>
  </channel>

  
  <item rdf:about="http://www.numiton.com/blog/2008/07/09/1215624902650.html">
    <title>PHP bad practice: the use of extract()</title>
    <link>http://www.numiton.com/blog/2008/07/09/1215624902650.html</link>
    
      
        <description>
          Working with complex data structures in PHP requires the use of associative arrays. Even PHP classes are an extension of this concept. There are always disadvantages when one does not have alternatives (e.g. strictly defined data structures - see &amp;quot;struct&amp;quot; in C), but at least there are lots of built-in functions that work with arrays. Operations such as sorting, searching, merging, iterating with foreach are thus supported out-of-the-box for associative arrays.&lt;br /&gt;
&lt;br /&gt;
One operation is perhaps a little too dynamic in nature, with unexpected side-effects. It is the &lt;a target=&#034;_blank&#034; href=&#034;http://php.net/extract&#034;&gt;extract()&lt;/a&gt; function.&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;The problem&lt;/h1&gt;
According to the documentation, the extract() function imports variables from an array into the current symbol table. In its simplest form, &lt;code&gt;extract(array(&amp;quot;a&amp;quot;=&amp;gt;3))&lt;/code&gt; will assign the value 3 to the variable $a.&lt;br /&gt;
&lt;br /&gt;
The problem here is that you need to know what keys the array holds, both when calling the function and maintaining it.&lt;br /&gt;
&lt;br /&gt;
Let us consider this simple function declaration:&lt;br /&gt;
&lt;pre&gt;function display_user_details($user) {&lt;br /&gt;  extract($user);&lt;br /&gt;&lt;br /&gt;  echo &#039;User name: &#039;.$user_name.&amp;quot;\n&amp;quot;;&lt;br /&gt;  echo &#039;User age: &#039;.$user_age.&amp;quot;\n&amp;quot;;&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;
Calling this function with the argument &lt;code&gt;array(&amp;quot;user_name&amp;quot; =&amp;gt; &amp;quot;Mike&amp;quot;, &amp;quot;user_age&amp;quot; =&amp;gt; 20)&lt;/code&gt; is a valid operation.&lt;br /&gt;
&lt;br /&gt;
But whenever you call this function, you need to check which key refers to which piece of the user&#039;s data.&lt;br /&gt;
&lt;br /&gt;
So even in its simplest form, the usage of extract() raises issues. Factor in that there are multiple ways of doing the extraction, based on combinations between the $extract_type and $prefix arguments:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;EXTR_OVERWRITE - if there is a collision, overwrite the existing variable&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;EXTR_SKIP - if there is a collision, don&#039;t overwrite the existing variable&lt;/li&gt;
    &lt;li&gt;EXTR_PREFIX_SAME - if there is a collision, prefix the variable name with $prefix&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;EXTR_PREFIX_ALL - prefix all variable names with $prefix&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;EXTR_PREFIX_INVALID - only prefix invalid/numeric variable names with $prefix&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;EXTR_IF_EXISTS - only overwrite the variable if it already exists in the current symbol table, otherwise do nothing&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;EXTR_PREFIX_IF_EXISTS - only create prefixed variable names if the non-prefixed version of the same variable exists in the current symbol table&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;EXTR_REFS - extracts variables as references&lt;/li&gt;
&lt;/ul&gt;
All this suggests that there are several options to choose from but the code becomes harder to understand. The more entries the array has, the more the extract() call does and harder to trace the data becomes.&lt;br /&gt;
&lt;h1&gt;The solution&lt;/h1&gt;
My suggestion is to keep it simple. There are at least three alternatives:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;work directly with the array: &lt;code&gt;$user[&amp;quot;user_name&amp;quot;]&lt;/code&gt; doesn&#039;t look that bad after all&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;explicitly &amp;quot;import&amp;quot; the variables: &lt;code&gt;$user_name = $user[&amp;quot;user_name&amp;quot;]&lt;/code&gt;&lt;br /&gt;
    &lt;/li&gt;
    &lt;li&gt;use function arguments with default values: &lt;code&gt;function display_user_details($user_name, $user_age = 18)&lt;/code&gt;&lt;br /&gt;
    &lt;/li&gt;
&lt;/ul&gt;
Depending on your specific needs, there might be other alternatives. But any of the above three will make your code easier to maintain and extend.
        </description>
      
      
    
  </item>
  
  <item rdf:about="http://www.numiton.com/blog/2008/07/04/1215194429366.html">
    <title>New whitepaper added</title>
    <link>http://www.numiton.com/blog/2008/07/04/1215194429366.html</link>
    
      
        <description>
          We&#039;re often asked about the quality of the translated code, from an architectural point of view. &lt;br /&gt;
So we have coded a small test application in PHP, translated it into Java using plain servlets and then Spring MVC, and we discuss the results in a new whitepaper: &lt;a href=&#034;http://www.numiton.com/products/whitepapers/19-whitepapers/88-exploring-ntile-ptoj-output-flavors.html&#034;&gt;Exploring nTile PtoJ output flavors&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
This is the abstract of the whitepaper:&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&amp;quot;The nTile PtoJ migrator is built in a modular fashion, allowing easy adjustments of the output. These adjustments can be as broad as the application architecture itself, or as narrow as varying a threshold for the package import (&amp;ldquo;import package.*&amp;rdquo;) statements.&lt;/em&gt;
&lt;p&gt;&lt;em&gt;The paper explores two major output architectures (called flavors from now on), plain servlets and Spring MVC. A comparative analysis highlights the differences as well as the common points between these two.&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;And this is the summary:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Introduction&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;Common considerations&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;The test application&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;Translation to servlets&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;Translation to Spring&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
    &lt;li&gt;Conclusions&lt;/li&gt;
&lt;/ul&gt;
        </description>
      
      
    
  </item>
  
  <item rdf:about="http://www.numiton.com/blog/2008/06/20/1213952553459.html">
    <title>PHP bad practice: variable reuse</title>
    <link>http://www.numiton.com/blog/2008/06/20/1213952553459.html</link>
    
      
        <description>
          &lt;p&gt;Anyone who has worked with PHP knows that it is extremely permissive with variables and their types. There&#039;s no need to declare variables, not even at class level, and data types cannot really be enforced. This is one of the greatest strengths of weakly typed dynamic languages, but it can be easily used the wrong way.&lt;/p&gt;
&lt;h1&gt;The problem&lt;/h1&gt;
&lt;p&gt;&amp;nbsp;Variable reuse is one such unfortunate case, which can easily lead to unmaintainable code.&lt;/p&gt;
&lt;p&gt;Let me illustrate by this example:&lt;/p&gt;
&lt;pre&gt;$fruit = array(1 =&amp;gt; &amp;ldquo;orange&amp;rdquo;, 2 =&amp;gt; &amp;ldquo;banana&amp;rdquo;);&lt;br /&gt;// Some code using the $item array&lt;br /&gt;$fruit = $fruit[2];&lt;/pre&gt;
&lt;p&gt;As you can see, the &lt;code&gt;$fruit&lt;/code&gt; variable starts out as an associative array and is used as such to perform various tasks. After a while, it gets reassigned and points to a string typed value inside the array.&lt;/p&gt;
&lt;p&gt;Now what if you need some other items from your fruit array in some newly added code? You will not be able to do that, since the array is no longer available. This is especially troublesome if the functionality is added by someone else. That developer will need to figure out what is going on, modify the existing code and only afterward implement the new feature.&lt;/p&gt;
&lt;p&gt;During our various migration projects, I have regularly encountered code that uses this annoying pattern. Such constructs are not yet explicitly handled by our type inference algorithm and so we end up with Java variables of &lt;code&gt;java.lang.Object&lt;/code&gt; type. This is obviously not a nice thing in the generated code.&lt;/p&gt;
&lt;p&gt;Not even larger PHP code base is free from variable reuse. From the open-source world, phpBB and WordPress come to my mind as heavy users of this construct.&lt;/p&gt;
&lt;h1&gt;The solution&lt;/h1&gt;
&lt;p&gt;Variables should be named consistently, based on the data they hold. While regarding variable types and their declarations the opinions are diverging (see static vs. dynamic typing), the meaning of the variable content is fundamental and should be reflected by its naming.&lt;/p&gt;
&lt;p&gt;With this in mind, the above code should rather look like this:&lt;/p&gt;
&lt;pre&gt;$fruits = array(1 =&amp;gt; &amp;ldquo;orange&amp;rdquo;, 2 =&amp;gt; &amp;ldquo;banana&amp;rdquo;);&lt;br /&gt;// Some code using the $item array&lt;br /&gt;$favourite_fruit = $fruits[2];&lt;/pre&gt;
&lt;p&gt;Now isn&#039;t this much cleaner and maintainable? Had I written this, it would make me sleep better at night :-)&lt;/p&gt;
        </description>
      
      
    
  </item>
  

</rdf:RDF>
