Programming Style–Kendo & jQuery

  • Which of the following do you prefer? shutterstock_76533307
  • Which is easier to read?
  • Which is easier to write?
  • Which is easier to maintain?

 

 var dataItem = this.target().closest(".k-grid").data("kendoGrid").dataSource.getByUid(this.target().closest("tr").data("uid"));

Or…

var dataItem = this.target()
                .closest(".k-grid")
                .data("kendoGrid")
                .dataSource
                .getByUid(this.target().closest("tr").data("uid"));

Or…

 var grid = this.target().closest(".k-grid").data("kendoGrid");
 var dataSrc = grid.dataSource;
 var dataItem = dataSrc.getByUid(this.target().closest("tr").data("uid"));

I’m not convinced the answers are obvious.

Share

About Jesse Liberty

Jesse Liberty is a Master Consultant for Falafel Software, and has three decades of experience writing and delivering software projects. He is the author of 2 dozen books and multiple Pluralsight courses, and has been a Technical Evangelist for Telerik and for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS.
This entry was posted in JavaScript, Kendo, Languages, Opinion, Programming and tagged . Bookmark the permalink.

4 Responses to Programming Style–Kendo & jQuery

  1. Vinny Brown says:

    I’m not a Kendo user, but along the lines of what Mark Rendle suggested, It looks like we need to do something like:

    var target = this.target();
    if(target){
    return $(target).getDataItem();
    }else{ return null; }

    where the specifics of how to do that would be encapsulated in getDataItem() and have a lot more null-reference checking than we have in the sample :)

  2. Mark Rendle says:

    They’re all pretty bad.

    I’d be looking to wrap it all in a jQuery extension/plug-in so I could just call:

    this.closestKendoDataItem();

  3. Leo says:

    To me, 2 is the easier to read. To read 3 you have to keep in memory the lines to fully understand what is happening.
    And since we are talking about javascript, the less code, the better (I know this would eventually be minified but we still save some bytes with 1 or 2).

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>