<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users1277</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/1277" rel="self"/>
  <title>Ants</title>
  <updated>Thu Feb 09 03:29:57 -0800 2012</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor617212</id>
    <published>2012-02-09T03:29:57-08:00</published>
    <title>[Ruby] On Please improve</title>
    <content type="html">&lt;p&gt;I figured out why you were sorting brand_names to give preference for matching to longer strings. That is why I left that sorting in place there.&lt;/p&gt;

&lt;p&gt;What I couldn't figure out was why you were sorting all_brands by inverse id length. Everything found in all_brands gets put into brand_names anyway, and brand_names gets sorted by inverse length anyway. Is there something magic about Ruby's sorting algorithm that it runs faster if an array is already partially sorted? (I wouldn't be surprised if the answer is yes since I only recently learned that Ruby is slow when using recursion.)&lt;/p&gt;

&lt;p&gt;Be very careful about downcasing (and/or upcasing), and then using it for matching. See the issue about the Turkish-I:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/deeptanshuv/archive/2004/09/04/225720.aspx" target="_blank"&gt;http://blogs.msdn.com/b/deeptanshuv/archive/2004/09/04/225720.aspx&lt;/a&gt;
&lt;br /&gt;&lt;a href="http://www.i18nguy.com/unicode/turkish-i18n.html" target="_blank"&gt;http://www.i18nguy.com/unicode/turkish-i18n.html&lt;/a&gt;
&lt;br /&gt;&lt;a href="http://stackoverflow.com/questions/1910573/ruby-1-9-how-to-properly-upcase-downcase-multibyte-strings" target="_blank"&gt;http://stackoverflow.com/questions/1910573/ruby-1-9-how-to-properly-upcase-downcase-multibyte-strings&lt;/a&gt;
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1997-please-improve/refactors/617212" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor617207</id>
    <published>2012-02-09T03:02:44-08:00</published>
    <title>[C] On Simple Particle Engine for a shooter game</title>
    <content type="html">

&lt;pre&gt;const int MAX_PARTICLES = 16;
const int MAX_SLOTS = 16;
const int MAX_PARTICLE_SPEED = 3;
const int PARTICLE_FRAMES = 120;

enum State {
    Inactive,
    Active,
};

typedef struct {
    int xPos;
    int yPos;
} Point;

typedef struct {
    int xV;
    int yV;
} Speed;

typedef struct {
    Point position;
    Speed speed;
    DWORD color;
} Particle;

typedef struct {
    State isActive;
    Point origin;
    int frame;
    int numParticles;
    Particle particles[MAX_PARTICLES];
} Slot;

Slot g_slots[MAX_SLOTS] = { 0 }

void initSlot(Slot * slot, int x, int y) {
    Particle * particle = slot-&amp;gt;particles;

    slot-&amp;gt;isActive = Active;
    slot-&amp;gt;origin.xPos = x;
    slot-&amp;gt;origin.yPos = y;
    slot-&amp;gt;frame = 0;
    slot-&amp;gt;numParticles = dbRND(MAX_PARTICLES) + 1;

    for(int i = slot-&amp;gt;numParticles; i &amp;gt; 0; --i, ++particle) {
        particle-&amp;gt;position = slot-&amp;gt;origin;
        particle-&amp;gt;speed.xV = myRND(MAX_PARTICLE_SPEED);
        particle-&amp;gt;speed.yV = myRND(MAX_PARTICLE_SPEED);
    }
}

void startParticles(int x, int y) {
    Slot * slot = g_slots;

    for(int i = 0; i &amp;lt; MAX_SLOTS; ++i, ++slot) {
        if(!slot-&amp;gt;isActive) {
            initSlot(slot, x, y);
            break;
        }
    }
}

void drawSlot(Slot * slot) {
    Particle * particle = slot-&amp;gt;particles;

    if(slot-&amp;gt;frame &amp;gt;= PARTICLE_FRAMES) {
        slot-&amp;gt;isActive = Inactive;
        return;
    }

    slot-&amp;gt;frame++;

    for(int i = slot-&amp;gt;numParticles; i &amp;gt; 0; --i, ++particle) {
        randomInk();
        dbCircle(particle-&amp;gt;position.xPos, particle-&amp;gt;position.yPos, 3);
        defaultInk();
        particle-&amp;gt;position.xPos += particle-&amp;gt;speed.xV;
        particle-&amp;gt;position.yPos += particle-&amp;gt;speed.yV;
    }
}

void drawParticles() {
    Slot * slot = g_slots;

    for(int i = 0; i &amp;lt; MAX_SLOTS; ++i, ++slot) {
        if(slot-&amp;gt;isActive)
            drawSlot(slot);
    }
}
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1999-simple-particle-engine-for-a-shooter-game/refactors/617207" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor617109</id>
    <published>2012-02-08T17:16:54-08:00</published>
    <title>[C] On Snake / Nibbles clone in C and Ncurses</title>
    <content type="html">&lt;p&gt;Faster moveSnake()...&lt;/p&gt;

&lt;pre&gt;struct coords g_vectors[4] =
{
    {  0, -1 }, // UP
    { -1,  0 }, // LEFT
    {  0,  1 }, // DOWN
    {  1,  0 }  // RIGHT
};

void moveSnake(struct snake* player, int direction)
{
    struct coords * head = &amp;amp;player-&amp;gt;segment[0];
    struct coords * last = &amp;amp;player-&amp;gt;segment[player-&amp;gt;length];
    struct coords * penultimate = last - 1;

    while(last &amp;gt; head)
        *last-- = *penultimate--;
    head-&amp;gt;x += g_vectors[direction].x;
    head-&amp;gt;y += g_vectors[direction].y;
}&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1998-snake-nibbles-clone-in-c-and-ncurses/refactors/617109" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor617040</id>
    <published>2012-02-08T11:26:08-08:00</published>
    <title>[C] On Snake / Nibbles clone in C and Ncurses</title>
    <content type="html">&lt;p&gt;Faster drawObjects()...&lt;/p&gt;

&lt;pre&gt;inline void draw(struct coords * coord, char symbol)
{
    mvaddch(coord-&amp;gt;y, coord-&amp;gt;x, symbol);
}

void drawObjects(struct snake* player, struct coords* fruit)
{
/*      Draws dynamic objects (snake and fruit) on screen.
 */

/***    SNAKE   ***/
    struct coords * segment = players-&amp;gt;segment;
    draw(segment, 'S');
    ++segment;
    int count = player-&amp;gt;length;
    for(int i = 1; i &amp;lt; count; ++i, ++segment)
        draw(segment, 'O');

/***    FRUIT   ***/
    draw(fruit, 'F');
}&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1998-snake-nibbles-clone-in-c-and-ncurses/refactors/617040" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor617036</id>
    <published>2012-02-08T11:06:00-08:00</published>
    <title>[C] On Snake / Nibbles clone in C and Ncurses</title>
    <content type="html">&lt;p&gt;Better moveSnake()...&lt;/p&gt;

&lt;pre&gt;struct coords g_vectors[4] =
{
    {  0, -1 }, // UP
    { -1,  0 }, // LEFT
    {  0,  1 }, // DOWN
    {  1,  0 }  // RIGHT
};

void moveSnake(struct snake* player, int direction)
{
    int j;
    for(j = player-&amp;gt;length; j &amp;gt; 0; --j)
        player-&amp;gt;segment[j] = player-&amp;gt;segment[j - 1];

    player-&amp;gt;segment[0].x += g_vectors[direction].x;
    player-&amp;gt;segment[0].y += g_vectors[direction].y;
}&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1998-snake-nibbles-clone-in-c-and-ncurses/refactors/617036" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor617034</id>
    <published>2012-02-08T10:56:00-08:00</published>
    <title>[C] On Snake / Nibbles clone in C and Ncurses</title>
    <content type="html">&lt;p&gt;Better getInput()...&lt;/p&gt;

&lt;pre&gt;int getInput(int direction)
{
/*      Pressing keys will buffer the input. getch() will take the character in the 
 *      buffer and place it in keyTemp. Because nodelay() is true, if no key is 
 *      pressed buffer will be NULL. If buffer is NULL or input is not one of the 
 *      macros in the switch, or player attempts to move the snake in the opposite 
 *      direction of travel (back into itself), snake continues in current direction.
 */

        switch(getch())
        {
        case KEY_UP:
            return direction == DOWN  ? direction : UP;
        case KEY_RIGHT:
            return direction == LEFT  ? direction : RIGHT;
        case KEY_DOWN:
            return direction == UP    ? direction : DOWN;
        case KEY_LEFT:
            return direction == RIGHT ? direction : LEFT;
        }

        return direction;
}&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1998-snake-nibbles-clone-in-c-and-ncurses/refactors/617034" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor616932</id>
    <published>2012-02-08T03:36:06-08:00</published>
    <title>[C] On Snake / Nibbles clone in C and Ncurses</title>
    <content type="html">&lt;p&gt;Instead of the magic numbers 0 through 3 used in getInput() and moveSnake(), I would have used #define to declare them.&lt;/p&gt;

&lt;pre&gt;#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1998-snake-nibbles-clone-in-c-and-ncurses/refactors/616932" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor616924</id>
    <published>2012-02-08T02:51:02-08:00</published>
    <title>[Ruby] On Please improve</title>
    <content type="html">&lt;p&gt;Are you missing an 'end' for the loop where you are building the brand_names array?&lt;/p&gt;

&lt;p&gt;Why does it matter if all_brands array is sorted or not?&lt;/p&gt;

&lt;p&gt;Why are you using forcing the brand_name to lower case in the regular expression if you are going to do a case insensitive match anyway? Also building the regular expression can be delayed until it really is needed.&lt;/p&gt;

&lt;p&gt;In the code below, I inverted the inner and outer loops so that other loop iterates over line items, and then only for line items without a brand, then try to find a matching brand. I also got rid of the sorting of all_brands.&lt;/p&gt;

&lt;pre&gt;@all_brands = Brand.find(:all).map(&amp;amp;:name) 
@all_brands.each do |brand|
  brand_names = [brand.name] + brand.brand_synonims.map(&amp;amp;:name)
end
brand_names.sort_by{|x| (-1)*x.length}

@line_items = LineItem.find(:all)
@line_items.each do |li|
  next if li.brand != 'undefined_brand'
  brand_names.each do |brand_name|
    r = Regexp.new(brand_name.gsub(&amp;quot; &amp;quot;,&amp;quot;\ &amp;quot;).gsub(&amp;quot;-&amp;quot;,&amp;quot;\-&amp;quot;).gsub(&amp;quot;.&amp;quot;,&amp;quot;\.&amp;quot;), true)        
    if (!li.model.blank? &amp;amp;&amp;amp; li.model =~ r) || (!li.name.blank? &amp;amp;&amp;amp; li.name =~ r)
      li.brand = brand.name
      li.save
      break
    end
  end        
end
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1997-please-improve/refactors/616924" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor616224</id>
    <published>2012-02-05T20:21:32-08:00</published>
    <title>[PHP] On Parsing of XML data has high CPU usage</title>
    <content type="html">&lt;p&gt;Why not use XMLReader instead?&lt;/p&gt;

&lt;p&gt;The following is untested, but hopefully you get the idea;&lt;/p&gt;

&lt;pre&gt;-------
#shplaying.php file

&amp;lt;?php
// URL to your Shoutcast server, including port
// don't put in the http:// part!  do it like you see here
// also, reverse DNS lookup needs to be on, or this probably won't work
$server = &amp;quot;SERVERADDRESS&amp;quot;;

// Admin password for your Shoutcast server
$password = &amp;quot;PASSWORD&amp;quot;;



$mysession = curl_init();
curl_setopt($mysession, CURLOPT_URL, &amp;quot;http://$server/admin.cgi?sid=1&amp;amp;mode=viewxml&amp;quot;);
curl_setopt($mysession, CURLOPT_HEADER, false);
curl_setopt($mysession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($mysession, CURLOPT_POST, false);
curl_setopt($mysession, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($mysession, CURLOPT_USERPWD, &amp;quot;admin:$password&amp;quot;);
curl_setopt($mysession, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($mysession, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
$xml = curl_exec($mysession);
curl_close($mysession);

$songs = &amp;quot;&amp;quot;;
$xmlreader = new XMLReader();
$xmlreader-&amp;gt;xml($xml);
while ($xmlreader-&amp;gt;next('SHOUTCASTSERVER')) {
    while ($xmlreader-&amp;gt;next('SONGHISTORY')) {
        while ($xmlreader-&amp;gt;next('SONG')) {
            while ($xmlreader-&amp;gt;next('TITLE') {
                $data = $xmlreader-&amp;gt;readString();
                if (strpos($data, &amp;quot; 0:&amp;quot;) === false) {
                    $songs .= &amp;quot;\n&amp;quot; . $data;
                }
            }
        }
    }
}

?&amp;gt;

-----------

THIS IS THE SCRIPT THAT IN AN HTML FILE THAT INTERACTS WITH THE PHP


&amp;lt;script src=&amp;quot;jquery-1.2.6.pack.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;script&amp;gt;

$(document).ready(function() {

   $('div').css(&amp;quot;display&amp;quot;, &amp;quot;inline&amp;quot;);
   $('tr:even').addClass('greybar1');
   $('tr:odd').addClass('greybar2');
   pollstation();
   setInterval(pollstation, 15000);

});

function pollstation() {

	$.ajax( {
    		url: 'playing.php?id='+Math.random(),
    		type: 'GET',
    		dataType: 'html',
    		success: function(stationdata) {
			var lines = stationdata.split('\n'); 
                        var my_string = lines[2];
                        if(my_string.length &amp;gt; 34)
	                   my_string = my_string.substring(0, 34)+&amp;quot;...&amp;quot;;
                        
			$('#currentsong').html(my_string);
			for (var i = 1; i &amp;lt;= 1; i++) {
                             my_string = lines[i+2];
                             if(my_string.length &amp;gt; 34)
            	                       my_string = my_string.substring(0, 34)+&amp;quot;...&amp;quot;;
 			     $('#prevsong' + i).html(my_string);
                        }
    		}
	} );
}

&amp;lt;/script&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1996-parsing-of-xml-data-has-high-cpu-usage/refactors/616224" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor614908</id>
    <published>2012-01-31T05:07:16-08:00</published>
    <title>[Ruby] On clean the code</title>
    <content type="html">&lt;p&gt;@Nirmit, I like your refactoring, but there's the issue of what if today's date is less than START_DATE ? The original code would handle that condition, but your code doesn't.&lt;/p&gt;

&lt;p&gt;Admittedly, since we are now past START_DATE so the issue shouldn't be a problem now, but if the same code is recycled for next year and changed to 2013-01-15, and the code is run on 2013-01-01?
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1989-clean-the-code/refactors/614908" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor612690</id>
    <published>2012-01-25T22:07:48-08:00</published>
    <title>[JavaScript] On Learning JS</title>
    <content type="html">&lt;p&gt;- Changed data structures to simulate an associative array in JavaScript.
&lt;br /&gt;- Separated concerns: translateWords() just translates or remaps the words and traductor() takes care of input and output management.
&lt;br /&gt;- Made translateWords() take the dictionary to be used as a parameter so that the function can be reused.&lt;/p&gt;

&lt;p&gt;There is still room for more improvement in the code below. For example, I'm not happy with the way undefined words are always quoted the Spanish way. What happens when it's English to Spanish?
&lt;/p&gt;

&lt;pre&gt;var spanishToEnglish = {
    &amp;quot;arbol&amp;quot;   : &amp;quot;tree&amp;quot;,
    &amp;quot;casa&amp;quot;    : &amp;quot;house&amp;quot;,
    &amp;quot;el&amp;quot;      : &amp;quot;the&amp;quot;,
    &amp;quot;la&amp;quot;      : &amp;quot;the&amp;quot;,
    &amp;quot;quintas&amp;quot; : &amp;quot;crack&amp;quot;,
    &amp;quot;es&amp;quot;      : &amp;quot;is&amp;quot;,
    &amp;quot;verde&amp;quot;   : &amp;quot;green&amp;quot;,
    &amp;quot;y&amp;quot;       : &amp;quot;and&amp;quot;,
    &amp;quot;roja&amp;quot;    : &amp;quot;red&amp;quot;
};

function translateWords(dict, words)
{
    var values = [];
    for(i = 0; i &amp;lt; words.length; i++)
    {
        var value = dict[words[i]];
        if (value == undefined)
            value = &amp;quot;&#191;&amp;quot; + words[i] + &amp;quot;?&amp;quot;;
        values.push(value);
    }
    return values;
}

function traductor()
{
    var phrase = prompt(&amp;quot;Introduce tu frase:&amp;quot;);
    var output = translateWords(spanishToEnglish, phrase.split(&amp;quot; &amp;quot;));
    for(i = 0; i &amp;lt; output.length; i++)
        console.log(output[i]);
}

traductor();
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1986-learning-js/refactors/612690" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor612254</id>
    <published>2012-01-24T20:46:10-08:00</published>
    <title>[Ruby] On Euler Problem 2 solution</title>
    <content type="html">&lt;p&gt;And getting rid of the ugly corner case check for max &amp;lt; 2, and using the multi-assignment...&lt;/p&gt;

&lt;pre&gt;def sum_even_fib_numbers_below max
    prev = 1
    fib = 1
    sum = 0
    while fib &amp;lt;= max
        sum += fib if fib.even?
        prev, fib = fib, prev + fib
    end
    sum
end

print sum_even_fib_numbers_below 4000000&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1984-euler-problem-2-solution/refactors/612254" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor612247</id>
    <published>2012-01-24T20:30:38-08:00</published>
    <title>[Ruby] On Euler Problem 2 solution</title>
    <content type="html">&lt;p&gt;Sorry, the point was to get the sum of all the even Fibonacci numbers...&lt;/p&gt;

&lt;pre&gt;def sum_even_fib_numbers_below max
    return 0 if max &amp;lt; 2
    first = 1
    second = 2
    fib = first + second
    sum = 2
    while fib &amp;lt;= max
        sum += fib if fib.even?
        first = second
        second = fib
        fib = first + second
    end
    sum
end

print sum_even_fib_numbers_below 4000000&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1984-euler-problem-2-solution/refactors/612247" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor612245</id>
    <published>2012-01-24T20:20:25-08:00</published>
    <title>[Ruby] On Euler Problem 2 solution</title>
    <content type="html">&lt;p&gt;Your code is already nicely written with loose coupling and tight cohesion. For general purpose code, or code that will be maintained over a long time, you want to keep things this way. You've nicely separated the concerns, and made the code very readable as to what is happening through its phases of operation.&lt;/p&gt;

&lt;p&gt;Unfortunately, for the Euler project problems. Often the general purpose solutions are the slowest. Code has to be written precisely to solve that problem, and quite often that problem only. By taking advantage of the characteristics of the problem, one gets insights and can optimize the algorithm/code. So one can then refactor the code to be optimal, but not always necessarily more readable. (Personally, for code that I'll be expected to maintain over the long run, I'll always pick readable over fast, but that's just my preference.)&lt;/p&gt;

&lt;p&gt;So to that end of refactoring to go faster, notice how your code loops over the array multiple times. The first time is to actually generate the contents. The second to reject the odd numbers, and the last to sum up the numbers. Since all that matters is just getting the sum at the end of the run, you can forego of maintaining the array, and just keeping a running sum of all the odd numbers.
&lt;/p&gt;

&lt;pre&gt;def sum_odd_fib_numbers_below max
    first = 1
    second = 2
    fib = first + second
    sum = 1
    while fib &amp;lt;= max
        sum += fib if fib.odd?
        first = second
        second = fib
        fib = first + second
    end
    sum
end

print sum_odd_fib_numbers_below 4000000
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1984-euler-problem-2-solution/refactors/612245" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor610678</id>
    <published>2012-01-19T03:27:30-08:00</published>
    <title>[Java] On first BST</title>
    <content type="html">&lt;p&gt;Let's try this again, but in the code field where whitespace maybe significant:&lt;/p&gt;

&lt;pre&gt;I think there is a bug in the remove method.

Given a tree:

     5 
   /   \ 
  3     8 
 / \   / \ 
1   4 6   9 
 \     \ 
  2     7


Attempting to delete 5, I see that the in order successor will be 6. So we end up with the following tree:

    6 
   / \ 
  3   7 
 / \ 
1   4 
 \ 
  2

8 and 9 just got orphaned.&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1979-first-bst/refactors/610678" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor610666</id>
    <published>2012-01-19T02:26:01-08:00</published>
    <title>[Java] On first BST</title>
    <content type="html">&lt;p&gt;I think there is a bug in the remove method.&lt;/p&gt;

&lt;p&gt;Given a tree:&lt;/p&gt;

&lt;p&gt;     5
&lt;br /&gt;   /   \
&lt;br /&gt;  3     8
&lt;br /&gt; / \   / \
&lt;br /&gt;1   4 6   9
&lt;br /&gt; \     \
&lt;br /&gt;  2     7&lt;/p&gt;

&lt;p&gt;     
&lt;br /&gt;Attempting to delete 5, I see that the in order successor will be 6. So we end up with the following tree:&lt;/p&gt;

&lt;p&gt;     6
&lt;br /&gt;   /   \
&lt;br /&gt;  3     7
&lt;br /&gt; / \
&lt;br /&gt;1   4
&lt;br /&gt; \
&lt;br /&gt;  2&lt;/p&gt;

&lt;p&gt;8 and 9 just got orphaned.
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1979-first-bst/refactors/610666" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor610208</id>
    <published>2012-01-17T17:44:25-08:00</published>
    <title>[PHP] On php refactoring</title>
    <content type="html">&lt;p&gt;Yeah, sorry about the code not working. I was just hacking on the keyboard and did not actually setup a test harness to try things out. Hopefully it gave you some ideas.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1978-php-refactoring/refactors/610208" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor610131</id>
    <published>2012-01-17T11:50:54-08:00</published>
    <title>[PHP] On php refactoring</title>
    <content type="html">&lt;p&gt;I had some mismatched tags, duplicate parallel code, and some view logic still intertwined with the behavior. This should fix the mismatched tags, get rid of the parallel duplication, do a bit more separation of concerns by blocks.
&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
    $email = strtolower(mysql_real_escape_string($_REQUEST['email']));
    if ($email &amp;amp;&amp;amp; filter_var($email, FILTER_VALIDATE_EMAIL)):
        $getlink = mysql_query(&amp;quot;SELECT SUM(invites) from table WHERE email='$email'&amp;quot;);
        if($row = mysql_fetch_assoc($getlink)):
            $totalinvites = $row['SUM(invites)'] + 1;
            $query = &amp;quot;UPDATE table SET invites=invites+1 WHERE email='$email'&amp;quot;;
            $message = &amp;quot;You&amp;amp;rsquo;re back!&amp;lt;br /&amp;gt;You have a total of $totalinvites invites.&amp;quot;;
        else:
            $hashed = md5($email);
            $ip = $_SERVER['REMOTE_ADDR'];
            $query = &amp;quot;INSERT INTO table &amp;quot; .
                     &amp;quot;(email, hash, invites, ip, signup_time, signup_date, signup_jointime) VALUES &amp;quot; .
                     &amp;quot;('$email', '$hashed', 0, '$ip', CURTIME(), NOW(), NOW())&amp;quot;;
            $message = &amp;quot;No invites, yet.&amp;quot;;
        endif;
        mysql_query($query) or die('error');
?&amp;gt;
        &amp;lt;p&amp;gt;
          &amp;lt;span&amp;gt;
            &amp;lt;?php echo $message; ?&amp;gt;
          &amp;lt;/span&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;
          &amp;lt;span&amp;gt;
            The more friends you invite, the sooner you'll get access!&amp;lt;br /&amp;gt;
            Copy/paste this URL below into Twitter, Facebook, or an email:
          &amp;lt;/span&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;
          Share Link
          &amp;lt;input class=&amp;quot;referral&amp;quot;
                 type=&amp;quot;text&amp;quot;
                 size=&amp;quot;27&amp;quot;
                 readonly=&amp;quot;1&amp;quot;
                 onClick=&amp;quot;select();&amp;quot;
                 value=&amp;quot;&amp;lt;?php echo $sitedomain . $hashed; ?&amp;gt;&amp;quot; /&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;
          Or use the buttons:&amp;lt;br /&amp;gt;
        &amp;lt;/p&amp;gt;
&amp;lt;?php else: ?&amp;gt;
        &amp;lt;p class=&amp;quot;invalid_email&amp;quot;&amp;gt;
          Please enter a valid e-mail address
        &amp;lt;/p&amp;gt;
        &amp;lt;ul&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;span&amp;gt;
              &amp;lt;a href=&amp;quot;&amp;lt;?php echo htmlspecialchars($_SERVER['HTTP_REFERER']) ?&amp;gt;&amp;quot;&amp;gt;go back&amp;lt;/a&amp;gt;
            &amp;lt;/span&amp;gt;
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
&amp;lt;?php endif; ?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1978-php-refactoring/refactors/610131" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor610009</id>
    <published>2012-01-17T01:38:38-08:00</published>
    <title>[PHP] On php refactoring</title>
    <content type="html">&lt;p&gt;This isn't a true refactoring since refactoring shouldn't change behavior including any existing bugs. This is how I envision the code working. I'm making a big assumption that behavior is that the invite count gets bumped up based on the email address being seen again.&lt;/p&gt;

&lt;p&gt;BTW, your use of $hashed picking up only the first 4 characters of the MD5 hash is very susceptible to collisions because MD5 computes 128 bits, but you are only using the first 16 bits.&lt;/p&gt;

&lt;p&gt;Code below is untested but hopefully it gets you going:&lt;/p&gt;

&lt;pre&gt;&amp;lt;?php
    $email = strtolower(mysql_real_escape_string($_REQUEST['email']));
    if ($email &amp;amp;&amp;amp; filter_var($email, FILTER_VALIDATE_EMAIL)):
        $getlink = mysql_query(&amp;quot;SELECT SUM(invites) from table WHERE email='$email'&amp;quot;);
        if($row = mysql_fetch_assoc($getlink)):
            $totalinvites = $row['SUM(invites)'] + 1;
            mysql_query(&amp;quot;UPDATE table SET invites=invites+1 WHERE email='$email'&amp;quot;) or die('error!');
            echo &amp;quot;&amp;lt;p&amp;gt;&amp;lt;span&amp;gt;Your back!&amp;lt;br&amp;gt;You have a total of &amp;quot;.$totalinvites.&amp;quot; invites.&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&amp;quot;;
        else:
            $hashed = md5($email);
            $ip = $_SERVER['REMOTE_ADDR'];
            mysql_query(&amp;quot;INSERT IGNORE INTO table &amp;quot; .
                        &amp;quot;(email, hash, invites, ip, signup_time, signup_date, signup_jointime) VALUES &amp;quot; .
                        &amp;quot;('$email', '$hashed', '0', '$ip', CURTIME(), NOW(), NOW())&amp;quot;) or die('error');
            echo &amp;quot;&amp;lt;p&amp;gt;No invites, yet.&amp;lt;/p&amp;gt;&amp;quot;;
        endif;
?&amp;gt;
        &amp;lt;p&amp;gt;
          &amp;lt;span&amp;gt;
            The more friends you invite, the sooner you'll get access!&amp;lt;br /&amp;gt;
            Copy/paste this URL below into Twitter, Facebook, or an email:
          &amp;lt;/span&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;
          Share Link
          &amp;lt;input class=&amp;quot;referral&amp;quot;
                 type=&amp;quot;text&amp;quot;
                 size=&amp;quot;27&amp;quot;
                 readonly=&amp;quot;1&amp;quot;
                 onClick=&amp;quot;select();&amp;quot;
                 value=&amp;quot;&amp;lt;?php echo $sitedomain.$hashed; ?&amp;gt;&amp;quot; /&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;p&amp;gt;
          Or use the buttons:&amp;lt;br /&amp;gt;
        &amp;lt;/p&amp;gt;
&amp;lt;?php else: ?&amp;gt;
        &amp;lt;p class=&amp;quot;invalid_email&amp;quot;&amp;gt;
          Please enter a valid e-mail address&amp;lt;/p&amp;gt;
        &amp;lt;ul&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;span&amp;gt;&amp;lt;a href=&amp;quot;&amp;lt;?php echo htmlspecialchars($_SERVER['HTTP_REFERER']) ?&amp;gt;&amp;quot;&amp;gt;go back&amp;lt;/span&amp;gt;
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
&amp;lt;?php endif; ?&amp;gt;
&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1978-php-refactoring/refactors/610009" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor609693</id>
    <published>2012-01-16T01:53:07-08:00</published>
    <title>[PHP] On php refactoring</title>
    <content type="html">&lt;p&gt;I'm sorry if this sounds harsh, but I'm at a loss to understand how can you say that &amp;quot;everything works&amp;quot;?&lt;/p&gt;

&lt;p&gt;Issues:
&lt;br /&gt;1) You are outputting incorrect HTML. You don't have opening or closing &amp;lt;html&amp;gt;, or &amp;lt;body&amp;gt; elements. You have mismatched &amp;lt;li&amp;gt; elements. You are mixing XHTML and HTML.
&lt;br /&gt;2) On your initial insert, you are inserting the hash of the email address in the hash column, yet later you are attempting to bump up the invite count by matching the hash column against the URI. How does the invite count ever get bumped up?
&lt;br /&gt;3) Your code is susceptible to SQL injection. For example you pick up $_REQUEST['uri'] without any escaping. I recommend using SQL prepare and bind operations to help insulate yourself from SQL injection attacks.
&lt;br /&gt;4) Your while loop to comptue totalinvites doesn't sum up the values from the rows with a matching hash. Even if it did, it would have been more efficient to use the SQL SUM() operator rather than doing the looping yourself.
&lt;br /&gt;5) It doesn't look like you are opening the SQL database connection.
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1978-php-refactoring/refactors/609693" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor609387</id>
    <published>2012-01-14T23:36:38-08:00</published>
    <title>[JavaScript] On Multiple jQuery Sliders &amp; OpenLayers setOpacity methods</title>
    <content type="html">&lt;p&gt;Untested, but I hope you get the idea...&lt;/p&gt;

&lt;pre&gt;$(document).ready(function() {
    var init = [ 20, 20, 20, 16, 16, 8 ];
    var hii = [ hii_1, hii_2, hii_3, hii_3, hii_4, hii_5, hii_6 ];
    var sliders = $(&amp;quot;#sliders .slider&amp;quot;);
    var availableTotal = 100;

    sliders.each(function(index, element) {
        $(element).slider({
            range: &amp;quot;min&amp;quot;,
            min: 0,
            value: init[index],
            slide: function(event, ui) {
                var total = ui.value;
                sliders.not(this).each(function() {
                    total += $(this).slider(&amp;quot;option&amp;quot;, &amp;quot;value&amp;quot;);
                });
                if (total &amp;gt; availableTotal)
                    return false;
                hii[index].setOpacity(ui.value / 100);
                $(this).siblings().text(ui.value);
            }
        });
    });
});&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1977-multiple-jquery-sliders-openlayers-setopacity-methods/refactors/609387" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor608703</id>
    <published>2012-01-12T11:57:55-08:00</published>
    <title>[Ruby] On String Incrementing</title>
    <content type="html">&lt;p&gt;@rochefort: Good, but unfortunately, as was revealed by @caustic-interface, one of the other things that the function is supposed to do is convert &amp;quot;z&amp;quot; to &amp;quot;aa&amp;quot;.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1959-string-incrementing/refactors/608703" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor608377</id>
    <published>2012-01-11T10:40:30-08:00</published>
    <title>[Ruby] On Looping over a file</title>
    <content type="html">&lt;p&gt;I think it's because your second 'end' ended the loop block. I think you meant to use 'ensure', rather than 'end' at that point.&lt;/p&gt;

&lt;pre&gt;When /^I request test urls with headers &amp;quot;([^&amp;quot;]*)&amp;quot; &amp;quot;([^&amp;quot;]*)&amp;quot;$/ do |accept, headers|
    header_map = create_header_map_from_string(headers)
    test_urls = load_fixture(&amp;quot;test-urls1.txt&amp;quot;)
    test_urls.each_line do |line|
        begin
            response = get_with_custom_header(line, accept, header_map)
        rescue Exception =&amp;gt; e
            if !e.respond_to?('response')
                throw e
            end
            response = e.response
        ensure
            puts &amp;quot;got http #{response.code} for #{line}&amp;quot;
        end
end&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1976-looping-over-a-file/refactors/608377" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor608204</id>
    <published>2012-01-10T22:10:25-08:00</published>
    <title>[PHP] On Templating Engine</title>
    <content type="html">&lt;p&gt;LOL! Talk about optimized output from that templating engine... It always prints out 'v'. :-)
&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1975-templating-engine/refactors/608204" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor608135</id>
    <published>2012-01-10T14:52:03-08:00</published>
    <title>[C] On Test</title>
    <content type="html">

&lt;pre&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main()
{
    int value = 0;
    printf(&amp;quot;%d\n&amp;quot;, value);
    return EXIT_SUCCESS;
}&lt;/pre&gt;</content>
    <author>
      <name>Ants</name>
      <email>code.herder@recombulator.net</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/1974-test/refactors/608135" rel="alternate"/>
  </entry>
</feed>

