Skip to content

General

This document lists some recommendations for code written it MPS.

Before reading this document, read the mbeddr Development Guide. It describes how git is used in the mbeddr project for contributing changes, and it also provides some general guidelines (commit messages, branches, merging, test organization etc.).

Readability/Maintainability

  • Use enhanced for loops instead of for loops with counters: for(type variable:iterable) and foreach variable in iterable
  • Mark classes/concepts etc. as deprecated when they shouldn't be used: DeprecatedNodeAnnotation DeprecatedNodeAnnotation DeprecatedBlockDocTag DeprecatedBlockDocTag Java Deprecated annotation Java Deprecated annotation
  • Use the text TODO in comments. These strings can be found with the TODO tool.
  • Check for redundant variable initializers (typesystem warning).
  • Use empty lines between statements (for example method declarations) for readability.
  • Use virtual packages to organize your code.

Null safety

  • Use language checkedDots for saver access of possible null values. Nodes can be checked for null: node.isNull
  • Use annotation @NotNull and @Nullable for baselanguage code. There is a typeystem rule in MPS that checks these annotations.
  • Use :eq: (NPE-safe equals operation) and :ne: NPE-safe not equals operation instead of == and equals.
  • Return optional values instead of null in baselanguage code1.

Exceptions

  • Use checked exceptions for recoverable conditions and run-time exceptions for programming errors 2,3.
  • Consider alternative ways of showing errors than throwing exceptions such as showing notifications. Examples for such notifications are balloons and dialogs.
  • Attach throwable objects to logging statement if available: log error "This is an error",errorObject
  • Clean up in the finally block of a try statement.

Swing components

  • Use components from the intellij platform. Some additional components can be found here. Use the intellij platform ui guidelines to create consistent and usable user interfaces.
  • Capitalization: almost all short textual items (menus, buttons, labels, for example) should have headline capitalization, where all words are capitalized except for common words with up to three letters (a, an, the, and, or, so, yet, etc.), and do not appear as the first or last word. If the text is not short, then the capitalization used in ordinary prose (sentence capitalization) should be used instead.
  • Read the Swing tutorials to get a better understanding of the components.

Last update: November 11, 2021

Comments

Back to top