Jump to content

Development/Code Conventions

From The Document Foundation Wiki

Code Conventions

Most of the LibreOffice code is written with C++, but there are also many source code files that are in Java, Python and BASIC. LibreOffice uses specific coding conventions, in naming, data type and many other things. Following those conventions helps keeping the code clean and readable.

LibreOffice Data Types

Note:
Data types are discussed in more details in LibreOffice Developer's Guide. To learn more, please refer to first chapter of the LibreOffice Developer's Guide.

The data types that are listed in the following table are used for members of structs, as method return types or method parameters. Also, mappings to Java, C++, and LibreOffice Basic types are provided.

UNO Type description Java C++ Basic
void empty type, used only as method return type and in any void void -
boolean Boolean type; true and false boolean bool
(sal_Bool is deprecated)
Boolean
byte signed 8-bit integer type byte sal_Int8 Integer
short signed 16-bit integer type short sal_Int16 Integer
unsigned short unsigned 16-bit integer type (deprecated) - sal_uInt16 -
long signed 32-bit integer type int sal_Int32 Long
unsigned long unsigned 32-bit integer type (deprecated) - sal_uInt32 -
hyper signed 64-bit integer type long sal_Int64 -
unsigned hyper unsigned 64-bit integer type (deprecated) - sal_uInt64 -
float IEC 60559 single precision floating point type float float (if appropriate) Single
double IEC 60559 double precision floating point type double double (if appropriate) Double
char 16-bit Unicode character type (more precisely: UTF-16 code units)- char sal_Unicode -
string[1] Unicode string type (more precisely: strings of Unicode scalar values) java.lang.­String rtl::OUString String

C++ Code Conventions

LibreOffice uses C++ 20 as the standard for the code base. Therefore, you can use the latest features that are available in C++20.

On the other hand, there are things that are specific to LibreOffice source code. In order to make it possible to have interoperability across different operating systems and different programming languages for the LibreOffice API, LibreOffice has its own set of data types, as discussed above.

Code Formatting

For the newer files, you should use the tool clang-format, to re-format your code. Otherwise, CI will not accept your submitted code.

Naming Conventions

In LibreOffice C++ code, there are specific rules for naming variables according to their data types. That is helpful to learn about the data type of a variable by just looking into its prefix.

Data Type Template Example
(Smart) pointer/reference to a UNO interface xName xContext
(Smart) pointer to anything else pName pControl
Reference to anything else rName rTopic
Integer value nName nWhich
Floating-point number fName fWidth
Boolean bName, or isName, or canName isEndOfSentence
String optionally sName (for char * -> pName; for OUString -> aName sDateFormat
Another object aName aActual

Notes

  1. There are special conditions for types that do not have an exact mapping in this table. Check for details about these types in the corresponding sections about type mappings in UNO Language Bindings