



\section{
\label{code-style.tex}
Coding Style}


To ensure maintainability and consistency, please follow the recommendations below when developing Moses.



\subsection{
Formatting}


{\bf Indentations} are 2 spaces. No tab characters allowed in the code. To ensure that your code follows this format run {\tt scripts/other/beautify.perl} in the directory of the source code.



{\bf Opening braces} are on a separate line, for instance:






\begin{SaveVerbatim}{myverb}
if (expr) {
...
}
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

However, there is one special case, namely functions: they have the
opening brace at the beginning of the next line, thus:






\begin{SaveVerbatim}{myverb}
int Function(int x)
{
body of function
}
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

{\bf Upper/lowercase:} Start all functions and class names with capital letters. Start all variable with small letter. Start all class variable with {\tt m\_}. For instance:






\begin{SaveVerbatim}{myverb}
void CalcNBest(size_t count, LatticePathList &ret) const;
Sentence m_source;
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Use {\bf long variable names}, not variables called {\tt s}, {\tt q}, or {\tt qb7}.



{\bf Do not use Hungarian notation\footnote{\sf http://en.wikipedia.org/wiki/Hungarian\_notation}.}



\subsection{
Comments}


The code will be parsed by Doxygen to create online documentation\footnote{\sf http://www.statmt.org/moses/html/hierarchy.html}. To support this, you have to add comments for each class, function, and class variable. More information is available at the Doxygen\footnote{\sf http://www.stack.nl/\textasciitilde{}dimitri/doxygen/docblocks.html} web site.



{\bf Class} definitions in the {\tt *.h} file need to be preceeded by a block starting with {\tt /**}, for instance:






\begin{SaveVerbatim}{myverb}
/** The Manager class implements a stack decoding algorithm.
* Hypotheses are organized in stacks. One stack contains all hypothesis that have
* the same number of foreign words translated.  The data structure for hypothesis
* stacks is the class HypothesisStack. The data structure for a hypothesis
* is the class Hypothesis.
[...]
**/
class Manager
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

{\bf Class member variable} definitions in {\tt *.h} must be followed by a comment that starts with {\tt //!}, for instance:






\begin{SaveVerbatim}{myverb}
size_t m_maxNumFactors;  //! max number of factors on both source and target sides
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

{\bf Functions} in the {\tt *.cpp} file need to be preceeded by a block starting with {\tt /**}, for instance:






\begin{SaveVerbatim}{myverb}
/**
* Main decoder loop that translates a sentence by expanding
* hypotheses stack by stack, until the end of the sentence.
*/
void Manager::ProcessSentence()
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

Function parameters are described by {\tt param}, for instance:






\begin{SaveVerbatim}{myverb}
/** Create translation options that exactly cover a specific input span.
* Called by CreateTranslationOptions() and ProcessUnknownWord()
* \param decodeGraph list of decoding steps
* \param factorCollection input sentence with all factors
* \param startPos first position in input sentence
* \param lastPos last position in input sentence
* \param adhereTableLimit whether phrase & generation table limits are adhered to
*/
void TranslationOptionCollection::CreateTranslationOptionsForRange(
const DecodeGraph &decodeGraph
, size_t startPos
, size_t endPos
, bool adhereTableLimit)
{
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

In addition the definition in the {\tt *.h} must be preceded by a short comment that starts with  {\tt //!}. This comment will be displayed in the beginning of the class definition. For instance:






\begin{SaveVerbatim}{myverb}
//! load all language models as specified in ini file
bool LoadLanguageModels();
\end{SaveVerbatim}
\begin{shaded}
\small
\UseVerbatim{myverb}
\end{shaded}

\subsection{
Data types and methods}

\begin{compactitem}
\item Code for cross-platform compatibility.

\item Use object-orientated designs, including
\begin{compactitem}
\item Create Get/Set functions rather than exposing class variables. 

\item Label functions, variables and arguments as const where possible. 

\item Prefer references over pointers

\item General styles

\item Prefer enum types over integers

\item Resolve compiler warnings as well as errors

\item Delete tracing/debugging code once they are not needed.

\end{compactitem}

\end{compactitem}

\subsection{
Source Control Etiquette}

\begin{compactitem}
\item Do not check in non-compilable code, or if functionality is reduced

\item Ignore the above if you need to, just let people know

\item Check-in your work often to avoid resolution conflicts

\item Add log messages to check-ins

\item Check in make/project files. However, you are not required to update project files other than the ones you use.

\end{compactitem}










\begin{center}\textit{\small Subsection last  modified on May 26, 2015, at 05:41 PM}\end{center}
