03 Code Formatting
03 Code Formatting
2
Code Formatting
Why Do We Need It?
Why Code Needs Formatting?
public const string FILE_NAME
="example.bin" ; static void Main ( ){
FileStream fs= new FileStream(FILE_NAME,FileMode
. CreateNew) // Create the writer for data .
;BinaryWriter w=new BinaryWriter ( fs );//
Write data to Test.data.
for( int i=0;i<11;i++){w.Write((int)i);}w .Close();
fs . Close ( ) // Create the reader for data.
;fs=new FileStream(FILE_NAME,FileMode. Open
, FileAccess.Read) ;BinaryReader r
= new BinaryReader(fs); // Read data from Test.data.
for (int i = 0; i < 11; i++){ Console .WriteLine
(r.ReadInt32 ())
;}r . Close ( ); fs . Close ( ) ; }
4
Code Formatting Fundamentals
Good formatting goals
if (some condition)
{
// Block contents indented by a single [Tab]
// VS will replace the [Tab] with 4 spaces
}
6
Formatting Blocks in JavaScript
if (some condition) {
// Block contents indented by a single [Tab]
// Don't use spaces for indentation
}
7
Empty Lines between Methods
Use empty line for separation between methods:
public class Factorial
{
private static ulong CalcFactorial(uint num)
{
if (num == 0) Always use { and } after if
return 1; (there is no space to do it here)
else
return num * CalcFactorial(num - 1);
}
Leave empty line
static void Main() between methods
{
ulong factorial = CalcFactorial(5);
Console.WriteLine(factorial);
}
}
8
Methods Indentation
Methods should be indented with a single
[Tab] from the class body
Methods body should be indented with a
single [Tab] as well
10
Separating Parameters
Separate method parameters by comma
followed by a space
Don't put space before the comma
Examples:
private void RegisterUser(string username, string password)
RegisterUser("nakov", "s3cr3t!p@ssw0rd");
Incorrect examples:
private void RegisterUser(string username,string password)
15
Formatting Conditional
Statements and Loops
Formatting conditional statements and loops
if (matrix[x, y] == 0 || matrix[x-1, y] == 0 ||
matrix[x+1, y] == 0 || matrix[x, y-1] == 0 ||
matrix[x, y+1] == 0)
{
21
Breaking Long Lines
in C# and JavaScript
In C# use single [Tab] after breaking a long line:
if (matrix[x, y] == 0 || matrix[x-1, y] == 0 ||
matrix[x+1, y] == 0 || matrix[x, y-1] == 0 ||
matrix[x, y+1] == 0)
{
matrix[x, y] == 1;
}
int count = 0;
DateTime date = DateTine.Now.Date;
Student student = new Student();
List<Student> students = new List<Student>();
http://academy.telerik.com
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
SEO - ,
, iPhone, Android, WP7, PhoneGap
, HTML, CSS, JavaScript, Photoshop -
free C# book, C#, Java, C# -
" "
" cloud " C# , ,
Homework
1. Format correctly the following source code (given in
Code-Formatting-Homework.zip):
C# code given in the file events.cs.
JavaScript code given in the file code.js.
Use the official code conventions for C# / JavaScript /
Java / PHP:
Official C# Coding Conventions (MSDN)
Google JavaScript Style Guide
Official Java Code Conventions (by Oracle)
Zend code convention for PHP
26
Free Trainings @ Telerik Academy
C# Programming @ Telerik Academy
csharpfundamentals.telerik.com