Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested methods, best practice

I'm currently fiddling around with jQuery's ajax method to get some data from a JSON feed and animating some bars according to the data.

As I'm hacking away, I don't really know if my approach is right.

$.ajax({

    url: "/echo/json/",
    type: "POST",
    data: data,
    success: function(data){


    $("#value1-bar .usage-bar-fill").animate({
        width: data.Value1
        }, 2500, function() {
        // Animation complete.
    });

    $("#value2-bar .usage-bar-fill").animate({
        width: data.Value2
        }, 2500, function() {
        // Animation complete.
    });

    },
    dataType: "json"
});

Is it OK to animate the divs from inside the ajax method, or is there a more correct way to do this - formatting wise, memory wise etc.?

Is it better to some how return some variables from the ajax method and executing the animation method from outside the ajax method?

Check out jsfiddle example here: http://jsfiddle.net/timkl/KPvCj/

I know this is a pretty broad question, but being a graphic designer come self-taught web designer - I never really know if what I'm doing is good practice.

like image 764
timkl Avatar asked Jan 05 '12 13:01

timkl


People also ask

Is it good practice to use nested functions?

no, there's nothing wrong with that at all, and in js, it's usually a good thing. the inside functions may not be a pure function, if they rely on closure variables. If you don't need a closure or don't need to worry about polluting your namespace, write it as a sibling.

How many number of nested procedures are allowed?

The max nesting level is 32 Nesting Stored Procedures.

What is a nested method?

What is a nested method. If a method is defined inside another method, the inner method is said to be nested inside the outer method or it is called Nested method. All languages do not support nesting a method inside another method but python allows you to do so.

What is the point of a nested function?

Nested (or inner, nested) functions are functions that we define inside other functions to directly access the variables and names defined in the enclosing function. Nested functions have many uses, primarily for creating closures and decorators.


2 Answers

I like this question, and I'd love to see some more detailed feedback, but I'd say it looks good. You're already calling an embedded success function within the ajax method, so worrying about memory / performance of further embedded functions seems almost negligible...

If you're going to want to animate your usage-bar-fills some more with the same / similar animation you used within your success function, then I might create separate functions and call those instead...but I'm sure you knew that already ;)

Nice question!

like image 111
Chris Kempen Avatar answered Sep 24 '22 01:09

Chris Kempen


This is nearly a question of taste, since this seems to be a very small application. When your applications grow, overall design and principles becomes a more crucial part of the process.

But that said, I don't like this approach. It is very prone to produce code that repeats itself unnecessarily - unless these are strictly one-off situations that are very specific to this use case, but most of the time you can generalize to enable code reuse.

Personally, I would declare the callback functions elsewhere, probably on an object which already knows what it should be working with (i.e. your selector) and knows what to do with it. This way, the code almost structures itself, keeping only the very locally relevant bits where they are needed.

In this specific case, it would be something along the lines of a graph object, which when set a value updates its own display/view appropriately.

Hopefully my ramblings make sense. I might be a tad too tired to give out opinions and expert advice, so I'll just stop here.

like image 28
nikc.org Avatar answered Sep 23 '22 01:09

nikc.org