Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2012
    Posts
    36
    Thanks
    9
    Thanked 1 Time in 1 Post

    Is this good coding "ethics"?

    Say I have something like

    Code:
    ...
    var example {
    
    "example1" : 1,
    "example2" : 2,
    "example3" : 3,
    "example4" : 4,
    "example5" : 5
    
    };
    ...
    Would doing something like this be "valid" code? (I want to optimize my scripts for the web.) Are there also some tips you guys know of to optimizing scripts? Like, for example, should I get rid of whitespace, etc?

    Code:
    ...
    var example {"example1" : 1,"example2" : 2,"example3" : 3,"example4" : 4,"example5" : 5};
    ...

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,983
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    There is no difference between the two.

    Minification (Google for more info) is the practice of removing unnecessary characters from code to reduce its size thereby improving load times. When code is minified all comments are removed, as well as unneeded white space characters (space, newline, and tab). In the case of JavaScript, this in theory improves response time performance because the size of the downloaded file is reduced. In reality usually no appreciable advantage is gained. These days it is pointless to try to save a few bytes - clarity of code is far more important than compression. Better to optimize your images.

    A simple trick is to replace repeated calls to document.getElementById by a local variable, saving 24 bytes per call. Example:

    Code:
    <div id = "mydiv"> Some text in here </div>
    
    function $(id) {return document.getElementById(id) }
    $("mydiv").style.backgroundColor = "red";
    You should reduce the number of HTTP requests by combining all scripts into a single script, and similarly combining all CSS into a single stylesheet. And naturally making sure you do not load duplicate scripts! Load your CSS in your <head> tag above your body. Load your JavaScript just before you close your </body> tag.


    But at the end of the day the value of a website depends on its impact on the user - not on how cleverly it was coded. Within reason download times (at least on broadband) are not very relevant unless your web application is very complex.

    In other words the priority should be to focus your efforts on the customer, not on the product.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 07-23-2012 at 09:38 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Senior Coder Arbitrator's Avatar
    Join Date
    Mar 2006
    Location
    Splendora, Texas, United States of America
    Posts
    3,302
    Thanks
    28
    Thanked 276 Times in 270 Posts
    Quote Originally Posted by cord View Post
    Would doing something like this be "valid" code?
    No; both of your examples are missing assignment operators.
    For every complex problem, there is an answer that is clear, simple, and wrong.

  • Users who have thanked Arbitrator for this post:

    cord (07-23-2012)

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,983
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    Quote Originally Posted by Arbitrator View Post
    No; both of your examples are missing assignment operators.
    Ah, so they are!
    The best way to declare a two-dimensional array is

    Code:
    var items = [["example1",1],["example2",2],["example3",3],["example4",4]];
    alert (items[2][0]);

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    cord (07-23-2012)

  • #5
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts

    But at the end of the day the value of a website depends on its impact on the user - not on how cleverly it was coded. Within reason download times (at least on broadband) are not very relevant unless your web application is very complex.

    In other words the priority should be to focus your efforts on the customer, not on the product.


    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    This has to be the best description I've ever read of the proper focus for a website coder. Just think, if you're writing code that will be used by a million people and you save 5 seconds off the time of execution, multiply that 5 X 1,000,000 and figure how much user time you've saved. If you've made that webpage that much more intuitive, think of how many more people will be able to use and understand it.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,438
    Thanks
    75
    Thanked 4,372 Times in 4,337 Posts
    Except I doubt seriously that *ANYTHING* you could do in JavaScript would impact a page by 5 seconds. 0.5 seconds is even a lot. 0.05 seconds (50 milliseconds) is probably more like the max impact that JS would have.

    IMAGES (and video and Flash, etc., but especially images) are almost always the biggest culprit when it comes to page load times. How many people are too lazy to use Photoshop or something similar to resize their images to exactly what is needed on the page and to choose the right JPEG (or other) compression to make the image file sizes no larger than are needed? I'd bet the 30% of the entire internet bandwidth, or more, is taken up by improperly sized image files. (Remember, it's not just the number of pixels; it's also the compression ratio chosen.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,974
    Thanks
    15
    Thanked 229 Times in 229 Posts
    Quote Originally Posted by Old Pedant View Post
    Except I doubt seriously that *ANYTHING* you could do in JavaScript would impact a page by 5 seconds. 0.5 seconds is even a lot. 0.05 seconds (50 milliseconds) is probably more like the max impact that JS would have.

    IMAGES (and video and Flash, etc., but especially images) are almost always the biggest culprit when it comes to page load times. How many people are too lazy to use Photoshop or something similar to resize their images to exactly what is needed on the page and to choose the right JPEG (or other) compression to make the image file sizes no larger than are needed? I'd bet the 30% of the entire internet bandwidth, or more, is taken up by improperly sized image files. (Remember, it's not just the number of pixels; it's also the compression ratio chosen.)
    And server requests. I was on a page today that must have spent nearly a minute on one server request. Put your library files on your own server so there won't be a half dozen server requests to load one page.

  • #8
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,639
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Philip M View Post
    Ah, so they are!
    The best way to declare a two-dimensional array is

    Code:
    var items = [["example1",1],["example2",2],["example3",3],["example4",4]];
    alert (items[2][0]);
    Except that they were not defining an array but were defining properties for an object - just using array notation rather than object notation.

    Quote Originally Posted by Old Pedant View Post
    Except I doubt seriously that *ANYTHING* you could do in JavaScript would impact a page by 5 seconds.

    When I have run tests to see which variant of JavaScript code is faster I usually set up to run somewhere between 100,000 and 1,000,000 iterations of the code - that way the time difference will show up as a measurable fraction of a second. I have only ever done this in attempting to prove that the version of the code that I consider to be easier to read is either effectively as fast or much faster than the not so easy to read alternative that I have seen a lot of people use (for example using the table properties and methods rather than the generic alternatives).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •