DevTools/CodingStandards: Difference between revisions

Jump to navigation Jump to search
Added Async Code section
(Added guidelines about comments)
(Added Async Code section)
Line 123: Line 123:
* A global comment at the very top of a file explaining what the file is about and the major types/classes/functions it contains is a good idea for quickly browsing code.
* A global comment at the very top of a file explaining what the file is about and the major types/classes/functions it contains is a good idea for quickly browsing code.
* If you are forced to employ some kind of hack in your code, and there's no way around it, then add a comment that explains the hack and why it is needed. The reviewer is going to ask for one anyway.
* If you are forced to employ some kind of hack in your code, and there's no way around it, then add a comment that explains the hack and why it is needed. The reviewer is going to ask for one anyway.
== Asynchronous Code ==
A lot of code in DevTools is asynchronous, because a lot of it relies on connecting to the DevTools debugger server and getting information from there in an asynchronous fashion.
It's easy to make mistakes with asynchronous code, so here are a few guidelines that should help:
* Prefer [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise promises] over callbacks.
* Use the <code>new Promise(() => {})</code> syntax.
* Don't forget to catch rejections by defining a rejection handler: <code>promise.then(() => console.log("resolved"), () => console.log("rejected"));</code> or <code>promise.catch(() => console.log("rejected"));</code>.
* Make use of [https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Task.jsm <code>Tasks</code> and generator functions] to make asynchronous code look synchronous.


== Advice to patch authors ==
== Advice to patch authors ==
130

edits

Navigation menu