I felt like I should blog at least once about making your code look good. Clean code is generally not a topic discussed much, as personal styles vary but I feel that we should at least mention some better practices to use.
1) Use comments.
This generally should go without saying but you would be so surprised how much code I go through everyday that has not been commented or documented. When you practice commenting your code you make it easier to go back and debug for anyone. If you are really that concerned about it adding the size of the file then clean all comments out in your final version and transfer the comments to some type of documentation. Let's review examples of comments in jQuery:
// Use these two slashes to comment out one line at a time
// Or we use /* */ to span multiple lines
/* Like
* This
*/
2) Use indentations:
Generally people prefer to use tab indents in their code. An example of this would:
var data = {
id: id,
name : name
}
However, you can do what I do and just use space indentation:
var moreData =
{ id: id,
name: name
}
Either way is acceptable, just stay consistent.
3) Use camel case or an underscore:
Or something to make the code readable and identifiable. I know several people that will simple use:
var variable1 = 0;
var variable2 = 0; ... so forth and so on.
A better way to do it is naming the variable something related to what it is doing or storing.
var dayOfTheMonth = 'Monday';
var name_of_my_teacher = 'Judy Watson';
These are just some simple techniques that will save you and others some time by keeping the code clean and easier to understand.
No comments:
Post a Comment