Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm working on a JavaScript plugin that makes font-sizes flexible (for a responsive layout). The code works well so far, but I'm sure that it's capable of improvement (especially if you look at the global variable).

(function () {
    var extend = function (obj, ext) {
        for (var key in ext) {
            if (ext.hasOwnProperty(key)) {
                obj[key] = ext[key];
            }
        }
        return obj;
    };

    window.txt = function (el, k, c) {
        var s = extend({
            n: Number.NEGATIVE_INFINITY,
            x: Number.POSITIVE_INFINITY
        }, c);

        var fit = function () {
            var a = k;

            var i = function () {
                el.style.fontSize = Math.max(Math.min(el.clientWidth / a, parseFloat(s.x)), parseFloat(s.n)) + "px";
            };
            i();
            window.addEventListener("resize", i);
            window.addEventListener("orientationchange", i);
        };

        if (el.length) {
            for (var i = 0; i < el.length; i++) {
                fit(el[i]);
            }
        } else {
            fit(el);
        }

        return el;
    };
})();

DEMO

Meaning of the used variables:

  • n = minFontSize
  • x = maxFontSize
  • s = settings
  • c = config
  • el = element
  • i → running the function
  • a or k = fontRatio/compressor
share|improve this question
2  
What you may and may not do after receiving answers. I've rolled back Rev 3 → 2. –  200_success 16 hours ago
    
@Malachi's advice can stand on its own. –  200_success 16 hours ago

1 Answer 1

something that I would definitely do on the next iteration, before this code gets too big, is to make the variables meaningful. I can't take a quick look at this and tell you that I know what it is doing or how to plug into it.

JavaScript is an older browser language that, for a long time, required using as little code as possible to achieve it's goal, this is not the case anymore.

in a huge JavaScript file you can use minify to make your code more compact and take out all the extra spaces. But please don't skimp on the variable names, it makes it hard for others to see exactly what your code is doing.

share|improve this answer
    
Thanks for the good advice. I changed that. –  Maximilian Fuchs 16 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.