4
\$\begingroup\$

How can I format this code so it's more readable?

$(".card").click(function(){
        $(this).stop().animate({ 
            width:'0px', 
            marginLeft: margin+'px', 
            opacity: 0.5}, 
            500, 
            function(){
               $(this).siblings('.card').animate({
                   width: width + 'px',
                   marginLeft:'0px',
                   opacity:'1'},500);
            });         
});
\$\endgroup\$
1
  • 1
    \$\begingroup\$ I just read it.... \$\endgroup\$
    – Naftali
    Commented Aug 13, 2012 at 19:07

4 Answers 4

4
\$\begingroup\$

This is a very subjective question

I am a fan of separating things so each is identifiable and easily maintained in the future. However some will argue this does lead to more code, but more readable code in my opinion.

var animateCallback = function() {
    var props = {
        width: width + 'px',
        marginLeft: '0px',
        opacity: '1'
    };
    $(this).siblings('.card').animate(props, 500);
};
var clickHandler = function() {
    var props = {
        width: '0px',
        marginLeft: margin + 'px',
        opacity: 0.5
    };
    $(this).stop().animate(props, 500, animateCallback);
};
$(".card").click(clickHandler);
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Robert C. Martin and Martin Fowler are on your side (Clean Code by Robert C. Martin, G19: Use Explanatory Variables; Refactoring: Improving the Design of Existing Code by Martin Fowler, Introduce Explaining Variable) \$\endgroup\$
    – palacsint
    Commented Aug 13, 2012 at 19:36
  • \$\begingroup\$ I would assume your audience knows JQ and name your handler something more descriptive of the behavior initiated than 'clickHandler.' The faster you get at the general purpose of a thing the less people have to look when they don't need more details. (just making that point here since mine's TLDR as always : P) \$\endgroup\$ Commented Aug 13, 2012 at 20:32
0
\$\begingroup\$

This would be my preference:

$(".card")
    .click(function(){
            $(this)
                .stop()
                .animate({
                        width:'0px', 
                        marginLeft: margin+'px', 
                        opacity: 0.5
                    }, 
                    500, 
                    function(){
                        $(this)
                            .siblings('.card')
                            .animate({
                                    width: width + 'px',
                                    marginLeft:'0px',
                                    opacity:'1'
                                }, 500);
                    });
        });
\$\endgroup\$
4
  • 1
    \$\begingroup\$ ahhhh, after reading that my legs hurt. staircase code is not fun to read \$\endgroup\$
    – rlemon
    Commented Aug 13, 2012 at 19:41
  • \$\begingroup\$ I think it's the best you can do if the only thing you changed was the styling (white-spaces) of the code provided originally. Unless, you have something better, but in the end isn't it a matter of preference? \$\endgroup\$
    – Sam
    Commented Aug 13, 2012 at 19:44
  • \$\begingroup\$ yes, and my preference is to not have my code looking like a water slide :P no offence, I think you will find a lot of developers agree here. \$\endgroup\$
    – rlemon
    Commented Aug 13, 2012 at 19:55
  • 1
    \$\begingroup\$ Water slides! Sounds like fun. :D \$\endgroup\$
    – Sam
    Commented Aug 13, 2012 at 20:00
0
\$\begingroup\$

This is really very open ended and is open to many forms of interpretation. The code format below is strictly the way I like to make my code legible.

$( ".card" ).click( function()
{
    $( this ).stop()
             .animate(
                 { 
                     width:'0px', 
                     marginLeft: margin+'px', 
                     opacity: 0.5
                 }, 500,

             function()
             {
                 $(this).siblings('.card')
                        .animate(
                            {
                                width: width + 'px',
                                marginLeft:'0px',
                                opacity:'1'
                            },500);
    });         
});

Only functions parenthesis start on the same vertical. Objects and arrays are always indented 4 spaces beyond the function.

\$\endgroup\$
0
\$\begingroup\$

My principle is to start general and get more specific. A good label on something is the fastest way to provide information. Name your functions for what they do and your properties/vars for what they store.

I like my function/object-constructor internals to answer three questions in order:

  1. What's local to this scope? (vars and a list of funcs defined later)
  2. What's the general action of this thing (basic method calls that lead to output/changes)
  3. How does the internally defined stuff work (hoisted function definitions last)

The merits of function hoisting for legibility are of course debatable but I prefer general stuff first, details later to trying to learn the general action of a thing while swimming in a sea of internal function definitions. Note: function hoisting only works with function <label>() definitions, not when you assign anonymous functions to vars as in var someFunc = function(){}.

On the jquery front, I would use the .animate(options, settings) object arguments approach since object literal labels can add a lot more clarity (for instance to what 500 means) but I'm sticking with what you have here in case there's a JQ version issue. Also it's easy to miss methods in chaining. It's okay to use line-breaks between '.' operators and we often do where I work to make all method calls clear.

$('.card').click(animateCards); // name well and intent is clear sooner

function animateCards(){

    //locals(1)
    var optionsObject = {
        //<everything in the first animate arg object>
    }

    //actual comment I would put in my code typically follows:
    //functions defined at bottom:
    //animateCallback - list so we can see the local funcs


    //main (2)

    $(this) //breaking chaining into lines like this can be helpful
    .stop()
    .animate(optionsObject,500,animateCallBack);


    //internal functions (3)

    function animateCallback(){
        //<everything in animate's callback arg>
    }

}//end animateCards - always indicate ending brackets of longer functions
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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