Javascript Code Standards: Difference between revisions

From PM Wiki
Jump to navigation Jump to search
No edit summary
 
(20 intermediate revisions by the same user not shown)
Line 1: Line 1:
= Naming Conventions =
= Naming Conventions =


== Local Variables ==
All variables, functions, and classes should all have meaningful names:


Local variable names should all be lower case: <pre>email</pre>
<pre>
// do not do this
var f = "Aaron";
 
// do this
var first_name = "Aaron";
</pre>
 
== Variables ==
 
Variable names should all be lower case:  
 
<pre>
var email;
</pre>
 
If the variable name is made up of multiple words, use snake case:
 
<pre>
var email_address;
</pre>
 
== Strings ==
 
Strings should be enclosed in double quotes ("):
 
<pre>
var user_name = "Aaron";
</pre>


If the local variable name is made up of multiple words, use snake case: <pre>email_address</pre>
=== String Concatenation vs. Variable Injection ===
 
String concatenation is discouraged. Instead, enclose your strings that require variable injections in backticks (`) and place the variable using ${} notation:
 
<pre>
// do not do this:
var message = user_name + " has logged out of the system.";
 
// do this:
var message = `${user_name} has logged out of the system.`;
</pre>


== Functions ==
== Functions ==


Function names should all be lowercase: <pre>update</pre>
Function names should all be lowercase:  
 
<pre>
function update();
</pre>


If the function name is made up of multiple words, use snake case: <pre>update_by_id</pre>
If the function name is made up of multiple words, use snake case:  
 
<pre>
function update_by_id();
</pre>


== Classes ==
== Classes ==


Class names should all be capitalized: <pre>Vertical</pre>
Class names should all be capitalized:  
 
<pre>
class Vertical {


If the class name is made up of multiple words, use Pascal case: <pre>VerticalGroup</pre>
    constructor() {
 
        // code goes here
    }
}
</pre>
 
If the class name is made up of multiple words, use Pascal case:  
 
<pre>
class VerticalGroup {
 
    constructor() {
 
        // code goes here
    }
}
</pre>


= Function Definitions =
= Function Definitions =
Line 30: Line 96:
</pre>
</pre>


= In-line Callbacks =
= Callbacks =


In-line callbacks should utilize shorthand/arrow notation:  
Callbacks should utilize shorthand/arrow notation:


<pre>
<pre>
button.addEventListener('click', (event) => {
button.addEventListener("click", (event) => {
 
    event.preventDefault();


     // code goes here
     // code goes here
Line 41: Line 109:
</pre>
</pre>


If the in-line callback is a reusable block of code that already has a function definition, use the function name instead of shorthand/arrow notation:
If the callback is a reusable block of code, create a function for the reusable block if it doesn't already exist and use the function name instead of shorthand/arrow notation:


<pre>
<pre>
function validate_form_fields(form_fields) {
function validate_form_fields(event, form_fields) {
 
    event.preventDefault();


     // code goes here
     // code goes here
}
}


button.addEventListener('click', validate_form_fields(form_fields));
form.addEventListener("submit", validate_form_fields(event, form_fields));
</pre>
</pre>
== event vs. e ==
Do not simply type "e" for an event. Completely type out "event":
<pre>
// do not do this:
button.addEventListener("click", (e) => {
    e.preventDefault();
});
// do this:
button.addEventListener("click", (event) => {
    event.preventDefault();
});

Latest revision as of 18:01, 27 October 2022

Naming Conventions

All variables, functions, and classes should all have meaningful names:

// do not do this
var f = "Aaron";

// do this
var first_name = "Aaron";

Variables

Variable names should all be lower case:

var email;

If the variable name is made up of multiple words, use snake case:

var email_address;

Strings

Strings should be enclosed in double quotes ("):

var user_name = "Aaron";

String Concatenation vs. Variable Injection

String concatenation is discouraged. Instead, enclose your strings that require variable injections in backticks (`) and place the variable using ${} notation:

// do not do this:
var message = user_name + " has logged out of the system.";

// do this:
var message = `${user_name} has logged out of the system.`;

Functions

Function names should all be lowercase:

function update();

If the function name is made up of multiple words, use snake case:

function update_by_id();

Classes

Class names should all be capitalized:

class Vertical {

    constructor() {

        // code goes here
    }
}

If the class name is made up of multiple words, use Pascal case:

class VerticalGroup {

    constructor() {

        // code goes here
    }
}

Function Definitions

Function definitions should all be full definitions with no shorthand/arrow notation:

function function_name(parameter_1, parameter_2) {

    // code goes here
}

Callbacks

Callbacks should utilize shorthand/arrow notation:

button.addEventListener("click", (event) => {

    event.preventDefault();

    // code goes here
});

If the callback is a reusable block of code, create a function for the reusable block if it doesn't already exist and use the function name instead of shorthand/arrow notation:

function validate_form_fields(event, form_fields) {

    event.preventDefault();

    // code goes here
}

form.addEventListener("submit", validate_form_fields(event, form_fields));

event vs. e

Do not simply type "e" for an event. Completely type out "event":

// do not do this:
button.addEventListener("click", (e) => {

    e.preventDefault();
});

// do this:
button.addEventListener("click", (event) => {

    event.preventDefault();
});