SlideShare a Scribd company logo
1 of 62
Download to read offline
Game Programming
Style & Design Principles
Nick Prühs
Objectives
• To get an idea of good code style and structure in general
• To understand the importance of consistent naming and code
conventions
• To learn how to property design types and members
2 / 61
What is “good code”?
• Straight-forward and obvious
 You’ll spend far more time reading code than writing.
 You’ll spend much time reading code you didn’t write.
• Loose coupling
• Well tested
• Reused code
• Efficient (?)
3 / 61
How to achieve “good code”?
Source: http://xkcd.com/844/ 4 / 61
How to achieve “good code”?
• Pair programming
• Code Reviews
• Client-first programming, Test-driven development
• Refactoring
• Unit Testing
• Great tools
• Static code analysis
5 / 61
Naming Conventions
• Greatly increase readability and usability
• Differ from language to language
 We’ll take a look at C#, but many guidelines apply to other
languages as well.
6 / 61
C# Capitalization
• PascalCasing
 Namespaces
 Types
 Member Names
• camelCasing
 Method Parameter Names
7 / 61
Capitalization of Acronyms
• Both characters of two-character acronyms
 Example: IO
• Only first character of three-character acronyms
 Example: Xml, Html
• Never for camelCased identifiers (such as parameters)
8 / 61
Word Choice
• Easily readable:
 HorizontalAlignment instead of AlignmentHorizontal
• Favor readability over brevity:
 CanScrollHorizontally instead of ScrollableX
• No underscores
• No Hungarian notation
• No abbreviations
 Abbreviations don’t work well with IDEs.
• Semantic names
 GetLength instead of GetInt
9 / 61
Namespace Names
<Company>.<Product>.<Feature>.<SubNamespace>
• Plural names where appropriate
 Example: System.Collections
• Don’t use the same name for namespace and type
• Don’t use common names such as Element, Component
10 / 61
Type Names
• Nouns for classes and structs
 Example: List, Vector
• Derived classes can end with name of base class
 Example: ArgumentException
• Adjectives for interfaces
• Prefix interface names with ‘I’
 Example: IComparable
• Use descriptive names for type-parameters
 Dictionary<TKey, TValue> instead of Dictionary<K, V>
• Singular names for non-flags enums
 Color instead of Colors
11 / 61
Member Names
• Verbs for methods and events
 Example: AddComponent, ComponentAdded
• Nouns or adjectives for fields
 Example: Duration, Invulnerable
• Nouns for properties
• Plurals for collections
 Items instead of ItemList
12 / 61
Boolean Trap #1
• Be positive!
 Enabled = true instead of Disabled = false
 CaseSensitive = true instead of CaseInsensitive = false
13 / 61
Tabs vs. Spaces
• Holy war between two fractions of programmers
• Some are for tabs…
 Seperation of visual representation from data
 Customizable
 Faster
 Specifically meant for indentation
• … while others are for spaces.
 Always one column
 Supported by IDEs anyway
14 / 61
Tabs vs. Spaces
Most important rule:
Stay consistent.
… or it might even blow up your version control.
15 / 61
Tabs vs. Spaces
„That said, only a moron would use tabs to format their code.”
- Jeff Atwood
16 / 61
Type Design – Class vs. Struct
Make a type a struct instead of a class if it…
• Logically represents a single value,
• Has a valid state if all data is set to zero,
• Has an instance size < 16 bytes,
• Is Immutable, and
• Won’t have to be boxed frequently
17 / 61
Type Design – Class vs. Struct
Make a type a struct instead of a class if it…
• Logically represents a single value,
• Has a valid state if all data is set to zero,
 This is the case for array initialization, for example.
• Has an instance size < 16 bytes,
• Is Immutable, and
• Won’t have to be boxed frequently
18 / 61
Type Design – Class vs. Struct
Make a type a struct instead of a class if it…
• Logically represents a single value,
• Has a valid state if all data is set to zero,
• Has an instance size < 16 bytes,
 Value type assignments copy all values
• Is Immutable, and
• Won’t have to be boxed frequently
19 / 61
Type Design – Class vs. Struct
Make a type a struct instead of a class if it…
• Logically represents a single value,
• Has a valid state if all data is set to zero,
• Has an instance size < 16 bytes,
• Is Immutable, and
 Passing (and returning) by value implicitly creates a copy
 Value types that can be changed will be confusing!
• Won’t have to be boxed frequently
20 / 61
Type Design – Class vs. Struct
Make a type a struct instead of a class if it…
• Logically represents a single value,
• Has a valid state if all data is set to zero,
• Has an instance size < 16 bytes,
• Is Immutable, and
• Won’t have to be boxed frequently
 Happens when they’re cast to a interface
 Allocated on heap and garbage collected, then!
21 / 61
Type Design – Class vs. Interface
• Use interfaces for polymorphic value type hierarchies
 Example: IComparable, IConvertible
