##Your Codecode is not finished.
###First of.
###3 rules of coding.:
Now back to the code.
- Only capitalise variables if they are functions that are intended to be instantiated via the new token. note the syntax highlighting.
Only capitalise variables if they are functions that are intended to be instantiated via the new token. Note the syntax highlighting.
function MyObj(){} // good var obj = new MyObj(); function myObj(){} // bad var obj = new myObj(); function doSomething(){} // good function DoSomething(){} // bad var myData = []; // good var MyData = []; // bad.
function MyObj(){} // good
var obj = new MyObj();
function myObj(){} // bad
var obj = new myObj();
function doSomething(){} // good
function DoSomething(){} // bad
var myData = []; // good
var MyData = []; // bad.
- Don't take short cuts when it comes to syntax, especial concerning curly braces when defining blocks, it is a major source of syntax related bugs that are very hard to find when you are staring at thousands of lines of code.
Don't take shortcuts when it comes to syntax, especial concerning curly braces when defining blocks, it is a major source of syntax related bugs that are very hard to find when you are staring at thousands of lines of code.
for(i = 0; i < 10; i+= 1) doSomethif(); // bad for(i = 0; i < 10; i+= 1) doSomethif(); // better but still bad for(i = 0; i < 10; i+= 1) { doSomethif(); } // OK for(i = 0; i < 10; i+= 1) { // Good doSomethif(); }
for(i = 0; i < 10; i+= 1)
doSomethif(); // bad
for(i = 0; i < 10; i+= 1) doSomethif(); // better but still bad
for(i = 0; i < 10; i+= 1) { doSomethif(); } // OK
for(i = 0; i < 10; i+= 1) { // Good
doSomethif();
}
- Layout. DonDon't spread out initialization code throughout the source. You are making calls to
clear
andradixSort
intermixed within the source. Ok maybe for a short bit of code but when your code gets over a few pages it can be very hard to see what is going on.
Naming. Use clear descriptive words or phrases for the functions. You have
Clear
?. Clear what, mayberesetBuckets
would be a better name for the function.Use ES6. There is no reason not to use Javascript latest and best incarnation. Using ES5 because it has more browsers that support it is only shooting your self in the foot. Time moves along and ES6 will soon be ES7. As a programmer you can not afford to not learn the latest and best, by the time you know it back to front, it will be obsolete.
Use
const
for any variables the will not change. (not a must, but it makes you look more professional) The general accepted reasoning between the use of constantsconst
, block scopelet
, and function scopevar
variables is mostly about reducing bugs by protecting state. You cant mess up the state of the code if it is a protected 'const' or inaccessible (out of scope), which makes sense, well kind of, if you feel that you need the training wheels down that is.
Example of clean names-spacenamespace code.:
// pass to the function data
function radixSort(numArray){
// define required functions within the function
function Clear() {
for (var i = 0; i < 10; i++)
buckets[i] = [];
}
// declare inside the function
var length = numArray.length;
var buckets = new Array(10);
// return the processed information
return sorted
// Or leave in place but still return the array
return numArray
}
// pass to the function data
function radixSort(numArray){
// define required functions within the function
function Clear() {
for (var i = 0; i < 10; i++)
buckets[i] = [];
}
// declare inside the function
var length = numArray.length;
var buckets = new Array(10);
// return the processed information
return sorted
// Or leave in place but still return the array
return numArray
}
- Don't use
new Array()
andnew Object()
when creating objects and arrays, they are not quite the same as using literals[]
for array and{}
for object. The same applies to strings (if youareyou're ever tempted) don't use= new String("my string");
use= "my string";
So always keep that in mind when coding in any managed environment.
Example:
// bad
function clear() {
for (var i = 0; i < 10; i++) {
buckets[i] = []; // dereferences the array held in [i] and creates a brand new one
}
}
// good
function clear() {
for (var i = 0; i < 10; i++) {
buckets[i].length = 0; // reduces the array size but does not create a new one
}
}
// great by hard work
// first array item is the current number of usable items in the array
function clear() {
for (var i = 0; i < 11; i++) {
buckets[i][0] = 0; // totally clean nothing created, nothing deleted, and very fast
// but you can no longer use bucket[i].length.
}
}
// bad
function clear() {
for (var i = 0; i < 10; i++) {
buckets[i] = []; // dereferences the array held in [i] and creates a brand new one
}
}
// good
function clear() {
for (var i = 0; i < 10; i++) {
buckets[i].length = 0; // reduces the array size but does not create a new one
}
}
// great by hard work
// first array item is the current number of usable items in the array
function clear() {
for (var i = 0; i < 11; i++) {
buckets[i][0] = 0; // totally clean nothing created, nothing deleted, and very fast
// but you can no longer use bucket[i].length.
}
}
Don't repeat code. You have the function
copyToArray
and at the bottom ofradixSort
the very same code, done twice. If you want to change something you have to do it twice, and if it is a long bit of code, you may forget one.Don't repeat calculations. You have:
// Bad for (var index = 0; index < length; index++) { var tenIndex = Math.pow(10, count); // recalculating the same number for each array item. Bad //... blah blah } count++ // Good var tenIndex = Math.pow(10, count); // only once for (var index = 0; index < length; index++) { //... blah blah } count++
// Bad
for (var index = 0; index < length; index++) {
var tenIndex = Math.pow(10, count); // recalculating the same number for each array item. Bad
//... blah blah
}
count++
// Good
var tenIndex = Math.pow(10, count); // only once
for (var index = 0; index < length; index++) {
//... blah blah
}
count++
- Use the correct function appropriately.
parseInt
is for parsing integer values into Javascript's floating point numbers. It IS NOT how you remove the fractional part of a number. UseMath.floor
,Math.ceil
orMath.round
. Some may suggest using bitwise operators to trim of the fractionvar num = myNum | 0;
Don't unless you understand the difference between adouble
and asigned int 32
and why that will make the the test runs fail.Use the correct function appropriately.
parseInt
is for parsing integer values into Javascript's floating point numbers. It IS NOT how you remove the fractional part of a number. UseMath.floor
,Math.ceil
orMath.round
. Some may suggest using bitwise operators to trim of the fractionvar num = myNum | 0;
Don't unless you understand the difference between adouble
and asigned int 32
and why that will make the the test runs fail.
Note that I expect that the numbers are correctly formatted as unsigned integers and no exponent notation (ege.g. 127e10).
Thus the rewrite.: