Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save nrigaudiere/85602edc812c77d24c8fda21caae92e6 to your computer and use it in GitHub Desktop.

Select an option

Save nrigaudiere/85602edc812c77d24c8fda21caae92e6 to your computer and use it in GitHub Desktop.

Revisions

  1. @jrm2k6 jrm2k6 revised this gist Jun 25, 2014. 1 changed file with 8 additions and 4 deletions.
    12 changes: 8 additions & 4 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -76,7 +76,8 @@ Leave spaces inside the braces and after the colons in object literals. If it do
    // GOOD
    var point = { x: 100, y: 200 };

    var p2 = {
    var p2 =
    {
    x: 100,
    y: 200
    };
    @@ -106,9 +107,11 @@ function foo() {}
    You can declare a class variable directly from the constructor, take advantage of it. It avoids code repetition.

    ````typescript
    class Animal {
    class Animal
    {
    constructor(private name: string) { }
    move(meters: number) {
    move(meters: number)
    {
    alert(this.name + " moved " + meters + "m.");
    }
    }
    @@ -118,7 +121,8 @@ class Animal {
    When you want to bind ```this``` to your callback, you can use the [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/arrow_functions)

    ````javascript````
    function Person(){
    function Person()
    {
    this.age = 0;

    setInterval(() => {
  2. @jrm2k6 jrm2k6 revised this gist Jun 12, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -54,6 +54,7 @@ function foo(bar:number):String { ... }
    * always add types to method signatures
    * except for the void return type in classes (void return type is required in interfaces)
    * add types to class variables; it is required
    * try not to use the ```any``` type. You can always add a definition file from the excellent [Definitely Typed github repo]("https://github.com/borisyankov/DefinitelyTyped") if you want to have more specific type when using a third party library.

    ```
    // GOOD
  3. @jrm2k6 jrm2k6 revised this gist Jun 12, 2014. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -112,3 +112,18 @@ class Animal {
    }
    }
    ````

    ### arrow function
    When you want to bind ```this``` to your callback, you can use the [arrow function](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/arrow_functions)

    ````javascript````
    function Person(){
    this.age = 0;

    setInterval(() => {
    this.age++; // |this| properly refers to the person object
    }, 1000);
    }

    var p = new Person();
    ````
  4. @jrm2k6 jrm2k6 revised this gist Jun 12, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -55,8 +55,6 @@ function foo(bar:number):String { ... }
    * except for the void return type in classes (void return type is required in interfaces)
    * add types to class variables; it is required

    Even if the haxe compiler supports [type inference](http://haxe.org/ref/type_infer) we choose to add types to methods to improve code documentation.

    ```
    // GOOD
    public function setToRotation(radians:number)
  5. @jrm2k6 jrm2k6 revised this gist Jun 12, 2014. 1 changed file with 24 additions and 5 deletions.
    29 changes: 24 additions & 5 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -48,23 +48,23 @@ Don’t put spaces around the type defining colon.
    ```
    var _message:String;
    function foo(bar:number):string { ... }
    function foo(bar:number):String { ... }
    ```

    * always add types to method signatures
    * except for the void return type in classes (void return type is required in interfaces)
    * try to avoid the type ```any``` as much as possible.
    * you can download definitions files to include in your project from the [Definitely Typed github repo](https://github.com/borisyankov/DefinitelyTyped)
    * add types to class variables; it is required

    ```typescript```
    Even if the haxe compiler supports [type inference](http://haxe.org/ref/type_infer) we choose to add types to methods to improve code documentation.

    ```
    // GOOD
    public function setToRotation(radians:number)
    {
    var cos = Math.cos(radians);
    var sin = Math.sin(radians);
    ...
    }

    ```

    Method arguments should be typed.
    @@ -93,5 +93,24 @@ An empty function should be written in one line.
    function foo() {}
    ```

    ### variables
    * No need to prefix variables declaration with public keyword, it is the default visibility in Typescript.
    * All variables name must be written using the camelCase notation.

    ### private variables
    * Prefix all private variables with an underscore

    ### static variables
    * Must be written in capitals, separated by underscores if containing several words.

    ### class constructor
    You can declare a class variable directly from the constructor, take advantage of it. It avoids code repetition.

    ````typescript
    class Animal {
    constructor(private name: string) { }
    move(meters: number) {
    alert(this.name + " moved " + meters + "m.");
    }
    }
    ````
  6. @jrm2k6 jrm2k6 revised this gist Jun 12, 2014. 1 changed file with 4 additions and 20 deletions.
    24 changes: 4 additions & 20 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -48,16 +48,15 @@ Don’t put spaces around the type defining colon.
    ```
    var _message:String;
    function foo(bar:number):String { ... }
    function foo(bar:number):string { ... }
    ```

    * always add types to method signatures
    * except for the void return type in classes (void return type is required in interfaces)
    * add types to class variables; it is required
    * try to avoid the type ```any``` as much as possible.
    * you can download definitions files to include in your project from the [Definitely Typed github repo](https://github.com/borisyankov/DefinitelyTyped)

    Even if the haxe compiler supports [type inference](http://haxe.org/ref/type_infer) we choose to add types to methods to improve code documentation.

    ```
    ```typescript```
    // GOOD
    public function setToRotation(radians:number)
    {
    @@ -66,21 +65,6 @@ public function setToRotation(radians:number)
    ...
    }

    // BAD
    inline public function setToRotation(radians:Float):Void
    {
    ...
    }
    ```

    Void return type shouldn’t be typed.

    ```
    // BAD
    inline public function setToRotation(radians)
    {
    ...
    }
    ```
    Method arguments should be typed.
  7. @jrm2k6 jrm2k6 created this gist Jun 12, 2014.
    113 changes: 113 additions & 0 deletions gistfile1.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,113 @@
    ## Formatting

    ### spaces vs. tabs
    Use only tabs for indentation. Do not use spaces. Yes, spaces would be better :), it's for historical reasons.

    ### curly braces, control flow

    * Always put opening curly braces on new line.
    * Add a space after control flow keywords.
    * Use braces for single line ifs as well

    ```
    // BAD
    if(foo){
    // do something
    }
    // GOOD
    if (foo)
    {
    // do something
    }
    // BAD
    if (foo) bar;
    // GOOD
    if (foo)
    {
    bar;
    }
    ```

    ### operators
    Put spaces around the operator except unary operators.

    ```
    var i = 0;
    i++;
    i = i + 1;
    var s = "hello" + ", " + "world";
    ```

    ### typing
    Don’t put spaces around the type defining colon.

    ```
    var _message:String;
    function foo(bar:number):String { ... }
    ```

    * always add types to method signatures
    * except for the void return type in classes (void return type is required in interfaces)
    * add types to class variables; it is required

    Even if the haxe compiler supports [type inference](http://haxe.org/ref/type_infer) we choose to add types to methods to improve code documentation.

    ```
    // GOOD
    public function setToRotation(radians:number)
    {
    var cos = Math.cos(radians);
    var sin = Math.sin(radians);
    ...
    }
    // BAD
    inline public function setToRotation(radians:Float):Void
    {
    ...
    }
    ```

    Void return type shouldn’t be typed.

    ```
    // BAD
    inline public function setToRotation(radians)
    {
    ...
    }
    ```

    Method arguments should be typed.

    ### object literals

    Leave spaces inside the braces and after the colons in object literals. If it doesn't fit easily on one line, break it into one line per field.

    ```typescript
    // GOOD
    var point = { x: 100, y: 200 };

    var p2 = {
    x: 100,
    y: 200
    };

    // BAD
    var point = {x:100,y:200};
    ```

    ### Empty function
    An empty function should be written in one line.

    ```
    function foo() {}
    ```

    ### private variables
    * Prefix all private variables with an underscore