Share via


Spacing and Alignment

[This content is no longer valid. For the latest information on "M", "Quadrant", SQL Server Modeling Services, and the Repository, see the Model Citizen blog.]

The following guidelines provide recommendations for spacing and alignment in “M” code.

Do Use a Single Space after a Comma

The following “M” code example uses this guideline.

type Person : HasAutoId, HasDescription {...}

Persons : Person* 
{
    {Name = "Mary", Age = 27},
    {Name = "Joe", Age = 29}
}

The following “M” code example is incorrect, because it does not use a space following the commas.

type Person : HasAutoId,HasDescription {...}

Persons : Person* 
{
    {Name = "Mary",Age = 27},
    {Name = "Joe",Age = 29}
}

Do Use a Space before and after the Colon in a Type Ascription

The following “M” code example uses this guideline.

type Person : HasAutoId, HasDescription 
{
    FullName : Text; 

}

People : Person*;

The following “M” code example does not follow this guideline. In the definition of the Person type, there are no spaces before or after the colon. In the People extent definition, there is no space after the colon.

type Person:HasAutoId, HasDescription 
{
    FullName:Text; 

}

People: Person*;

Do Not Leave a Space before the Multiplicity Operators

Do not leave a space before the multiplicity operators. These include the ? operator, the + operator, and the * operator. The following “M” code example uses this guideline.

Description : Text?;

People : Person*;

The following “M” code example incorrectly places a space before the multiplicity operators.

Description : Text ?;

Persons : Person *;

Do Not Leave a Space before the Semicolon Line Terminator

The following “M” code example uses this guideline.

People : Person*;

The following “M” code example incorrectly places a space before the semicolon.

People : Person* ;

Do Not Use a Space inside Parentheses or Braces

The following “M” code example uses this guideline.

type Person
{
    Name : Text;

    Age : Integer32 where (0 <= value && value <= 124);

}

People : Person*
{
    {Name = "Mary", Age = 27},
    {Name = "Joe", Age = 29}
}

The following “M” code example incorrectly uses spaces inside the parentheses of the Age constraint. It also incorrectly places spaces inside the braces of each entity in the People extent.

type Person
{
    Name : Text;

    Age : Integer32 where ( 0 <= value && value <= 124 );

}

People : Person*
{
    { Name = "Mary", Age = 27 },
    { Name = "Joe", Age = 29 }
}

Do Use a Space before and after Operators

Place spaces before and after operators in expressions. The following code snippet uses this guideline.

... where x > (y + z) && y < 3;

The following example incorrectly removes spaces between all of the operators in the previous example.

... where x>(y+z)&&y<3;

Do Use a Blank Line after Each Declaration in a Type, Extent or Module

The following “M” code example uses this guideline.

type Person
{
    Id  : Integer64;

    Name : Text;

    Age : Integer32 where 0 <= value && value <= 123;

} where identity Id;

Persons : Person*;

The following “M” code example incorrectly removes spaces between each declaration in the Contact module.

type Person
{
    Id  : Integer64;
    Name : Text;
    Age : Integer32 where 0 <= value && value <= 123;
} where identity Id;
Persons : Person*;

Do Not Leave a Blank Line between Declarations in an Initializer

Do not include any blank lines between declarations in an initializer list. There is one exception to this rule. If comments are included in an initializer, each comment should be placed immediately before the entry it describes with a blank line before the comment. The following “M” code example demonstrates this guideline with three different variations of initializers.

Family 
{
    {Name = "Sue", Age = 27},
    {Name = "John", Age = 29}
}

Friends 
{
    {
        Name = "Mary", 
        Age = 27,
        HomeAddress = "1010 NE 200th Street, Redmond, WA"
    },
    {
        Name = "Joe",
        Age = 29,
        HomeAddress = "5100 45th Ave SE, Tacoma, WA"
    }
}

type AgeGroup : Text where value in
{
    // Age 0-11.
    "Child",
        
    // Age 12-18
    "Youth",
        
    // Age 18 and older
    "Adult",
    "Unknown"
};

In the preceding example, the first extent, Family, shows that there are no blank lines between the single-line initializer instances. The Friends extent shows the same pattern for multi-line initializer instances. The AgeGroup initializer list does include a blank line before each comment; however, the final entry named Unknown does not contain a comment, so there is no blank line preceding that declaration.

The following “M” code example incorrectly includes blank lines between the items in initializers. It also incorrectly removes blank lines between commented initializer items.

Family 
{
    {Name = "Sue", Age = 27},
        
    {Name = "John", Age = 29}
        
}

Friends 
{
    {
        Name = "Mary", 
        Age = 27,
        HomeAddress = "1010 NE 200th Street, Redmond, WA"
    },
        
    {
        Name = "Joe",
        Age = 29,
        HomeAddress = "5100 45th Ave SE, Tacoma, WA"
    }
}

type AgeGroup : Text where value in
{
    // Age 0-11.
    "Child",
    // Age 12-18
    "Youth",
    // Age 18 and older
    "Adult",

    "Unknown"
};

Do Align Clauses in Constraint Expressions with Commas Separating Top-level Clauses

The following “M” code example uses this guideline.

type Person
{
    Id : Integer64;   

    Name : Text;

    HomeAddress : Address?;

    WorkAddress : Address?;

} where 
    identity Id,
    unique Name;

Persons : Person* where
    item.Id in Parties.Id,
    item.HomeAddress in Addresses,
    item.WorkAddress in Addresses;

The following “M” code example incorrectly uses the && operator instead of commas. It also does not align the clauses on separate lines.

Persons : Person* where item.Id in Parties.Id
    && item.HomeAddress in Addresses && item.WorkAddress in Addresses;

See Also

Other Resources

"M" Coding Conventions