• If an interface does more than exactly one thing, that’s a warning
sign.
22 / 61
Type Design – Class vs. Interface
23 / 61
Type Design – Class vs. Interface
• Favor (abstract) classes over interfaces
 Adding members to an interface is a breaking change!
 Seperating of contract from implementation with an interface is a
myth…
 … whereas doing so with an abstract class is not.
24 / 61
Type Design – Enums
• Favor enums over static constants
• Provide a zero value for simple enums (e.g. None)
• Don’t use enums for open sets
• Don’t use flag enums if some combinations can be invalid
25 / 61
Member Design – Property vs. Method
Make a member a method instead of a property if it…
• Is significantly slower than a field access would be,
• Is a conversion operation (such as ToString),
• Returns a different result each time it is called,
• Has an observable side effect,
• Returns a copy of an internal state, or
• Returns an array
26 / 61
Member Design – Properties
• Preserve previous value if a property setter throws an exception
• Don’t throw exceptions from property getters.
 Use a method in that case.
27 / 61
Gotcha!
Don‘t throw exceptions from
static constructors!
28 / 61
Member Design – Methods
• Shorter overloads should simply call longer ones
 public int IndexOf(string s)
 public int IndexOf(string s, int startIndex)
 public int IndexOf(string s, int startIndex, int count)
• Use descriptive parameter names
• Avoid varying names or inconsistent ordering
• Make only the longest overload virtual
• Allow null to be passed for optional parameters
 Default arguments are not CLS-compliant!
29 / 61
Member Design – Extensions
Use an extension method, if ...
• It adds functionality relevant to every implementation of an interface,
or
• An instance method would introduce unwanted dependencies
30 / 61
Member Design – Operator Overloads
• Make sense for types that feel like primitive types
• Should be defined symmetrically
• Provide methods with friendly names, as well
 Some languages don‘t support operator overloads.
31 / 61
Member Design – Parameters
• Use the least derived required paramter type
• Validate all arguments
• params keyword is useful for arbitrary (but small) numbers of
parameters
 Pay attention on parameter order
 Be aware that null is a valid params array argument
32 / 61
Boolean Trap #2
Guess what this code means?
widget.repaint(false);
33 / 61
Boolean Trap #2
Guess what this code means?
widget.repaint(false);
• Function argument is called immediate
 true means immediate painting
 false means deferred painting
• Better use method overloads or enums!
34 / 61
Boolean Trap #3
Guess what this code means?
var opacitySlider = new Slider(true);
35 / 61
Boolean Trap #3
Guess what this code means?
var opacitySlider = new Slider(true);
• Function argument is called horizontal
 true means horizontal slider
 false means vertical slider
• Better use subclasses!
 „Heck, who knows someday you’ll need a
diagonal slider!”
36 / 61
Boolean Trap #4
Guess what this code means?
stackView.updateHeight(false);
37 / 61
Boolean Trap #4
Guess what this code means?
stackView.updateHeight(false);
• Immediate or not?
• Don‘t update height?
• Update width???
38 / 61
Boolean Trap #5
The more, the merrier!
event.initKeyEvent("keypress", true, true,
null, null, false, false, false, false, 9,
0);
39 / 61
Exceptions
• Help you deal with any unexpected or exceptional situations that
occur when a program is running
• Exceptions are types that all ultimately derive from
System.Exception.
• Generated by the common language runtime (CLR), by the .NET
Framework or any third-party libraries, or by application code.
40 / 61
Benefits of Exceptions
• Integrate well with object-oriented languages
 Does not require changing the method signature
• Can’t be ignored, whereas error codes can
• Exception objects contain detailed information about the error, such
as the state of the call stack and a text description of the error.
• Possibility of defining an Unhandled Exception Filter (UEF)
• Supported by debuggers
41 / 61
Gotcha!
Don’t return error codes!
42 / 61
Exception Design
• Exceptions provide consistent error reporting
 No bool return value required
 No HRESULT
 No GetLastError
• Don’t return error codes.
 If your error code is for the developer, add additional information
to the exception message instead.
 If your error code is for the exception handler, add a new
exception type instead.
43 / 61
Throwing Exceptions
• Throw the most specific exception that makes sense.
 ArgumentException
 ArgumentNullException
• Provide rich and meaningful error messages.
• Introduce a new exception type only if it should be handled
differently than existing exception types.
44 / 61
Handling Exceptions
• Use a try block around the statements that might throw exceptions.
• Once an exception occurs in the try block, the flow of control jumps
to the first associated exception handler that is present anywhere in
the call stack.
• If no exception handler for a given exception is present, the program
stops executing with an error message.
45 / 61
Handling Exceptions
Unhandled exception reported by Microsoft Visual Studio 2012
46 / 61
Exception Best Practice
47 / 61
Exception Best Practice
• Do not catch an exception unless you can handle it and leave the
application in a known state.
• Do not catch non-specific exceptions, such as System.Exception.
• Use a finally block to release resources, for example to close any
streams or files that were opened in the try block.
48 / 61
Gotcha!
Never use empty catch blocks!
49 / 61
Common Exceptions
• Thrown by your application or the framework
 InvalidOperationException
 ArgumentException
 ArgumentNullException
 ArgumentOutOfRangeException
