...

View Full Version : Is this good coding "ethics"?



cord
07-23-2012, 06:28 AM
Say I have something like


...
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?


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

Philip M
07-23-2012, 09:16 AM
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:



<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.

Arbitrator
07-23-2012, 12:07 PM
Would doing something like this be "valid" code?No; both of your examples are missing assignment operators.

Philip M
07-23-2012, 12:31 PM
No; both of your examples are missing assignment operators.
Ah, so they are!
The best way to declare a two-dimensional array is


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

DrDOS
07-23-2012, 02:55 PM
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.

Old Pedant
07-23-2012, 09:04 PM
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.)

DrDOS
07-23-2012, 09:13 PM
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.

felgall
07-23-2012, 10:54 PM
Ah, so they are!
The best way to declare a two-dimensional array is


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.


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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum