Skip to content
Arran France edited this page Jun 28, 2015 · 45 revisions

The following are the adopted coding standards:

  • First and foremost, follow the established patterns you see in the existing code base. Don’t invent your own patterns!

  • See http://www.dotnetspider.com/tutorials/CodingStandards.doc

    • Except where noted in our Naming Conventions follow items 1 through 11 in section 7. Naming Conventions and Standards.
  • Use Spaces not TABs. Use 4 spaces instead of a tab. In Visual Studio, under Tools->Options, Text Editor, C#, Tabs, set your Tab size and Indent size to 4 and radio option to "Insert spaces".

    Tab Settings

  • Other C# "Spacing" Settings

    • Check both 'Insert space within argument list parenthesis' options

    Check both 'Insert space within argument list parenthesis' options

    • Check three other spacing options

    Check: * Insert space after keywords in control flow statements * Insert space within parenthesis of expressions * Insert spaces inside parenthesis for control flow statements

    • Check four more spacing options for delimiters

    Check: * Insert space after colon for base of interface in type declaration * Insert space after comma * Insert space after semicolon in "for" statement * Insert space before colon for base or interface in type declaration

  • Use the Organize Usings | Remove and Sort tools when editing your .cs files. Sort your usings putting 'System' directives first.

    Tab Settings

    • Turn this option on in Visual Studio, under Tools->Options, Text Editor, C#, Advanced, and enable "Place 'System' directives first when sorting usings"
  • Use GhostDoc

    Download GhostDoc from http://submain.com/download/ghostdoc/. Enter your email address and click "Download GhostDoc". Just use the default settings when installing. Now that it is installed, use it to document methods, classes, properties, etc. It is OK to just leave the default comments that it generates. At a minimum, it is nice to get rid of the compiler warnings due to wrong or missing XML comments.

Tip: Use the keyboard shortcut Ctrl-Shift-D for quickest usage.

Git Branching

Read the Git Branching Strategy page to follow our branching standard convention.

JavaScript Best Practices

Read the JavaScript Coding Standards page to get up to speed on what we like to see on the front-end.

Date, Time, Datetime and Numeric Comparison Logic

Read the Dates and Times section of the 101 book for vital things you should be aware of when writing Linq/SQL code involving Date, Time, Date/Time, or Numeric comparisons.

Code Regions

Code regions (#region X and #endregion) should be used to organize code. Models, controls, and blocks should use the following standard region names. Additional code regions can be used inside these standard region names

Models

Region Name Description
#region Entity Properties All the properties that should be mapped to a database field
#region Virtual Properties Non-mapped properties
#region Public Methods
#region Entity Configuration The class used to specify Entity Framework mapping configuration for the entity

Controls and Blocks

Region Name Description
#region Fields Private variables
#region Properties Public/Protected properties
#region Base Control Methods The overrides of the base RockBlock methods (i.e. OnInit, OnLoad)
#region Events Handlers called by the controls on your block.
#region Methods Your helper functional methods (like BindGrid(), etc.)

You can create additional sub regions as needed if you have a complicated block.

SQL Coding Standards

  • Capitalize reserved words
  • Main keywords on new line
  • Use brackets around table and column names
  • Use proper capitalization for table and column names

Example 1

    ALTER PROCEDURE [dbo].[spCheckin_WeeksAttendedInDuration]
        @PersonId int
        ,@WeekDuration int = 16
    AS
    BEGIN

        DECLARE @LastSunday datetime 

        SET @LastSunday = [dbo].[ufnUtility_GetPreviousSundayDate]()

        SELECT 
            COUNT(DISTINCT dbo.ufnUtility_GetSundayDate(a.[StartDateTime]) )
        FROM
            [Attendance] A
        WHERE 
            [GroupId] IN (SELECT [Id] FROM [dbo].[ufnCheckin_WeeklyServiceGroups]())
            AND A.[PersonId] IN (SELECT [Id] FROM [dbo].[ufnCrm_FamilyMembersOfPersonId](@PersonId))
            AND A.[StartDateTime] BETWEEN DATEADD("week", (@WeekDuration * -1), @LastSunday) AND @LastSunday 
    END

Example 2

    INSERT INTO [AttributeValue] 
        ([IsSystem]
        ,[AttributeId]
        ,[EntityId]
        ,[Order]
        ,[Value]
        ,[Guid])
    SELECT
        1
        ,@AttributeId
        ,B.[Id]
        ,0
        ,'290C53DC-0960-484C-B314-8301882A454C'
        ,NEWID()
    FROM
        [Block] B
        INNER JOIN [Layout] L ON L.[Id] = B.[LayoutId] AND L.[SiteId] = @RockSiteId
    WHERE
        [BlockTypeId] = @BlockTypeID

Example 3

    -- Delete all the security linked page attribute values for the rock and public sites
    DELETE [AV]
    FROM
        [AttributeValue] AV
        INNER JOIN [Attribute] A ON A.[Id] = AV.[AttributeId]
        INNER JOIN [Block] B ON B.[Id] = AV.[EntityId]
        LEFT OUTER JOIN [Page] P ON P.[Id] = B.[PageId]
        LEFT OUTER JOIN [Layout] PL ON PL.[Id] = P.[LayoutId]
        LEFT OUTER JOIN [Layout] BL ON BL.[Id] = B.[LayoutId]
        LEFT OUTER JOIN [Site] S ON (S.[Id] = PL.[SiteId] OR S.[Id] = BL.[SiteId])
    WHERE
        A.[EntityTypeId] = @BlockEntityTypeId 
        AND A.[EntityTypeQualifierColumn] = 'BlockTypeId' 
        AND A.[EntityTypeQualifierValue] IN (@LoginStatusBlockTypeId,@LoginBlockTypeId,@ConfirmBlockTypeId,
            @NewAccountBlockTypeId,@ForgotBlockTypeId) 
        AND A.[FieldTypeId] = 8
        AND S.[Id] IN ( @PublicSiteId, @RockSiteId )

Be sure to keep the Naming Conventions page handy when you are writing new entities and blocks.

Clone this wiki locally