Quality Code academy.zariba.com 1. Lecture Content 1.Software Quality 2.Code Formatting 3.Correct Naming 4.Documentation and Comments 5.Variables, Expressions.

Slides:



Advertisements
Similar presentations
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation For C# Developers.
Advertisements

Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Corporation
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation
Documentation 1 Comprehending the present – Investing in the future.
Software Engineering and Design Principles Chapter 1.
Chapter 3 Data Abstraction: The Walls. © 2005 Pearson Addison-Wesley. All rights reserved3-2 Abstract Data Types Modularity –Keeps the complexity of a.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Fall 2007CS 2251 Software Engineering Intro. Fall 2007CS 2252 Topics Software challenge Life-cycle models Design Issues Documentation Abstraction.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 1 Principles of Programming and Software Engineering.
C++ for Engineers and Scientists Third Edition
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
VB .NET Programming Fundamentals
Algorithm Programming Coding Advices Bar-Ilan University תשס " ו by Moshe Fresko.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Games and Simulations O-O Programming in Java The Walker School
Fundamentals of Python: From First Programs Through Data Structures
JS Arrays, Functions, Events Week 5 INFM 603. Agenda Arrays Functions Event-Driven Programming.
Fundamentals of Python: First Programs
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
Recap (önemli noktaları yinelemek) from last week Paradigm Kay’s Description Intro to Objects Messages / Interconnections Information Hiding Classes Inheritance.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
The Java Programming Language
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Syntax and Style JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
SE: CHAPTER 7 Writing The Program
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Best Practices. Contents Bad Practices Good Practices.
Programming Logic and Design Using Methods. 2 Objectives Review how to use a simple method with local variables and constants Create a method that requires.
Java The Java programming language was created by Sun Microsystems, Inc. It was introduced in 1995 and it's popularity has grown quickly since A programming.
Refactoring 2. Admin Blackboard Quiz Acknowledgements Material in this presentation was drawn from Martin Fowler, Refactoring: Improving the Design of.
REFACTORINGREFACTORING. Realities Code evolves substantially during development Requirements changes 1%-4% per month on a project Current methodologies.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Writing Maintainable code Dr. Susan McKeever DT228/3 GUI Programming.
Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation
© 2006 Pearson Addison-Wesley. All rights reserved2-1 Chapter 2 Principles of Programming & Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
Final Review. From ArrayLists to Arrays The ArrayList : used to organize a list of objects –It is a class in the Java API –the ArrayList class uses an.
GoodOO Programming Practice in Java © Allan C. Milne v
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
Refactoring Agile Development Project. Lecture roadmap Refactoring Some issues to address when coding.
Written by: Dr. JJ Shepherd
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
Principles of Programming. Achieving an Object-Oriented Design  Abstraction and Information Hiding  Object-Oriented Design  Functional Decomposition.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 7 Using Methods.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Principles of Programming & Software Engineering
Working with Java.
Principles of Programming and Software Engineering
Scripts & Functions Scripts and functions are contained in .m-files
An overview of Java, Data types and variables
Chapter 1: Computer Systems
Focus of the Course Object-Oriented Software Development
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Quality Code academy.zariba.com 1

Lecture Content 1.Software Quality 2.Code Formatting 3.Correct Naming 4.Documentation and Comments 5.Variables, Expressions and Constants 6.Control Statements – if, for, while, foreach… 7.High-qualityMmethods 8.High-quality Classes 9.Exceptions and Defensive Programming 10.Code optimization 2

1. Software Quality External quality Does the software behave correctly? Are the produced results correct Does the software run fast? Is the software UI easy to use? Is the code secure enough? Internal quality Is the code easy to read and understand? Is the code well structured? Is the code easy to modify? Are there any repetitions? 3

1. Software Quality High-quality programming code: Easy to read, understand, modify and maintain Behaves correctly – well tested Well architectured and designed Well documented (self-documenting code) Well formatted Strong cohesion and loose coupling at all levels Good naming 4