• Thrown by the CLR
 NullReferenceException
 IndexOfOutRangeException
 StackOverflowException
 OutOfMemoryException
50 / 61
Collection Design
• Inherit from Collection<T>, ReadOnlyCollection<T> or
KeyedCollection<TKey, TItem>
• Implement IEnumerable<T>
 Allows foreach iteration of your collection.
• Implement ICollection<T> or IList<T> where it makes sense
• Implement an indexer where it makes sense
51 / 61
Gotcha!
Don’t implement ICloneable!
52 / 61
The interface ICloneable
• Contract doesn’t define whether cloning an object returns a deep or
shallow copy
• Define your own Clone method if required, without implementing the
interface
53 / 61
Hint
Implement IEquatable<T>
on value types!
54 / 61
The interface IEquatable
• Defines a method for determining equality of object instances
• Implement on value types to prevent boxing
• Override Object.Equals, == and != operators as well
55 / 61
Hint
Implement IComparable<T>
where it makes sense!
56 / 61
The interface IComparable
• Defines a type-specific comparison method for ordering or sorting
type instances.
• Override <, >, <= and >= operators as well
57 / 61
Equals, GetHashCode & ToString
• If Object.Equals returns true for any two objects, GetHashCode must
return the same value for these objects.
 Dictionaries, which are implemented as Hashtables in .NET, will
place two equal objects in different buckets otherwise, and
lookup might fail in that case.
• Override ToString wherever it makes sense.
 Provides more useful debug output.
58 / 61
Gotcha!
GetHashCode must return the
exactly same value across the
lifetime of an object!
59 / 61
References
• Stack Overflow. What does a good programmer's code look like?
http://stackoverflow.com/questions/366588/what-does-a-good-
programmers-code-look-like, April 2017.
• Cwalina, Abrams. Framework Design Guidelines. 2nd Edition. Addison-
Wesley, 2011.
• Hidayat. hall of api shame: boolean trap.
http://ariya.ofilabs.com/2011/08/hall-of-api-shame-boolean-trap.html,
August 24, 2011.
• Atwood. Death to the Space Infidels!
http://www.codinghorror.com/blog/2009/04/death-to-the-space-
infidels.html, April 13, 2009.
• MSDN. System Namespace. https://msdn.microsoft.com/en-
us/library/System(v=vs.110).aspx, April 2017.
60 / 61
Thank you!
http://www.npruehs.de
https://github.com/npruehs
@npruehs
nick.pruehs@daedalic.com
5 Minute Review Session
• Name a few characteristics of good code!
• How can you achieve good code?
• Tabs or spaces?
• When should you use a struct instead of a class?
• When should you use a method instead of a property?
• Name the three common interfaces and base classes that can be used
for collections in .NET!
• What is the main purpose of the interface IEquatable?
• What is the main purpose of the interface IComparable?
• How are Equals and GetHashCode related to each other?

More Related Content

What's hot

Game Programming 06 - Automated Testing
Game Programming 06 - Automated TestingGame Programming 06 - Automated Testing
Game Programming 06 - Automated TestingNick Pruehs
 
Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Nick Pruehs
 
Game Programming 05 - Development Tools
Game Programming 05 - Development ToolsGame Programming 05 - Development Tools
Game Programming 05 - Development ToolsNick Pruehs
 
Eight Rules for Making Your First Great Game
Eight Rules for Making Your First Great GameEight Rules for Making Your First Great Game
Eight Rules for Making Your First Great GameNick Pruehs
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - ShadersNick Pruehs
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Thibaud Desodt
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsNick Pruehs
 
Practical Malware Analysis: Ch 15: Anti-Disassembly
Practical Malware Analysis: Ch 15: Anti-DisassemblyPractical Malware Analysis: Ch 15: Anti-Disassembly
Practical Malware Analysis: Ch 15: Anti-DisassemblySam Bowne
 
Game development (Game Architecture)
Game development (Game Architecture)Game development (Game Architecture)
Game development (Game Architecture)Rajkumar Pawar
 
Preventing Complexity in Game Programming
Preventing Complexity in Game ProgrammingPreventing Complexity in Game Programming
Preventing Complexity in Game ProgrammingYaser Zhian
 
Initial design (Game Architecture)
Initial design (Game Architecture)Initial design (Game Architecture)
Initial design (Game Architecture)Rajkumar Pawar
 
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...Gerke Max Preussner
 
