Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer www.nakov.com.

Slides:



Advertisements
Similar presentations
Windows Basic and Dynamic Disk Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator Marian Marinov CEO of 1H Ltd.
Advertisements

HTML Forms, GET, POST Methods Tran Anh Tuan Edit from Telerik Academy
Amazon S 3, App Engine Blobstore, Google Cloud Storage, Azure Blobs Svetlin Nakov Telerik Software Academy academy.telerik.com.
RPN and Shunting-yard algorithm Ivaylo Kenov Telerik Software Academy academy.telerik.com Technical Assistant
Shortest paths in edge-weighted digraph Krasin Georgiev Technical University of Sofia g.krasin at gmail com Assistant Professor.
Telerik Software Academy Telerik School Academy.
Asynchronous Programming with C# and WinRT
Touch and Gestures with Xamarin Forms
Character sequences, C-strings and the C++ String class, Working with Strings Learning & Development Team Telerik Software Academy.
Hybrid or Native?! Doncho Minkov Telerik Software Academy Senior Technical Trainer
Done already for your convenience! Telerik School Academy Unity 2D Game Development.
Processing Sequences of Elements Telerik School Academy C# Fundamentals – Part 1.
With Mocha and Karma Telerik Academy Telerik Software Academy.
C# Fundamentals – Part I
NoSQL Concepts, Redis, MongoDB, CouchDB
The Business Plan and the Business Model Margarita Antonova Volunteer Telerik Academy academy.telerik.com Business System Analyst Telerik Corporation.
What are ADTs, STL Intro, vector, list, queue, stack Learning & Development Team Telerik Software Academy.
Making JavaScript code by template! Learning & Development Team Telerik Software Academy.
Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training Who, What, Why?
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Accessing SQL Server and MySQL – Live Demo Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Processing Matrices and Multidimensional Tables Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Reading and Writing Text Files Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Telerik Software Academy ASP.NET Web Forms.
Classical OOP in JavaScript Classes and stuff Telerik Software Academy
Using Selenium for Mobile Web Testing Powered by KendoUI Telerik QA Academy Atanas Georgiev Senior QA Engineer KendoUI Team.
NoSQL Concepts, Redis, MongoDB, CouchDB Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
New features: classes, generators, iterators, etc. Telerik Academy Plus JavaScript.Next.
Creating E/R Diagrams with SQL Server Management Studio and MySQL Workbench Svetlin Nakov Telerik Software Academy Manager Technical.
Throwing and Catching Exceptions Tran Anh Tuan Edit from Telerik Software Academy
Handling Errors during the Program Execution Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Loops, Conditional Statements, Functions Tran Anh Tuan Edit from Telerik Academy
Telerik Software Academy ASP.NET Web Forms.
Private/Public fields, Module, Revealing Module Learning & Development Team Telerik Software Academy.
Building Data-Driven ASP.NET Web Forms Apps Telerik Software Academy ASP.NET Web Forms.
Course Introduction Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
General and reusable solutions to common problems in software design Vasil Dininski Telerik Software Academy academy.telerik.com Intern at Telerik Academy.
Planning and Tracking Software Quality Yordan Dimitrov Telerik Corporation Team Leader, Team Pulse, Team Leader, Team Pulse, Telerik Corporation,
What you need to know Ivaylo Kenov Telerik Corporation Telerik Academy Student.
Data binding concepts, Bindings in WinJS George Georgiev Telerik Software Academy academy.telerik.com Technical Trainer itgeorge.net.
Pavel Kolev Telerik Software Academy Senior.Net Developer and Trainer
Objects, Properties, Primitive and Reference Types Learning & Development Team Telerik Software Academy.
How to Design Error Steady Code Ivaylo Bratoev Telerik Corporation
When and How to Refactor? Refactoring Patterns Alexander Vakrilov Telerik Corporation Senior Developer and Team Leader.
Defensive Programming, Assertions and Exceptions Designing Fault-Resistant Code SoftUni Team Technical Trainers Software University
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Free Training and Job for Software Engineers Svetlin Nakov, PhD Manager Technical Training Telerik Corp. Telerik Software Academy.
Access to known folders, using pickers, writing to and reading from files, caching files for future access George Georgiev Telerik Software Academy academy.telerik.com.
Doing the Canvas the "easy way"! Learning & Development Telerik Software Academy.
Creating and Running Your First C# Program Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical Training
Defensive Programming, Assertions and Exceptions Designing Error Steady Code SoftUni Team Technical Trainers Software University
Correctly Formatting the Source Code Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
The past, the present, the future Learning & Development Team Telerik Software Academy.
Connecting, Queries, Best Practices Tran Anh Tuan Edit from Telerik Software Academy
Processing Sequences of Elements Telerik Software Academy C# Fundamentals – Part 2.
Telerik JavaScript Framework Telerik Software Academy Hybrid Mobile Applications.
Building Rock-Solid Software Nikolay Kostov Telerik Software Academy academy.telerik.com Senior Software Developer and Technical Trainer
Telerik Software Academy Databases.
Things start to get serious Telerik Software Academy JavaScript OOP.
Learning & Development Mobile apps for iPhone & iPad.
Processing Matrices and Multidimensional Tables Telerik Software Academy C# Fundamentals – Part 2.
Nikolay Kostov Telerik Software Academy academy.telerik.com Team Lead, Senior Developer and Trainer
Functions and Function Expressions Closures, Function Scope, Nested Functions Telerik Software Academy
Implementing Control Logic in C# Svetlin Nakov Telerik Software Academy academy.telerik.com Manager Technical trainer
Inheritance, Abstraction, Encapsulation, Polymorphism Telerik Software Academy Mobile apps for iPhone & iPad.
Mocking tools for easier unit testing Telerik Software Academy High Quality Code.
What why and how? Telerik School Academy Unity 2D Game Development.
Windows Security Model Borislav Varadinov Telerik Software Academy academy.telerik.com System Administrator
Presentation transcript:

Learn to Design Error Steady Code Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer

1. What is Defensive Programming? 2. Assertions and Debug.Assert(…) 3. Exceptions Handling Principles 4. Error Handling Strategies 2

Using Assertions and Exceptions Correctly

 Similar to defensive driving – you are never sure what other drivers will do  Expect incorrect input and handle it correctly  Think not only about the usual execution flow, but consider also unusual situations 4

 “Garbage in  garbage out” – Wrong!  Garbage in  nothing out / exception out / error message out / no garbage allowed in  Check the values of all data from external sources (from user, file, internet, DB, etc.) 5

 Check method preconditions  Parameters, object internal state, etc.  Check method postconditions  Method output, expected internal state, etc. 6 string Substring(string str, int startIndex, int length) { REQUIRE(str != NULL); REQUIRE(str != NULL); REQUIRE(startIndex < str.Length); REQUIRE(startIndex < str.Length); REQUIRE(startIndex + count < str.Lenght); REQUIRE(startIndex + count < str.Lenght); string result = … string result = … ENSURE(result.Length == length); ENSURE(result.Length == length);} Check preconditions Main method logic Check postconditions

Checking Preconditions and Postconditions

 Assertion – a statement placed in the code that must always be true at that moment  Assertions are used during development  Removed in release builds  Assertions check for bugs in code 8 public double GetAverageStudentGrade() { Debug.Assert(studentGrades.Count > 0, Debug.Assert(studentGrades.Count > 0, "Student grades are not initialized!"); "Student grades are not initialized!"); return studentGrades.Average(); return studentGrades.Average();}

 Use assertions for conditions that should never occur in practice  Failed assertion indicates a fatal error in the program (usually unrecoverable)  Use assertions to document assumptions made in code (preconditions & postconditions) 9 private Student GetRegisteredStudent(int id) { Debug.Assert(id > 0); Debug.Assert(id > 0); Student student = registeredStudents[id]; Student student = registeredStudents[id]; Debug.Assert(student.IsRegistered); Debug.Assert(student.IsRegistered);}

 Failed assertion indicates a fatal error in the program (usually unrecoverable)  Avoid putting executable code in assertions  Won’t be compiled in production. Better use:  Assertions should fail loud  It is fatal error  total crash 10 Debug.Assert(PerformAction(), "Could not perform action"); bool actionedPerformed = PerformAction(); Debug.Assert(actionedPerformed, "Could not perform action");

Live Demo

