Tuesday, February 25, 2014

Reverse Proxy with url rewrite.

I have been working on a project where I needed two two web servers to act like they wore on the same domain.  In production this will probably be done with a reverse proxy like hapoxy, but for my dev box I need something that was windows friendly and that I could setup with out much effort for any of my team.

Turns out that node can do this and it's very easy to do. In just 12 lines of my own code I had a reverse proxy that would do url rewriting. Check it out.





Monday, December 16, 2013

Fun In google drive

This is a useful little function for google docs spread sheet

=TO_DATE(TO_PURE_NUMBER(A2) +TO_PURE_NUMBER("0:10:00"))

Yes you can use it to give your self a spread sheet with a new time every 10 min with out having to write it out for every line.

Tuesday, January 22, 2013

Functional programming in javascript


Over the last several months I have been working on an article that talks about why you should stop using for loops and use map, each, and reduce to manipulate your data.  They increase the readability of code especially if you name the functions being applied to the collection of data.  I have never finished this article I get caught up in the details and it never flows the way I want it to. 

Today I did find some articles that explain why I prefer functional programming ides to using a traditional for loop,  these articles come form the the developers at Salsita Software.

Check them out: 

Friday, January 11, 2013

Taking it out is better


As developers we spend a lot of time writing code.  Most of this is adding new features to a product, the rest is spent changing a product to use a new API.  Seldom is it to go into a system and clean up all the relics lying around in the code base.

When you switch to the new shiny API you seldom remove the old code files that talked to the old API because you may need to switch back to the old API in a real hurry.  You comment out the code or put in a conditional statement thinking that you will come back and remove them later on when you aren't so busy. This is all well and good except for one thing you are always busy. 

Over time these code relics pile up and pollute a code base.  Remember the developer that put in the "if statement" on line 12 of foo.cs?  Well he took a new job and now no one knows why that relic exists. The knowledge left with that developers and now we have a liability.  Most developers are willing to let sleeping dogs lie.  If everything is working don't make changes to the code because change means bugs and bugs mean more work.


Not cleaning up code over time causes more work.  You may have unit tests that were written against those old logic branches that are now invalid and out of date.  They could be a red herring in trying to figure out real problems in the code base.  The worst part about this is that it can be prevented with just a little work when you are changing the code.

Take the time to remove the code that is no longer needed.  If you need to roll back to the old way of doing things you use your source control to get the code back.  Take the time to prune useless unit tests.  If a test is not meaningful either change it so it is or get rid of it.  Just by doing these simple things code can be made so much easier to maintain and update over time. 

Saturday, January 5, 2013

deep copy in javascript

One of the things about javascript I don't like is that there is not a simple way to copy an object literal so you don't have side effects when making updates to an object. this script show how I got around this. There is alos a fiddle as well which can be found here

Thursday, November 15, 2012

little bit of javascirpt fun (also know as why this sucks)

A couple weeks ago I was working with a couple developers who didn't understand why I hated prototype in javasciprt.  It's not that I don't think it has uses it's just that if you are using prototype you are probably doing it wrong.  I created this little fiddle to show them where the problems start to happen.

http://jsfiddle.net/phlik/Uv3Sg/

Tuesday, September 18, 2012

Javascript string replacing

I spend a lot of time working in JavaScript.  I also find that I spend a lot of time replacing text in strings in this language.  Most of the time this can be done easily by using a good templating system. When you are working with localization systems, you can't rely on templating.

My first attempt at string replace was:
var stamp = function(template, tag, value){
    return template.replace('{'+ tag + '}', value);
};

This was a very simple attempt and wasn't very good.  It has a lot of problems. Top of the list is it will not get every instance of the tag in the string. You end up having to call stamp several times to replace the same thing.  So I set out to fix this problem.  I found you could use regular expressions to do this kind of thing.  I built out this simple function to do the work.
var stampValues = function (template, replacements) {
    for (var tag in replacements) {
        var reg = new RegExp('\{' + tag + '\}', 'g');
        template = template.replace(reg, replacements[tag]);
    }
    return template;
};

It's much better than the above one because it gets all the tags in the string. It's actually much better than any other code I have tried but more on that later.

The next version looked like this.  It uses underscore to do the flow control because each is better than "for" -- right?
var stampValuesEx0 = function (template, replacements) {
    _(replacements).each(function (value, tag) {
        var reg = new RegExp('\{' + tag + '\}', 'g');
        template = template.replace(reg, value);
    });
    return template;
};

It's not. It's slower.  Not noticeably slower but when you put a timer to it the stampValues is faster.

After looking at this I realized I was doing the Regular expression incorrectly. I could change the way I did it and probably get more speed out of it.  So I built this . . .
var stampValuesEx2 = function (template, replacements) {
    var setValues = (function () {
        var translateReg = /{\w+}/g;
        var cleanup = /[{}]/g;
        return function (s) {
            return (s.replace(translateReg, function (match) {
                return replacements[match.replace(cleanup, '')];
            }));
        };
    })();
    return setValues(template);
}

And It SUCKS.  It's slow; it takes double the time of the stampValues function.  There are a few things you can do to speed it up.  You can build out a closure with the regular expressions already in them this actually helped a lot. It cuts about 1/3 of the execution time.  That version looks like this:
var stampValuesEx3 = (function () {
    var translateReg = /{\w+}/g;
    var cleanup = /[{}]/g;
    return function (template, replacements) {
        return (template.replace(translateReg, function (match) {
            return replacements[match.replace(cleanup, '')];
        }));
    }
})();
It still not as fast as stampValues but it is faster than other attempts I have made.

In short I ended up using stampValues for my string replace function.

The code I used to test all these functions can be found at here take a look. If you have examples that you think are better or faster I would be interested in seeing them.