Skip to content

Coding conventions

c8y3 edited this page Nov 18, 2024 · 28 revisions

JavaScript

  • avoid ternary operator (condition?expression1:expression2)
  • avoid delete
  • do not use ../ in imports (always go down)
  • avoid ref
  • use template literals
  • prefer typescript (.tsx)
  • use CamelCase

React

  • avoid class components
  • prefer arrow function components
  • define propTypes, possible types are bool, string, func, array, object, number (see https://www.npmjs.com/package/prop-types)
  • useState
  • useEffect. In particular for asynchronous calls to the server that updates the state. See also the impact of its second parameter here
  • useHistory

Full component syntax example:

import { useState, useEffect } from 'react';
...
const Component = ({target, disabled}) => {
    const [configuration, setConfiguration] = useState({ color: 'blue', length: 10, length: 0 });

    useEffect(() => {
        asynchronousCallToRetrieveConfiguration().then((configuration) => {
             setConfiguration(configuration);
        });
    }, []);

    const _incrementLength = () => {
        setConfiguration({
            ...configuration,
            length: configuration.length +1
        });
    };

    return ...;
}

Component.propTypes = {
    target: PropTypes.string.isRequired,
    disabled: PropTypes.bool,
}

React documentation

Internationalization (react-intl)

  • useIntl
import { useIntl } from 'react-intl';
...
const intl = useIntl();
  • FormattedMessage

Graylog components

  • components/common/Select: use clearable={false} to ensure there is always a value
  • useStore

Jest

css

It seems we are using different ways to apply style to components => should make some stricter rules

List of class names encountered so far:

TODO:

  • It seems to me, attribute style should be removed. The use of css modules, most probably too. And would it be possible to use class names from graylog but by importing them, instead of using a string constant?
  • list all class names, see where they originate from, check they work, decide which ones are authorized

Java

  • use CamelCase

  • do not omit brackets around if with one statements

    incorrect

              if (messageSummary.getTimestamp().isBefore(date))
                  date = messageSummary.getTimestamp();

    correct

              if (messageSummary.getTimestamp().isBefore(date)) {
                  date = messageSummary.getTimestamp();
              }
  • Avoid abbreviations

    incorrect

      EventNotificationContext ctx;

    correct

      EventNotificationContext context;
  • Avoid dead code, such as dead imports

  • Avoid ternary operator (condition?expression1:expression2)