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.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
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).

Comments are closed.