[Ubisoft] Perforce Integration in a AAA Game Engine
[Ubisoft] Perforce Integration in a AAA Game Engine[Ubisoft] Perforce Integration in a AAA Game Engine
[Ubisoft] Perforce Integration in a AAA Game EnginePerforce
 
Scrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small TeamsScrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small TeamsNick Pruehs
 
CNIT 126: 8: Debugging
CNIT 126: 8: DebuggingCNIT 126: 8: Debugging
CNIT 126: 8: DebuggingSam Bowne
 
Debugging multiplayer games
Debugging multiplayer gamesDebugging multiplayer games
Debugging multiplayer gamesMaciej Siniło
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitweili_at_slideshare
 
Software Security : From school to reality and back!
Software Security : From school to reality and back!Software Security : From school to reality and back!
Software Security : From school to reality and back!Peter Hlavaty
 
Steelcon 2014 - Process Injection with Python
Steelcon 2014 - Process Injection with PythonSteelcon 2014 - Process Injection with Python
Steelcon 2014 - Process Injection with Pythoninfodox
 

What's hot (20)

Game Programming 06 - Automated Testing
Game Programming 06 - Automated TestingGame Programming 06 - Automated Testing
Game Programming 06 - Automated Testing
 
Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)Component-Based Entity Systems (Demo)
Component-Based Entity Systems (Demo)
 
Game Programming 05 - Development Tools
Game Programming 05 - Development ToolsGame Programming 05 - Development Tools
Game Programming 05 - Development Tools
 
Eight Rules for Making Your First Great Game
Eight Rules for Making Your First Great GameEight Rules for Making Your First Great Game
Eight Rules for Making Your First Great Game
 
Game Programming 12 - Shaders
Game Programming 12 - ShadersGame Programming 12 - Shaders
Game Programming 12 - Shaders
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
 
School For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine BasicsSchool For Games 2015 - Unity Engine Basics
School For Games 2015 - Unity Engine Basics
 
Practical Malware Analysis: Ch 15: Anti-Disassembly
Practical Malware Analysis: Ch 15: Anti-DisassemblyPractical Malware Analysis: Ch 15: Anti-Disassembly
Practical Malware Analysis: Ch 15: Anti-Disassembly
 
Game development (Game Architecture)
Game development (Game Architecture)Game development (Game Architecture)
Game development (Game Architecture)
 
Preventing Complexity in Game Programming
Preventing Complexity in Game ProgrammingPreventing Complexity in Game Programming
Preventing Complexity in Game Programming
 
Initial design (Game Architecture)
Initial design (Game Architecture)Initial design (Game Architecture)
Initial design (Game Architecture)
 
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
West Coast DevCon 2014: Concurrency & Parallelism in UE4 - Tips for programmi...
 
[Ubisoft] Perforce Integration in a AAA Game Engine
[Ubisoft] Perforce Integration in a AAA Game Engine[Ubisoft] Perforce Integration in a AAA Game Engine
[Ubisoft] Perforce Integration in a AAA Game Engine
 
Scrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small TeamsScrum - but... Agile Game Development in Small Teams
Scrum - but... Agile Game Development in Small Teams
 
CNIT 126: 8: Debugging
CNIT 126: 8: DebuggingCNIT 126: 8: Debugging
CNIT 126: 8: Debugging
 
Debugging multiplayer games
Debugging multiplayer gamesDebugging multiplayer games
Debugging multiplayer games
 
Software testing
Software testingSoftware testing
Software testing
 
An Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnitAn Introduction to Unit Test Using NUnit
An Introduction to Unit Test Using NUnit
 
Software Security : From school to reality and back!
Software Security : From school to reality and back!Software Security : From school to reality and back!
Software Security : From school to reality and back!
 
Steelcon 2014 - Process Injection with Python
Steelcon 2014 - Process Injection with PythonSteelcon 2014 - Process Injection with Python
Steelcon 2014 - Process Injection with Python
 

Viewers also liked

Game Programming 03 - Git Flow
Game Programming 03 - Git FlowGame Programming 03 - Git Flow
Game Programming 03 - Git FlowNick Pruehs
 
Game Programming 02 - Component-Based Entity Systems
Game Programming 02 - Component-Based Entity SystemsGame Programming 02 - Component-Based Entity Systems
Game Programming 02 - Component-Based Entity SystemsNick Pruehs
 
Game Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance OptimizationGame Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance OptimizationNick Pruehs
 
What Would Blizzard Do
What Would Blizzard DoWhat Would Blizzard Do
What Would Blizzard DoNick Pruehs
 
Tool Development A - Git
Tool Development A - GitTool Development A - Git
Tool Development A - GitNick Pruehs
 
Game Programming 11 - Game Physics
Game Programming 11 - Game PhysicsGame Programming 11 - Game Physics
Game Programming 11 - Game PhysicsNick Pruehs
 
