Any fool can write code that a computer can understand. Good programmers write code that humans can understand. Telerik Software Academy

Slides:



Advertisements
Similar presentations
Creating Vector Graphics in the Web Learning & Development Telerik Software Academy.
Advertisements

Redundant Array of Independent Disks Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Writing HTML made easier Telerik School Academy HTML, CSS and JavaScript.
Guidelines to Preparing and Delivering an Elevator Pitch Presentation Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training.
Two basic algorithms for path searching in a graph Telerik Algo Academy Graph Algorithms.
Correctly Formatting the Source Code Svetlin Nakov Telerik Software Academy academy.telerik.com.
About the Course Telerik Software Academy CSS Styling.
Binary, Decimal and Hexadecimal Numbers Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Eclipse + Android SDK, VS + Windows Phone SDK Telerik Software Academy Hybrid Mobile Applications.
Welcome to the JSON-stores world Learning & Development Telerik Software Academy.
Course Content, Evaluation, Exams Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Eclipse + Android SDK, VS + Windows Phone SDK Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Twitter Bootstrap Telerik Software Academy
Things you need to know George Georgiev Technical Trainer GeorgeAtanasov George Atanasov Front-End Developer.
Hamiltonian Cycle Penka Borukova Student at Telerik Academy.
Group Policy Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Reverse polish notation, Operations with big numbers Ivelin Rachev Telerik Corporation “Baba Tonka” High School of Mathematics.
April, Sofia, Telerik Software Academy Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET MVC.
Sales Processes, Sales Cycle and the Sales Force Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik.
Design Patterns that ease the design by identifying a simple way to realize relationships between entities. Antony Jekov Telerik Software Academy academy.telerik.com.
Using KendoUI for SPA Applications Learning & Development Telerik School Academy.
The way to create cross-platform apps Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
PEST Analysis, Porter’s 5 Forces and Sustainability Strategies Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System.
Searching for a Job, Writing CV and Cover Letter, Passing a Job Interview Telerik Academy for Software Engineers October 6 th 2012 – Sofia academy.telerik.com.
Drive Partitioning Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Telerik Software Academy Mobile apps for iPhone & iPad.
Google APIs and Facebook API Ivaylo Kenov Penka Borukova Telerik Corporation Telerik Academy Students.
The magic of virtual machines Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Web development with ExpressJS Learning & Development Telerik School Academy.
Device APIs with Xamarin
Telerik Software Academy Mobile apps for iPhone & iPad.
Academy.telerik.com Class of , Fall 2013.
Telerik Software Academy Databases.
Telerik Software Academy ASP.NET Web Forms.
What are WinJS and WinRT, Using the APIs in JavaScript George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
JavaScript Modules and Patterns Telerik Software Academy
The True power of dynamic web pages Learning & Development Team Telerik Software Academy.
Approximate string matching Evlogi Hristov Telerik Corporation Student at Telerik Academy.
Xamarin with SQLite Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
Know your Tools! Telerik Software Academy JavaScript Fundamentals.
Ivaylo Kenov Telerik Software Academy Technical Assistant.
Code Formatting Correctly Formatting the Source Code Svetlin Nakov Technical Trainer Software University
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Developing "evil" JavaScript applications Learning & Development Telerik School Academy.
The PhoneGap History Doncho Minkov Telerik Software Academy Technical Trainer
Building Rock-Solid Software Svetlin Nakov Telerik Software Academy Manager Technical Training
Adobe® Photoshop® CS6 Essentials
Cultivating Professionals for Your Company The Success Story of Telerik Svetlin Nakov, PhD Manager Technical Training Telerik Corp.
Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
AJAX with ASP.NET MVC Telerik Software Academy
Nencho Nenchev Doroteya Agayna Telerik Software Academy Telerik Support Leads.
Installation, Sample Usage, Strings and OOP Telerik Software Academy Software Quality Assurance.
In JavaScript Learning & Development Telerik Software Academy.
Services in AngularJS Telerik Software Academy
Svetlin Nakov Telerik Software Academy Manager Technical Training
What is Roslyn and how can we use it? Telerik Academy Plus C# 6.0 and Roslyn Seminar.
Splitting JavaScript into Dependent Modules Learning & Development Telerik Software Academy.
Changing the default visualization of views in Xamarin.Forms Telerik School Academy Xamarin apps for iOS, Android & WinPhone.
Code Formatting Formatting the Source Code Correctly SoftUni Team Technical Trainers Software University
Telerik Software Academy HTML5.
What to expect from the new IDE Telerik Academy Plus Visual Studio 2015 and ASP.NET 5.
Hristian Hristov Telerik Corporation
I have to use math? I am out of here… Telerik School Academy Unity 2D Game Development.
Monitoring Server Performance Organizing Your Support Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Automating Windows Deployment Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Server Roles and Features Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Binary, Decimal and Hexadecimal Numbers Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Adding UI and Audio element Telerik Academy Plus Unity 2D Game Development.
Presentation transcript:

