Official Code Format Guide

Developers forum for Univeral Media Server-related development (only for programmers)
Post Reply
User avatar
SubJunk
Lead Developer
Posts: 3707
Joined: Sun May 27, 2012 4:12 pm

Official Code Format Guide

Post by SubJunk »

This guide is to show the proper format of code we prefer.
You can still submit patches with your own preferred formatting and we will convert them to our formatting, but if you are interested here is how it will be formatted:

Code: Select all

dlnaOrgOpFlags = "01";

if (mediaRenderer.isSeekByTime()) {
	if (getPlayer() != null) {
		if (getPlayer().isTimeSeekable()) {
			/**
			 * This comment is on multiple lines so it gets the asterisks
			 * This comment is on multiple lines so it gets the asterisks
			 */
			if (mediaRenderer.isSeekByTimeExclusive()) {
				dlnaOrgOpFlags = "10";
			} else {
				dlnaOrgOpFlags = "11";
			}

			if (mediaRenderer.isSeekByTimeExclusive()) {
				dlnaOrgOpFlags = "10";
			}
		}
	} else {
		// This comment only uses one line
		dlnaOrgOpFlags = "11";
	}
}
The important points here are:
  • Each indentation is one tab, not spaces. This is done to allow each developer to customise their own indents (most code editors have an option like "tab size") instead of it being dictated to us by spaces. Note: Due to the way the forum displays code, the tabs have been converted to spaces, so just imagine they are tabs.
  • Spaces between each separate part, for example on the fourth line there are 4 different spaces. This makes the code easier to read.
  • Always use brackets for if statements, loops, etc. and put the else brackets on the same line, as in the 5th line from the bottom.
  • Line-breaks between statements, as on the 15th line.
  • No trailing spaces - there should be no spaces after any line.
There are probably more conventions I haven't included here but that's a good start. If you have any questions or suggestions feel free to post.
Post Reply