Game Models - A Different Approach
Game Models - A Different ApproachGame Models - A Different Approach
Game Models - A Different ApproachNick Pruehs
 
Designing an actor model game architecture with Pony
Designing an actor model game architecture with PonyDesigning an actor model game architecture with Pony
Designing an actor model game architecture with PonyNick Pruehs
 
Entity System Architecture with Unity - Unite Europe 2015
Entity System Architecture with Unity - Unite Europe 2015Entity System Architecture with Unity - Unite Europe 2015
Entity System Architecture with Unity - Unite Europe 2015Simon Schmid
 
ECS architecture with Unity by example - Unite Europe 2016
ECS architecture with Unity by example - Unite Europe 2016ECS architecture with Unity by example - Unite Europe 2016
ECS architecture with Unity by example - Unite Europe 2016Simon Schmid
 
Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016
Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016
Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016Simon Schmid
 
Game Programming 01 - Introduction
Game Programming 01 - IntroductionGame Programming 01 - Introduction
Game Programming 01 - IntroductionNick Pruehs
 

Viewers also liked (13)

Game Programming 03 - Git Flow
Game Programming 03 - Git FlowGame Programming 03 - Git Flow
Game Programming 03 - Git Flow
 
Game Programming 02 - Component-Based Entity Systems
Game Programming 02 - Component-Based Entity SystemsGame Programming 02 - Component-Based Entity Systems
Game Programming 02 - Component-Based Entity Systems
 
Game Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance OptimizationGame Programming 13 - Debugging & Performance Optimization
Game Programming 13 - Debugging & Performance Optimization
 
What Would Blizzard Do
What Would Blizzard DoWhat Would Blizzard Do
What Would Blizzard Do
 
Tool Development A - Git
Tool Development A - GitTool Development A - Git
Tool Development A - Git
 
Game Programming 11 - Game Physics
Game Programming 11 - Game PhysicsGame Programming 11 - Game Physics
Game Programming 11 - Game Physics
 
Game Models - A Different Approach
Game Models - A Different ApproachGame Models - A Different Approach
Game Models - A Different Approach
 
Designing an actor model game architecture with Pony
Designing an actor model game architecture with PonyDesigning an actor model game architecture with Pony
Designing an actor model game architecture with Pony
 
Entity System Architecture with Unity - Unite Europe 2015
Entity System Architecture with Unity - Unite Europe 2015Entity System Architecture with Unity - Unite Europe 2015
Entity System Architecture with Unity - Unite Europe 2015
 
ECS architecture with Unity by example - Unite Europe 2016
ECS architecture with Unity by example - Unite Europe 2016ECS architecture with Unity by example - Unite Europe 2016
ECS architecture with Unity by example - Unite Europe 2016
 
Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016
Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016
Clean, fast and simple with Entitas and Unity - Unite Melbourne 2016
 
AAA Automated Testing
AAA Automated TestingAAA Automated Testing
AAA Automated Testing
 
Game Programming 01 - Introduction
Game Programming 01 - IntroductionGame Programming 01 - Introduction
Game Programming 01 - Introduction
 

Similar to Game Programming 04 - Style & Design Principles

Style & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureStyle & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureNick Pruehs
 
Getting started with CATIA V5 Macros
Getting started with CATIA V5 MacrosGetting started with CATIA V5 Macros
Getting started with CATIA V5 MacrosEmmett Ross
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"Kazuhiro Sera
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler ConstructionAhmed Raza
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programmingJuggernaut Liu
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytechyannick grenzinger
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design GuidelinesMohamed Meligy
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityJaime Jorge
 

Similar to Game Programming 04 - Style & Design Principles (20)

Style & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & StructureStyle & Design Principles 01 - Code Style & Structure
Style & Design Principles 01 - Code Style & Structure
 
Getting started with CATIA V5 Macros
Getting started with CATIA V5 MacrosGetting started with CATIA V5 Macros
Getting started with CATIA V5 Macros
 
Learning from "Effective Scala"
Learning from "Effective Scala"Learning from "Effective Scala"
Learning from "Effective Scala"
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
C for Engineers
C for EngineersC for Engineers
C for Engineers
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
 
Software Craftmanship - Cours Polytech
Software Craftmanship - Cours PolytechSoftware Craftmanship - Cours Polytech
Software Craftmanship - Cours Polytech
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design Guidelines
 
Code Metrics
Code MetricsCode Metrics
Code Metrics
 
Csharp
CsharpCsharp
Csharp
 
Annotations
AnnotationsAnnotations
Annotations
 
Complete Java Course
Complete Java CourseComplete Java Course
Complete Java Course
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
Scala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and qualityScala Bay Meetup - The state of Scala code style and quality
Scala Bay Meetup - The state of Scala code style and quality
 
Apex code (Salesforce)
Apex code (Salesforce)Apex code (Salesforce)
Apex code (Salesforce)
 