Any fool can write code that a computer can understand. Good programmers write code that humans can understand. Telerik Software Academy High-Quality Code

1. Why Do We Need Code Formatting? 2. Formatting Methods 3. Formatting Types 4. Common Mistakes 5. Alignments 6. Automated Tools 2

Why do we need it? How can white spaces and parenthesis help us?

4 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 for (int i = 0; i < 11; i++){ Console.WriteLine (r.ReadInt32 ()) ;}r. Close ( ); fs. Close ( ) ; }

 Good formatting goals  To improve code readability  To improve code maintainability  Fundamental principle of code formatting:  Any (consistent) formatting style that follows the above principle is good  Good formatting don’t affect speed, memory use or other aspects of the program 5 The formating of the source code should disclose its logical structure.

 Put { and } alone on a line under the corresponding parent block  Indent the block contents by a single [Tab]  Visual Studio will replace the [Tab] with 4 spaces  Example: 6 if (some condition) { // Block contents indented by a single [Tab] // Block contents indented by a single [Tab] // VS will replace the [Tab] with 4 spaces // VS will replace the [Tab] with 4 spaces}

 Put { at the end of the block and } alone on a line under the corresponding parent block  Indent the block contents by a single [Tab]  [tab] or spaces depends on the team style  Example: 7 if (some condition) { // Block contents indented by a single [Tab] // Block contents indented by a single [Tab] // Choosing between [tab] and spaces depends // Choosing between [tab] and spaces depends // on project formatting style // on project formatting style}

8 // swap left and right elements for whole array for ( var i=0; i<MaxElements; i++ ) leftElement = left[i]; leftElement = left[i]; left[i] = right[i]; left[i] = right[i]; right[i] = leftElement; right[i] = leftElement; // swap left and right elements for whole array for (var i = 0; i < MaxElements; i++) { leftElement = left[i]; leftElement = left[i]; left[i] = right[i]; left[i] = right[i]; right[i] = leftElement; right[i] = leftElement;} x = 3+4 * 2+7; x = (3 + 4) * (2 + 7);

 Use empty line for separation between methods: 9 public class Factorial { private static ulong CalcFactorial(uint num) private static ulong CalcFactorial(uint num) { if (num == 0) if (num == 0) return 1; return 1; else else return num * CalcFactorial(num - 1); return num * CalcFactorial(num - 1); } static void Main() static void Main() { ulong factorial = CalcFactorial(5); ulong factorial = CalcFactorial(5); Console.WriteLine(factorial); Console.WriteLine(factorial); }} Leave empty line between methods Always use { and } after if (there is no space to do it here)

 Methods should be indented with a single [Tab] from the class body  Methods body should be indented with a single [Tab] as well 10 public class IndentationExample { private int Zero() private int Zero() { return 0; return 0; }} The entire method is indented with a single [Tab] Method body is also indented

 Brackets in the method declaration should be formatted as follows:  Don't use spaces between the brackets:  The same applies for if -conditions and for - loops: 11 private static ulong CalcFactorial(uint num) if (condition) { … }

 Separate method parameters by comma followed by a space  Don't put space before the comma  Examples:  Incorrect examples: 12 private void RegisterUser(string username, string password) RegisterUser("academy",

 Use an empty line to separate logically related sequences of lines: 13 private List PrepareReports() { List reports = new List (); List reports = new List (); // Create incomes reports // Create incomes reports Report incomesSalesReport = PrepareIncomesSalesReport(); Report incomesSalesReport = PrepareIncomesSalesReport(); reports.Add(incomesSalesReport); reports.Add(incomesSalesReport); Report incomesSupportReport = PrepareIncomesSupportReport(); Report incomesSupportReport = PrepareIncomesSupportReport(); reports.Add(incomesSupportReport); reports.Add(incomesSupportReport); // Create expenses reports // Create expenses reports Report expensesPayrollReport = PrepareExpensesPayrollReport(); Report expensesPayrollReport = PrepareExpensesPayrollReport(); reports.Add(expensesPayrollReport); reports.Add(expensesPayrollReport); Report expensesMarketingReport = PrepareExpensesMarketingReport(); Report expensesMarketingReport = PrepareExpensesMarketingReport(); reports.Add(expensesMarketingReport); reports.Add(expensesMarketingReport); return reports; return reports;} Empty line

 Formatting classes / structures / interfaces / enumerations  Indent the class body with a single [Tab]  Use the following order of definitions:  Constants, delegates, inner types, fields, constructors, properties, methods  Static members, public members, protected members, internal members, private members  The above order of definitions is not the only possible correct one 14

15 public class Dog { // Static variables // Static variables public const string SPECIES = public const string SPECIES = "Canis Lupus Familiaris"; "Canis Lupus Familiaris"; // Instance variables // Instance variables private int age; private int age; // Constructors // Constructors public Dog(string name, int age) public Dog(string name, int age) { this.Name = name; this.Name = name; this.age = age; this.age = age; } (continues on the next slide)

16 // Properties // Properties public string Name { get; set; } public string Name { get; set; } // Methods // Methods public void Breath() public void Breath() { // TODO: breathing process // TODO: breathing process } public void Bark() public void Bark() { Console.WriteLine("wow-wow"); Console.WriteLine("wow-wow"); }}

 Formatting conditional statements and loops  Always use { } block after if / for / while, even when a single operator follows  Indent the block body after if / for / while  Always put a new line after a if / for / while block!  Always put the { on the next line (in C#)  Always put the { on the same line (in JavaScript)  Never indent with more than one [Tab] 17

 Example:  Incorrect examples: 18 for (int i = 0; i < 10; i++) { Console.WriteLine("i={0}", i); Console.WriteLine("i={0}", i);} for (int i = 0; i < 10; i++) Console.WriteLine("i={0}", i); Console.WriteLine("i={0}", i); for (int i = 0; i < 10; i++) Console.WriteLine("i={0}", i); for (int i = 0; i < 10; i++) { Console.WriteLine("i={0}", i); Console.WriteLine("i={0}", i);} The { and } are missing Never put multiple stetements on the same line! In C# the { should be on the next line

 Empty lines are used to separate logically unrelated parts of the source code  Don't put empty lines when not needed! 19 public static void PrintList(List ints) { Console.Write("{ "); Console.Write("{ "); foreach (int item in ints) foreach (int item in ints) { Console.Write(item); Console.Write(item); Console.Write(" "); Console.Write(" "); } Console.WriteLine("}"); Console.WriteLine("}");} static void Main() { // … // … An empty line separates the methods An empty line after the foreach block

20 public static void PrintList(List ints) { Console.Write("{ "); Console.Write("{ "); foreach (int item in ints) foreach (int item in ints) { Console.Write(item); Console.Write(item); Console.Write(" "); Console.Write(" "); } Console.WriteLine("}"); Console.WriteLine("}");} static void Main() { //... //...} What do these empty lines serve for?

 Break long lines after punctuation  Indent the second line by single [Tab]  Do not additionally indent the third line  Examples: 21 DictionaryEntry newEntry = new DictionaryEntry (oldEntry.Key, oldEntry.Value); new DictionaryEntry (oldEntry.Key, oldEntry.Value); if (matrix[x, y] == 0 || matrix[x-1, y] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x, y+1] == 0) matrix[x, y+1] == 0) { …

22 if (matrix[x, y] == 0 || matrix[x-1, y] == 0 || matrix[x+1, y] == 0 || matrix[x, 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x, y+1] == 0) y-1] == 0 || matrix[x, y+1] == 0) { … if (matrix[x, y] == 0 || matrix[x-1, y] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x, y+1] == 0) matrix[x, y+1] == 0) { … DictionaryEntry newEntry = new DictionaryEntry (oldEntry = new DictionaryEntry (oldEntry.Key, oldEntry.Value);.Key, oldEntry.Value);

 In C# use single [Tab] after breaking a long line:  In JavaScript use double [Tab] in the carried long lines: 23 if (matrix[x, y] == 0 || matrix[x-1, y] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x, y+1] == 0) matrix[x, y+1] == 0){ matrix[x, y] == 1; matrix[x, y] == 1;} if (matrix[x, y] == 0 || matrix[x-1, y] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x+1, y] == 0 || matrix[x, y-1] == 0 || matrix[x, y+1] == 0) { matrix[x, y+1] == 0) { matrix[x, y] == 1; matrix[x, y] == 1;}

 All types of alignments are considered harmful  Alignments are hard-to-maintain!  Modifying one line of code shouldn’t require modifying several others  Incorrect examples: 24 int count = 0; DateTime date = DateTine.Now.Date; Student student = new Student(); List students = new List (); matrix[x, y] = 0; matrix[x + 1, y + 1] = 0; matrix[2 * x + y, 2 * y + x] = 0; matrix[x * y, x * y] = 0; Think about renaming Student to SchoolStudent

 Take advantage of your IDE to help formatting the code [Ctrl+K+D]  Automatic alignment  Indentation  Style Code analysis  Visual Studio – StyleCop   Eclipse – CheckStyle   JSHint, JSLint – JavaScript code analysis (all IDEs) 

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране

 C# Telerik Academy  csharpfundamentals.telerik.com csharpfundamentals.telerik.com  Telerik Software Academy  academy.telerik.com academy.telerik.com  Telerik Facebook  facebook.com/TelerikAcademy facebook.com/TelerikAcademy  Telerik Software Academy Forums  forums.academy.telerik.com forums.academy.telerik.com