-
Notifications
You must be signed in to change notification settings - Fork 1
Coding and Style guide
So you've come up with a great exercise, but you're not sure how to get it to fit with the rest of the Khan Academy exercises. What should your code look like? Are there any tips and tricks to follow to make an exercise especially good?
For all of our code, we try to stick to a 80 character line limit.
For our Javascript, we tend to follow the jQuery style guide with a few exceptions:
- we use 4 space indents instead of tabs.
- we use $ rather than jQuery
- we do not use the liberal spacing with square brackets and parens as indicated in the style guide, except within object literals. That is, if-expressions, loop conditions, and functions should follow normal, tight wrapping:
For example, we do stuff like this:
if (condition) {
functionCall(bar, ["hi"], function() {
return 42;
});
return { color: "red" };
}
And not like this:
if ( condition ) {
functionCall( bar, [ "hi" ], function (){
return 42;
} );
return {color: "red"};
}
For our HTML, we try to use common sense, and make it look like the surrounding HTML. Try to not use custom styles as much as possible. You can check out a few of our exercises to see what it currently looks like.
We like to keep our codebase clean, so that things are easy to change and easy to understand. In general, don't try to do anything terribly fancy with your code. As a few overall principles, we try to:
Don't do something with 100 lines when it can be done in 10. This makes it immensely easier for us to test your code and make sure everything will work great.
Although <var>
tags are very useful because they allow you to execute useful code, try to keep that code to a minimum.
While it may be easier to code something up using a real javascript file, it clutters our codebase too much if the javascript can't be used in more than one context.
If possible, try to make your output fit into a form that can be used by our current utilities. For example, try using randRange
instead of making your own random function, or using toSentence
to turn a list into a string of sentence-like text.
If you think something you're making looks like something we're using already, try to go find it! Especially, check out our Utility Module Reference to check out the stuff we've done using utils already.
We like to use the functional programming style of underscore.js instead of an imperative style to simplify a lot of our code. For example, when iterating through a list we like to use
_.each(myList, function(e) {
// handle each element here
});
instead of
for (var i = 0; i < myList.length; i++) {
// handle each element here
}
In order to make sure that our exercises are correctly randomly generated, avoid using any randomly generating functions outside of the initial .vars
declarations.