Generics
GenericsGenerics
Generics
 

More from Nick Pruehs

Unreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual EffectsUnreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual EffectsNick Pruehs
 
Unreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceUnreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceNick Pruehs
 
Unreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesUnreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesNick Pruehs
 
Unreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - GameplayUnreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - GameplayNick Pruehs
 
Unreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorUnreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorNick Pruehs
 
Unreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkNick Pruehs
 
Game Programming - Git
Game Programming - GitGame Programming - Git
Game Programming - GitNick Pruehs
 
Tool Development 10 - MVVM, Tool Chains
Tool Development 10 - MVVM, Tool ChainsTool Development 10 - MVVM, Tool Chains
Tool Development 10 - MVVM, Tool ChainsNick Pruehs
 

More from Nick Pruehs (8)

Unreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual EffectsUnreal Engine Basics 06 - Animation, Audio, Visual Effects
Unreal Engine Basics 06 - Animation, Audio, Visual Effects
 
Unreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User InterfaceUnreal Engine Basics 05 - User Interface
Unreal Engine Basics 05 - User Interface
 
Unreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior TreesUnreal Engine Basics 04 - Behavior Trees
Unreal Engine Basics 04 - Behavior Trees
 
Unreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - GameplayUnreal Engine Basics 03 - Gameplay
Unreal Engine Basics 03 - Gameplay
 
Unreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal EditorUnreal Engine Basics 02 - Unreal Editor
Unreal Engine Basics 02 - Unreal Editor
 
Unreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game FrameworkUnreal Engine Basics 01 - Game Framework
Unreal Engine Basics 01 - Game Framework
 
Game Programming - Git
Game Programming - GitGame Programming - Git
Game Programming - Git
 
Tool Development 10 - MVVM, Tool Chains
Tool Development 10 - MVVM, Tool ChainsTool Development 10 - MVVM, Tool Chains
Tool Development 10 - MVVM, Tool Chains
 

Recently uploaded

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 