Best Practices for Exception Handling

 Exceptions provide a way to inform the caller about an error or exceptional events  Can be caught and processed by the callers  Methods can throw exceptions: 13 public void ReadInput(string input) { if (input == null) if (input == null) { throw new ArgumentNullException("input"); } throw new ArgumentNullException("input"); } …}

 Use try-catch statement to handle exceptions:  You can use multiple catch blocks to specify handlers for different exceptions  Not handled exceptions propagate to the caller 14 void PlayNextTurn() { try try { readInput(input); readInput(input); … } catch (ArgumentException e) catch (ArgumentException e) { Console.WriteLine("Invalid argument!"); Console.WriteLine("Invalid argument!"); }} Exception thrown here The code here will not be executed

 Use finally block to execute code even if exception occurs (not supported in C++):  Perfect place to perform cleanup for any resources allocated in the try block 15 void PlayNextTurn() { try try { … } … } finally finally { Console.WriteLine("Hello from finally!"); Console.WriteLine("Hello from finally!"); }} Exceptions can be eventually thrown here The code here is always executed

 Use exceptions to notify the other parts of the program about errors  Errors that should not be ignored  Throw an exception only for conditions that are truly exceptional  Should I throw an exception when I check for user name and password?  better return false  Don’t use exceptions as control flow mechanisms 16

 Throw exceptions at the right level of abstraction 17 class Employee { … public TaxId public TaxId { get { throw new NullReferenceException(…); } { get { throw new NullReferenceException(…); }} class Employee { … public TaxId public TaxId { get { throw new EmployeeDataNotAvailable(…); } { get { throw new EmployeeDataNotAvailable(…); }}

 Use descriptive error messages  Incorrect example:  Example:  Avoid empty catch blocks 18 throw new Exception("Error!"); throw new ArgumentException("The speed should be a number " + "between " + MIN_SPEED + " and " + MAX_SPEED + "."); "between " + MIN_SPEED + " and " + MAX_SPEED + "."); try{ …} catch (Exception ex) {}

 Always include the exception cause when throwing a new exception 19 try{ WithdrawMoney(account, amount); WithdrawMoney(account, amount);} catch (DatabaseException dbex) { throw new WithdrawException(String.Format( throw new WithdrawException(String.Format( "Can not withdraw the amount {0} from acoount {1}", "Can not withdraw the amount {0} from acoount {1}", amount, account), dbex); amount, account), dbex);} We chain the original exception (the source of the problem)

 Catch only exceptions that you are capable to process correctly  Do not catch all exceptions!  Incorrect example:  What about OutOfMemoryException ? 20 try{ ReadSomeFile(); ReadSomeFile();}catch{ Console.WriteLine("File not found!"); Console.WriteLine("File not found!");}

 Have an exception handling strategy for all unexpected / unhandled exceptions:  Consider logging (e.g. Log4Net)  Display to the end users only messages that they could understand 21 or

Live Demo (Decompiling System.DateTime ) (Decompiling System.DateTime )

Assertions vs. Exceptions vs. Other Techniques

 How to handle errors that you expect to occur?  Depends on the situation:  Throw an exception (in OOP)  The most typical action you can do  Return a neutral value, e.g. -1 in IndexOf(…)  Return an error code (in old languages / APIs)  Display an error message in the UI  Return the same answer as the previous time  Log a warning message to a file  Crash / shutdown / reboot 24

 Exceptions are announcements about error condition or unusual event  Inform the caller about error or exceptional event  Can be caught and application can continue working  Assertions are fatal errors  Assertions always indicate bugs in the code  Can not be caught and processed  Application can’t continue in case of failed assertion  When in doubt  throw an exception 25

 Assertions in C# are rarely used  In C# prefer throwing an exception when the input data / internal object state are invalid  Exceptions are used in C# and Java instead of preconditions checking  Prefer using unit testing for testing the code instead of postconditions checking  Assertions are popular in C / C++  Where exceptions & unit testing are not popular  In JS there are no built-in assertion mechanism 26

 Choose your error handling strategy and follow it consistently  Assertions / exceptions / error codes / other  In C#,.NET and OOP prefer using exceptions  Assertions are rarely used, only as additional checks for fatal error  Throw an exception for incorrect input / incorrect object state / invalid operation  In JavaScript use exceptions: try-catch-finally  In non-OOP languages use error codes 27

 How will you handle error while calculating single pixel color in a computer game?  How will you handle error in financial software? Can you afford to lose money?  Correctness == never returning wrong result  Try to achieve correctness as a primary goal  Robustness == always trying to do something that will allow the software to keep running  Use as last resort, for non-critical errors 28

29 public string Substring(string str, int startIndex, int length) { if (str == null) if (str == null) { throw new NullReferenceException("Str is null."); throw new NullReferenceException("Str is null."); } if (startIndex >= str.Length) if (startIndex >= str.Length) { throw new ArgumentException( throw new ArgumentException( "Invalid startIndex:" + startIndex); "Invalid startIndex:" + startIndex); } if (startIndex + count > str.Length) if (startIndex + count > str.Length) { throw new ArgumentException("Invalid length:" + length); throw new ArgumentException("Invalid length:" + length); } … Debug.Assert(result.Length == length); Debug.Assert(result.Length == length);} Check the input and preconditions Perform the method main logic Check the postconditions

 Barricade your program to stop the damage caused by incorrect data  Consider same approach for class design  Public methods  validate the data  Private methods  assume the data is safe  Consider using exceptions for public methods and assertions for private 30 public methods / functions private methods / functions safe data

 Too much defensive programming is not good  Strive for balance  How much defensive programming to leave in production code?  Remove the code that results in hard crashes  Leave in code that checks for important errors  Log errors for your technical support personnel  See that the error messages you show are user- friendly 31

форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен 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# Николай Костов - блог за програмиране

 For the exercises use the Visual Studio solution "9. Assertions-and-Exceptions-Homework.zip". 9. Assertions-and-Exceptions-Homework.zip9. Assertions-and-Exceptions-Homework.zip 1. Add assertions in the code from the project " Assertions-Homework " to ensure all possible preconditions and postconditions are checked. 2. Add exception handling (where missing) and refactor all incorrect error handling in the code from the " Exceptions-Homework " project to follow the best practices for using exceptions. 33

 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