💻 Code Basics

How to code efficiently Ciphera style.

Overview

Before Starting

Below we will show you the steps you should follow to program in what in our experience we consider to be the right way.

  • Structure the architecture: One of the most popular best practices for developers is to design a concrete architecture. Before starting to write code, consider its utility, how it functions, how it is modularized, and which services it will be compatible with. Think about the structure it will have, how you will test it, and how it will be updated.

  • Project folder structure: For the project structure, Screaming Architecture will be used on top of the rest for easy scalability and better organization of the project in the long term.

  • Comment your code: Remember that commenting your code is essential for the project.

  • Simplify as much as possible: Avoid complex code to reduce bugs and save time. Aim for functional, straightforward code.

Most examples will be shown in TypeScript but this can be applied to any language.

Best practices and conventions

When programming, the following standards for Ciphera should be followed:

  • Always elbow in American English.
Remember that this also applies when making a pull request!
// ❌ We are not coding efficiently for everybody.
const nombre = "Jack";

// ✅ We must use the standards in the world of technology.
const name = "Jack";
  • Use double quotation marks whenever possible.
// ❌ We must use ("") to follow Ciphera standards.
import DymoAPI from 'dymo-api';

// ✅ Double quotes are supported by most languages with respect to single quotes.
import DymoAPI from "dymo-api";
  • Use of string interpolation instead of value concatenation.
// ❌ This code is slightly slower than using string interpolation.
const path = "/path/to/" + folderName + "/file";

// ✅ The use of interpolation gives us more speed.
const path = `/path/to/${folderName}/file`;
  • Do not create single-use variables unless they are to be imported from a different module.
// ❌ We create unnecessary variables, and we do not respect their usefulness for recruiting.
function greet(name) {
    const greeting = "Hello";
    return `${greeting}, ${name}!`;
};

// ✅ In this way we increase the speed and efficiency of the code.
function greet(name) {
    return `Hello, ${name}!`;
};
  • Use semicolons (;) for languages that require them, such as JavaScript.
// ❌ We must use (;) to follow Ciphera standards.
import DymoAPI from "dymo-api"

// ✅ Remember that the (;) help us to maintain a clean code.
import DymoAPI from "dymo-api";
  • Sort imports by length.
// ❌ Unorganized code.
import DymoAPI from "dymo-api";
import axios from "axios";

// ✅ Code ordered.
import axios from "axios";
import DymoAPI from "dymo-api";
  • Use camelCase or snake_case depending on what the language requires.
// ❌ This code is in TypeScript and must use camelCase.
const fetch_data = await fetch("/api");

// ✅ In this way we respect the standards of each language.
const fetchData = await fetch("/api");
  • Do not create dependencies on external hostings.
This also applies to text fonts and logos.
// ❌ This import depends on a URL that can be modified at any time.
import resource from "https://cdn.tscodelib.com/[email protected]";

// ✅ Right.
import resource from "resource";
  • Avoid unnecessary use of block delimiters and follow a consistent indentation to structure the code when it is only one line.
// ❌ In this code we generate extra lines.
if (!data) {
    return "Error processing the request.";
}

// ✅ This code is more compact and still maintains readability.
if (!data) return "Error processing the request.";
  • Avoid all unnecessary code.
# ❌ We are adding unnecessary brackets.
if (not data and not info): return "Error processing the request."

# ✅ Clean code.
if not data and not info: return "Error processing the request."

# ❌ We are adding unnecessary commas.
params = {
    type: "GET",
    format: "mp4",
}

# ✅ Clean code.
params = {
    type: "GET",
    format: "mp4"
}
  • Use comments appropriately, including endpoints and types that match the type of comment you are adding.
// ❌ For one-line comments (//) must be used.
/* This function returns the customer's purchases. */

// ✅ Use the correct formatting and endpoints.
// This function returns the customer's purchases.

// ✅ Right.
/**
 * Calculates the area of a rectangle.
 * @param width - The width of the rectangle.
 * @param height - The height of the rectangle.
 * @returns The area of the rectangle.
 */
function calculateRectangleArea(width: number, height: number): number {
    return width * height;
};

Project structure

To maintain a good structure in your project follow these steps:

  • Encapsulate repetitive code in functions and export them.
  • Upload all media files to a Global CDN.
You can use Dymo CDN if you work in Ciphera.
  • In case of using obscure libraries download the code in your project utilities instead of dependency to keep the project safe.

  • Uses 4-space indexing.

// ❌ Code a little visually consuoupated.
function Some() {
  console.log("Hello World!");
};

// ✅ Uses 4-space indexing.
function Some() {
    console.log("Hello World!");
};

Speed/performance and statistics

To increase the performance and speed of your applications, you should follow the following tips:

  • Prioritize running the code that is most likely to be correct based on the features your customers use most frequently. Do not reproduce identical code fragments!

If you have a string to validate and 70% of the time it is the same value, validate that option over the rest to increase the probability.

// Example: 70% of the time the input is an email.

/* ✅ We validate the email first since according to previous
 requests most of them are email type, thus reducing the response time.
*/

if (\REGEX_EMAIL\.test(inputData)) {
    // An action with input.
} else if (\REGEX_DOMAIN\.test(inputData)) {
    // An action with input.
} else {
    // Error.
}
  • Use version control.
  • Review and test your code.
  • Avoid using technologies for cross-platform development, as they tend to result in lower performance. Instead, focus on specific, tailored solutions for greater efficiency and effectiveness.
  • Avoid as much as possible the use of barrel files in the code, they negatively affect the app.

Conclusion

Soon we will bring new concepts and best practices that you should apply to your code.