Recently uploaded (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 

Game Programming 04 - Style & Design Principles

  • 1. Game Programming Style & Design Principles Nick Prühs
  • 2. Objectives • To get an idea of good code style and structure in general • To understand the importance of consistent naming and code conventions • To learn how to property design types and members 2 / 61
  • 3. What is “good code”? • Straight-forward and obvious  You’ll spend far more time reading code than writing.  You’ll spend much time reading code you didn’t write. • Loose coupling • Well tested • Reused code • Efficient (?) 3 / 61
  • 4. How to achieve “good code”? Source: http://xkcd.com/844/ 4 / 61
  • 5. How to achieve “good code”? • Pair programming • Code Reviews • Client-first programming, Test-driven development • Refactoring • Unit Testing • Great tools • Static code analysis 5 / 61
  • 6. Naming Conventions • Greatly increase readability and usability • Differ from language to language  We’ll take a look at C#, but many guidelines apply to other languages as well. 6 / 61
  • 7. C# Capitalization • PascalCasing  Namespaces  Types  Member Names • camelCasing  Method Parameter Names 7 / 61
  • 8. Capitalization of Acronyms • Both characters of two-character acronyms  Example: IO • Only first character of three-character acronyms  Example: Xml, Html • Never for camelCased identifiers (such as parameters) 8 / 61
  • 9. Word Choice • Easily readable:  HorizontalAlignment instead of AlignmentHorizontal • Favor readability over brevity:  CanScrollHorizontally instead of ScrollableX • No underscores • No Hungarian notation • No abbreviations  Abbreviations don’t work well with IDEs. • Semantic names  GetLength instead of GetInt 9 / 61
  • 10. Namespace Names <Company>.<Product>.<Feature>.<SubNamespace> • Plural names where appropriate  Example: System.Collections • Don’t use the same name for namespace and type • Don’t use common names such as Element, Component 10 / 61
  • 11. Type Names • Nouns for classes and structs  Example: List, Vector • Derived classes can end with name of base class  Example: ArgumentException • Adjectives for interfaces • Prefix interface names with ‘I’  Example: IComparable • Use descriptive names for type-parameters  Dictionary<TKey, TValue> instead of Dictionary<K, V> • Singular names for non-flags enums  Color instead of Colors 11 / 61
  • 12. Member Names • Verbs for methods and events  Example: AddComponent, ComponentAdded • Nouns or adjectives for fields  Example: Duration, Invulnerable • Nouns for properties • Plurals for collections  Items instead of ItemList 12 / 61
  • 13. Boolean Trap #1 • Be positive!  Enabled = true instead of Disabled = false  CaseSensitive = true instead of CaseInsensitive = false 13 / 61
  • 14. Tabs vs. Spaces • Holy war between two fractions of programmers • Some are for tabs…  Seperation of visual representation from data  Customizable  Faster  Specifically meant for indentation • … while others are for spaces.  Always one column  Supported by IDEs anyway 14 / 61
  • 15. Tabs vs. Spaces Most important rule: Stay consistent. … or it might even blow up your version control. 15 / 61
  • 16. Tabs vs. Spaces „That said, only a moron would use tabs to format their code.” - Jeff Atwood 16 / 61
  • 17. Type Design – Class vs. Struct Make a type a struct instead of a class if it… • Logically represents a single value, • Has a valid state if all data is set to zero, • Has an instance size < 16 bytes, • Is Immutable, and • Won’t have to be boxed frequently 17 / 61
  • 18. Type Design – Class vs. Struct Make a type a struct instead of a class if it… • Logically represents a single value, • Has a valid state if all data is set to zero,  This is the case for array initialization, for example. • Has an instance size < 16 bytes, • Is Immutable, and • Won’t have to be boxed frequently 18 / 61
  • 19. Type Design – Class vs. Struct Make a type a struct instead of a class if it… • Logically represents a single value, • Has a valid state if all data is set to zero, • Has an instance size < 16 bytes,  Value type assignments copy all values • Is Immutable, and • Won’t have to be boxed frequently 19 / 61
  • 20. Type Design – Class vs. Struct Make a type a struct instead of a class if it… • Logically represents a single value, • Has a valid state if all data is set to zero, • Has an instance size < 16 bytes, • Is Immutable, and  Passing (and returning) by value implicitly creates a copy  Value types that can be changed will be confusing! • Won’t have to be boxed frequently 20 / 61
  • 21. Type Design – Class vs. Struct Make a type a struct instead of a class if it… • Logically represents a single value, • Has a valid state if all data is set to zero, • Has an instance size < 16 bytes, • Is Immutable, and • Won’t have to be boxed frequently  Happens when they’re cast to a interface  Allocated on heap and garbage collected, then! 21 / 61
  • 22. Type Design – Class vs. Interface • Use interfaces for polymorphic value type hierarchies  Example: IComparable, IConvertible • If an interface does more than exactly one thing, that’s a warning sign. 22 / 61
  • 23. Type Design – Class vs. Interface 23 / 61
  • 24. Type Design – Class vs. Interface • Favor (abstract) classes over interfaces  Adding members to an interface is a breaking change!  Seperating of contract from implementation with an interface is a myth…  … whereas doing so with an abstract class is not. 24 / 61
  • 25. Type Design – Enums • Favor enums over static constants • Provide a zero value for simple enums (e.g. None) • Don’t use enums for open sets • Don’t use flag enums if some combinations can be invalid 25 / 61
  • 26. Member Design – Property vs. Method Make a member a method instead of a property if it… • Is significantly slower than a field access would be, • Is a conversion operation (such as ToString), • Returns a different result each time it is called, • Has an observable side effect, • Returns a copy of an internal state, or • Returns an array 26 / 61
  • 27. Member Design – Properties • Preserve previous value if a property setter throws an exception • Don’t throw exceptions from property getters.  Use a method in that case. 27 / 61
  • 28. Gotcha! Don‘t throw exceptions from static constructors! 28 / 61
  • 29. Member Design – Methods • Shorter overloads should simply call longer ones  public int IndexOf(string s)  public int IndexOf(string s, int startIndex)  public int IndexOf(string s, int startIndex, int count) • Use descriptive parameter names • Avoid varying names or inconsistent ordering • Make only the longest overload virtual • Allow null to be passed for optional parameters  Default arguments are not CLS-compliant! 29 / 61
  • 30. Member Design – Extensions Use an extension method, if ... • It adds functionality relevant to every implementation of an interface, or • An instance method would introduce unwanted dependencies 30 / 61
  • 31. Member Design – Operator Overloads • Make sense for types that feel like primitive types • Should be defined symmetrically • Provide methods with friendly names, as well  Some languages don‘t support operator overloads. 31 / 61
  • 32. Member Design – Parameters • Use the least derived required paramter type • Validate all arguments • params keyword is useful for arbitrary (but small) numbers of parameters  Pay attention on parameter order  Be aware that null is a valid params array argument 32 / 61
  • 33. Boolean Trap #2 Guess what this code means? widget.repaint(false); 33 / 61
  • 34. Boolean Trap #2 Guess what this code means? widget.repaint(false); • Function argument is called immediate  true means immediate painting  false means deferred painting • Better use method overloads or enums! 34 / 61
  • 35. Boolean Trap #3 Guess what this code means? var opacitySlider = new Slider(true); 35 / 61
  • 36. Boolean Trap #3 Guess what this code means? var opacitySlider = new Slider(true); • Function argument is called horizontal  true means horizontal slider  false means vertical slider • Better use subclasses!  „Heck, who knows someday you’ll need a diagonal slider!” 36 / 61
  • 37. Boolean Trap #4 Guess what this code means? stackView.updateHeight(false); 37 / 61
  • 38. Boolean Trap #4 Guess what this code means? stackView.updateHeight(false); • Immediate or not? • Don‘t update height? • Update width??? 38 / 61
  • 39. Boolean Trap #5 The more, the merrier! event.initKeyEvent("keypress", true, true, null, null, false, false, false, false, 9, 0); 39 / 61
  • 40. Exceptions • Help you deal with any unexpected or exceptional situations that occur when a program is running • Exceptions are types that all ultimately derive from System.Exception. • Generated by the common language runtime (CLR), by the .NET Framework or any third-party libraries, or by application code. 40 / 61
  • 41. Benefits of Exceptions • Integrate well with object-oriented languages  Does not require changing the method signature • Can’t be ignored, whereas error codes can • Exception objects contain detailed information about the error, such as the state of the call stack and a text description of the error. • Possibility of defining an Unhandled Exception Filter (UEF) • Supported by debuggers 41 / 61
  • 43. Exception Design • Exceptions provide consistent error reporting  No bool return value required  No HRESULT  No GetLastError • Don’t return error codes.  If your error code is for the developer, add additional information to the exception message instead.  If your error code is for the exception handler, add a new exception type instead. 43 / 61
  • 44. Throwing Exceptions • Throw the most specific exception that makes sense.  ArgumentException  ArgumentNullException • Provide rich and meaningful error messages. • Introduce a new exception type only if it should be handled differently than existing exception types. 44 / 61
  • 45. Handling Exceptions • Use a try block around the statements that might throw exceptions. • Once an exception occurs in the try block, the flow of control jumps to the first associated exception handler that is present anywhere in the call stack. • If no exception handler for a given exception is present, the program stops executing with an error message. 45 / 61
  • 46. Handling Exceptions Unhandled exception reported by Microsoft Visual Studio 2012 46 / 61
  • 48. Exception Best Practice • Do not catch an exception unless you can handle it and leave the application in a known state. • Do not catch non-specific exceptions, such as System.Exception. • Use a finally block to release resources, for example to close any streams or files that were opened in the try block. 48 / 61
  • 49. Gotcha! Never use empty catch blocks! 49 / 61
  • 50. Common Exceptions • Thrown by your application or the framework  InvalidOperationException  ArgumentException  ArgumentNullException  ArgumentOutOfRangeException • Thrown by the CLR  NullReferenceException  IndexOfOutRangeException  StackOverflowException  OutOfMemoryException 50 / 61
  • 51. Collection Design • Inherit from Collection<T>, ReadOnlyCollection<T> or KeyedCollection<TKey, TItem> • Implement IEnumerable<T>  Allows foreach iteration of your collection. • Implement ICollection<T> or IList<T> where it makes sense • Implement an indexer where it makes sense 51 / 61
  • 53. The interface ICloneable • Contract doesn’t define whether cloning an object returns a deep or shallow copy • Define your own Clone method if required, without implementing the interface 53 / 61
  • 55. The interface IEquatable • Defines a method for determining equality of object instances • Implement on value types to prevent boxing • Override Object.Equals, == and != operators as well 55 / 61
  • 57. The interface IComparable • Defines a type-specific comparison method for ordering or sorting type instances. • Override <, >, <= and >= operators as well 57 / 61
  • 58. Equals, GetHashCode & ToString • If Object.Equals returns true for any two objects, GetHashCode must return the same value for these objects.  Dictionaries, which are implemented as Hashtables in .NET, will place two equal objects in different buckets otherwise, and lookup might fail in that case. • Override ToString wherever it makes sense.  Provides more useful debug output. 58 / 61
  • 59. Gotcha! GetHashCode must return the exactly same value across the lifetime of an object! 59 / 61
  • 60. References • Stack Overflow. What does a good programmer's code look like? http://stackoverflow.com/questions/366588/what-does-a-good- programmers-code-look-like, April 2017. • Cwalina, Abrams. Framework Design Guidelines. 2nd Edition. Addison- Wesley, 2011. • Hidayat. hall of api shame: boolean trap. http://ariya.ofilabs.com/2011/08/hall-of-api-shame-boolean-trap.html, August 24, 2011. • Atwood. Death to the Space Infidels! http://www.codinghorror.com/blog/2009/04/death-to-the-space- infidels.html, April 13, 2009. • MSDN. System Namespace. https://msdn.microsoft.com/en- us/library/System(v=vs.110).aspx, April 2017. 60 / 61
  • 62. 5 Minute Review Session • Name a few characteristics of good code! • How can you achieve good code? • Tabs or spaces? • When should you use a struct instead of a class? • When should you use a method instead of a property? • Name the three common interfaces and base classes that can be used for collections in .NET! • What is the main purpose of the interface IEquatable? • What is the main purpose of the interface IComparable? • How are Equals and GetHashCode related to each other?