Skip to content

Javascript coding style for Brython scripts

Pierre Quentel edited this page Oct 29, 2023 · 2 revisions

Here are guidelines for the Javascript programs used by Brython. PRs that don't follow them will be accepted anyway :-)

  • indent with 4 spaces

  • no whitespace at the end of lines

  • don't end lines with ";" except in cases when it is mandatory

No:

    x = "a";
    y = "b";

Yes:

    x = "a"
    y = "b"
  • preferably don't put several statements on the same line separated by ";", insert a new line instead

No:

    x = "a"; y = "b";

Yes:

    x = "a"
    y = "b"
  • no space between if, for, while etc. and the parenthesis

No:

    if (condition)

Yes:

    if(condition)
  • no space between the closing parenthesis and the following curly brace

No:

    while (condition) {
        do_something
    }

Yes:

    while(condition){
        do_something
    }
  • for lines that start or end with a curly brace, no space after or before

No:

    } else {

Yes:

    }else{
  • if ";" is used, no whitespace before, one after

No:

    for(var i = 0;i < 10;i++)

Yes:

    for(var i = 0; i < 10; i++)
  • always use the curly braces for code blocks, don't use indentation

No:

    if(condition)
        do_something

Yes:

    if(condition){
        do_something
    }
  • short expressions can be on the same line if line length <= 79
    if(condition){do_something()}

In this case, no whitespace between the curly braces and the code inside.

  • one space before and after operators and the equal sign

No:

    if(x<1){y=0}

Yes:

    if(x < 1){y = 0}
  • one space after "," or ":", none before

No:

    t = {"a":1,"b":2}
    t = {"a" : 1 , "b" : 2}

Yes:

    t = {"a": 1, "b": 2}