Skip to main content

Coding Style

Code Quality: Coding Style

When it comes to coding style and application development, what is the goal?

View Answer:
Interview Response: The goal is to take a complex task and code it correctly and in a human-readable manner.

note

Several code styling guides help programmers reach this goal.


Should you always use curly braces with conditional if statements?

View Answer:
Interview Response: Yes, it is a best practice and makes code more readable.

Code Example:

// conditional if statement
if (n < 0) {
alert(`Power ${n} is not supported`);
} else {
alert(`Power ${n} is supported`);
}

Is it okay to use long horizontal code lines or split them into separate lines? Give a reason why you choose your answer?

View Answer:
Interview Response: Professional JavaScript developers prefer separate lines because they are more readable than not. It is also a best practice.

Code Example: Proper Implementation

// backtick quotes (`) allow splitting the string into multiple lines
let str = `
ECMA International's TC39 is a group of JavaScript developers,
implementers, academics, and more collaborating with the community
to maintain and evolve the definition of JavaScript.
`;

// Conditional If statement variable conditions split in multiple lines
if (id === 123 && moonPhase === 'Waning Gibbous' && zodiacSign === 'Libra') {
letTheSorceryBegin();
}
note

A great example would be long paragraphs longer than 120 characters. We can use backticks to handle lines longer than 120 characters.


What do development teams typically agree to for the maximum character length?

View Answer:
Interview Response: The maximum length typically agreed upon by dev teams is between 80 to 120 characters.

Code Example: Proper Implementation

// backtick quotes (`) allow splitting the string into multiple lines
let str = `
ECMA International's TC39 is a group of JavaScript developers,
implementers, academics, and more collaborating with the community
to maintain and evolve the definition of JavaScript.
`;

What is a best practice for vertical space of code?

View Answer:
Interview Response: A best practice is to divide each code action by a line space to keep your code readable.

Code Example:

function pow(x, n) {
let result = 1;
// <--
for (let i = 0; i < n; i++) {
result *= x;
}
// <--
return result;
}

What is one way to reduce nesting levels in your code?

View Answer:
Interview Response: A great way to reduce nesting levels is handling minimum conditions early. The goal is to reduce nested code blocks that isolate variable access. The benefits of writing code in this fashion are increased readability and faster code because of early conditional error checks. If the code fails early, then it is handled without continuity.

Here are some rules of thumb for reducing nesting in your code:

  1. Keep your conditional blocks brief. Keeping things local improves readability.
  2. Think about restructuring if your loops and branches are more than two layers deep.
  3. Consider separating layered logic into distinct functions. For example, you may write a function to handle each item instead of using a double nested loop to cycle through a list of objects containing a list (such as a protocol buffer with repeated fields).

The two constructs below are identical:

function pow(x, n) {
if (n < 0) {
alert("Negative 'n' not supported");
} else {
let result = 1;

for (let i = 0; i < n; i++) {
result *= x;
}

return result;
}
}

This is proper way to avoid nesting:

function pow(x, n) {
if (n < 0) {
alert("Negative 'n' not supported");
return;
}

let result = 1;

for (let i = 0; i < n; i++) {
result *= x;
}

return result;
}

What is a Linter, and why should you use it?

View Answer:
Interview Response: Linters are tools that can automatically check the style of your code and make improvement suggestions. The great thing about linters is that style-checking can also find bugs, like typos in variable or function names. Because of this feature, using a linter is recommended even if you do not want to stick to one particular code style.