2. Code Formatting 1. Put { and } on a line under the corresponding parent block 2. Indent the block contents by a single tab 3. Use empty lines for separation between methods 4. Brackets in the method declaration should be formatted without spaces e.g. MethodName(int num) 5. The same applies to if conditions and for loops 6. Separate paramters by comma, followed by space 7. Use empty lines to separate logically related sequences of code 8. Always use { } brackets after if, for, while, foreach etc! 9. Break long lines after punctuation, or store complicated checks in a variable 5

3. Correct Naming 1.Always use English! 2.Avoid abbreviations (e.g. scrpCnt vs scriptsCount) 3.Use meaningful names. What the variable/class/ method used for? 4.Meaningful names also depend on the context in which they are used e.g. Generate() in a class GetAllPaths. e.g. Generate() in the Program class 5.Don’t use “fake meaningful” names: e.g. Topic6Exercise12, LoopsExercise12, Problem7 etc. 6

3. Correct Naming 1.Use PascalCase for Methods, Classes, Structures, Properties, Enums, Namespaces etc. 2.Use camelCase for variables 3.Name interfaces starting with “I” and add ”able” at the end where possible 4.If you are using your own exceptions, add “Exception” to the end of your class e.g. FileNotFoundException 5.The same Applies for Delegates or EventHandlers 6.Names can be as long as required – do not abbreviate and do not overcomplicate names. 7.Boolean variable names should imply true or false 8.Always use positive Boolean variable names, e.g. if ( ! notFound ) 7

3. Correct Naming 1.Use CAPITAL_LETTERS for const fields 2.Use PascalCase for readonly fields 3.Never give misleading names 4.Don’t use numbers in the names except when the number is part of the name. 8

4. Documentation and Comments Documentation can be external: 1.At a higher level, compared to the code 2.Detailed with problem definition, requirements, architecture, design, project plans, test plans, sound, gameplay, modes, graphics etc. Or internal: 1.At a lower level – explains a class, method or piece of code 9

4. Documentation and Comments 1.Do not comment every single line in your code – most lines of code (if well-written) are obvious and do not need further explanation 2.Effective comments do not repeat the code 3.If you are writing quality code it will not need to be commented – self-documenting 4.To write self-documenting code you should make it self- explainable, easy to read and write 5.Do not explain bad code – rewrite it! 6.Focus comments on why? rather than how? 7.Comment on workarounds, hacks, bugs, TODO 8.Write summaries in C# 10

4. Self-Documenting Code checklist 1.Does the interface present a consistent abstraction? 2.Does the interface make it obvious how you should use the class? 3.Is the class well-named? Does the name describe the purpose? 4.Do you reuse code in your class hierarchy? 5.Does each each routine’s name describe exactly what the method does? 6.Does each method perform one well-defined task? 7.Are type names descriptive enough? 8.Are variables used only for a single purpose related to their name? 9.Does naming conventions distinguish among types? 11

5. Variables, Expressions and Constants 1.Initialize all variables before their first usage! 2.Pay special attentions to counters, sums, products etc. 3.Check whether the data is in the correct format for your purpose 4.Don’t define unused variables 5.ALWAYS assign the result of a method in some variable before returning it 6.Think about the “visibility” of your variables – public, protected, internal, private … Always try to maximally reduce visibility 7.Always initialize variables, right before they are used 8.Do not declare variables on the same line 9.Variables should have a single purpose 10.If you cannot choose a god name for your variable, think of Avoid complex expressions – separate indexes and Boolean checks in variables 12.Do not use magic numbers – use constants 12

6. Control statements 1.Always use { and } for control statements, even if the body is on a single line! 2.Always put the normal ( expected ) conditions first 3.Avoid comparing to true or false e.g. (HasErrors() == true) 4.Always consider the else case 5.Avoid double negation e.g. (!HasNoError) 6.Do not copy paste code in if-else statements 7.Do not use complicated if conditions – simplify in variables or methods 8.Use brackets to improve readability and maintainability of complicated conditions 9.Avoid deep nesting of statements – more than 3 is too deep 13

