Showing posts with label dev. Show all posts
Showing posts with label dev. Show all posts

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: 

Thursday, March 22, 2012

String replace the better way.

This last week I have been working on making my team project at work run better on ie 7.  Usually that would mean that I spent all my time fixing styles, but there are better people then me at that.  So for me the project was getting rid of the scrpt is running two long errors.  The app I work on is very JavaScript heavy.  One of the worst offending functions I had found was the following:

function clean(word) {
        if (word) {
            
            return word.toLowerCase().
                replace(/\s/gi, "").                
                replace(/[àáâãäå]/gi, "a").
                replace(/æ/gi, "ae").
                replace(/ç/gi, "c").
                replace(/[èéêë]/gi, "e").
                replace(/[ìíîï]/gi, "i").
                replace(/ñ/gi, "n").
                replace(/[òóôõö]/gi, "o").
                replace(/œ/gi, "oe").
                replace(/[ùúûü]/gi, "u").
                replace(/[ýÿ]/gi, "y").
                replace(/\W/gi, "");
        } else {
            return word;
        }
    }

Which is a very brute force way of cleaning up strings for searching.  It was the most expensive call in our system.  With a little work it was changed to the following. 

var removeAccents = (function () {
        var translateReg = /[àáâãäåæçèéêëìíîïñòóôõöœùúûüýÿ]/g;
        var translate = {
            "à""a""á""a""â""a""ã""a""ä""a",
            "å""a""æ""ae""ç""ç""è""e""é""e",
            "ê""e""ë""e""ì""i""í""i""î""i",
            "ï""i""ñ""n""ò""o""ó""o""ô""o",
            "õ""o""ö""o""œ""oe""ù""u""ú""u",
            "û""u""ü""u""ý""y""ÿ""y"
        };
        return function(s) {
            return (s.replace(translateReg, function(match) {
                return translate[match];
            }));
        };
    })();
 
    function clean(word) {
        if (word) {
            return removeAccents(word.toLowerCase().replace(/\s/gi, "").replace(/\W/gi, ""));
        } else {
            return word;
        }
    }

For a massive boost in performance.




Thursday, August 4, 2011

My Rules for Development

  1. If you think you need a region you really need a new class.
  2. If you are using a for loop in c# there is something wrong with your code.
  3. If you are using a foreach fix you code so you don't need it.
  4. If you think you want to use exceptions to control flow stop now and go to your room and think about what damage you may do.
  5. If you don't write a unit test it's not done.