6. Control statements 1.Use for loop to repeat some block of code a certain number of times 2.Use foreach loop to process each element of a collection 3.Use while/do-while when you do not know how many times a block should be repeated 4.Keep loops simple 5.Avoid empty loops 6.Don’t change the value of a for loop to stop the loop – use while instead 7.Avoid break/continue if you can. Use while + condition instead 8.Do not EVER use “go-to” 14

7. High-quality Methods 1.Using methods reduces complexity, improves readability, helps avoiding repetition 2.Methods hide implementation details and increase the level of abstraction 3.Methods should consider all possible scenarios of the given task they perform 4.Methods should have strong cohesion and loose coupling 5.Do not modify method parameters. Create new variables if needed 6.Limit the number of parameters to 7 (+/- 2) 7.Put the most important parameters first 15

8. High-quality Classes 1.Strong cohesion, loose coupling 2.Use inheritance to reuse repeating code – data and logic, and to simplify maintenance 3.Polymorphism is a tool to enable code reuse – implemented via virtual, abstract or interfaces methods 4.Present a consistent level of abstraction 5.Minimize visibility of classes and members – encapsulation 6.Use design patterns (next lecture) 7.Use namespaces to structure your application design and architecture 8.Never use plural noun as a class name unless they hold some kind of collection – e.g. public class Teachers, e.g. public class GameFieldConstants 9.Do not throw exceptions without parameters 10.Always use this to access members within the class! 16

8. High-quality Classes 1.Don’t use magic numbers 2.Call the base constructer to reuse code 3.Do not copy-paste code – properties, methods or fields. This is always a bad practice 4.The base class should never know about its children 5.Do not break encapsulation 6.Move repeating code upper in the hierarchy 7.Extract abstract classes from classes 17

9. Exceptions and Defensive Programming 1.Similar to defensive driving, you are never sure what other drivers will do 2.The same applies to programming – expect incorrect input and handle it correctly 3.Consider unusual situations, parameters etc. 4.Protect from invalid input 5.Decode how to handle bad inputs – return neutral value, substitute with valid data, throw an exception, display error message etc. 6.Use try-catch to handle exceptions 7.Use finally block to always execute need code if exception occurs 8.Use descriptive error messages 9.Always try to keep the software running 18

10. Code Optimization 1.Remember! “Premature optimization is the root of all evil” Donald Knuth 2.Often the code quality is decreased to increase the performance Code Tuning Myths 1.“Reducing the lines of code improves the speed” 2.“A fast program is just as important as a correct one” 3.“Certain mathematical operations are faster than others” e.g. * and +, or multiplying and bitwise operations 4.“You should optimize as you go” 5.Junior developers think that selection sort is slow. Is this correct? 19

10. Systematic Tuning Steps 1.Assess the problem and establish numeric values that categorize acceptable behavior 2.Measure the performance before modification 3.Identify the part of the system that is critical for improving the performance (this is called a bottleneck) 4.Modify the part of the system to remove the bottleneck 5.Measure the performance of the system after modification 6.If the modification makes the performance better, use it 7.If not, remove it 8.Rinse and repeat 20

10. Optimization Tips C# is fast (unlike Java). It is a bit slower than C and C++. Tips: 1.Static fields and methods are faster than instance fields 2.It is faster to minimize method arguments 3.Constants are fast 4.Some switches are faster than if statements 5.Using two-dimensional arrays is relatively slow 6.StringBuilder can considerably improve performance. Char[ ] is the fastest way to build a string, but has limitations 7.Simple array T[ ] is always faster than List 8.Use efficient data structures e.g. HashSet, Dictionary 9. For loops are faster than foreach loops 10.Structs are slower than classes 11.It is better to work with a char instead of a single-char string 21

22 References

23 Zariba Academy Questions