Object-Oriented Programming
PROGRAMMING? • Just writing some text in a specific format 2
PROGRAMMING LANGUAGES 3
SO, WHAT IS TRICK HERE? • A compiler/interpreter does the actual job! • Read text file, interpret the contents, output another file more understandable by machines, less understandable by us  4
MODERN IDES • Previous slide is not a modern day practice. – At least only computer engineers likes to do it. • Now we have IDEs (Integrated Development Environment) – Providing  Text editors  Compilers  And many other useful features 5
IDE SAMPLES 6
IDE SAMPLES 7
WHAT IS A PROGRAM? • Just some sequential lines to instruct the computer what to do! 8
WHAT IS A PROGRAMMING LANGUAGE? • Provides the developer useful constructs – Defining variables – Looping – Conditionals – Many more… • And advanced constructs – Data management – Memory management – Networking – Scheduling 9
WHICH LANGUAGE • According to Google*: – The fastest language is C++ – BUT  If you don’t optimize your code, you may end up slower than other languages – See C++ Dbg vs other languages 10*Loop Recognition in C++/Java/Go/Scala by Robert Hundt - Google
OF COURSE… • The programs are not small any more! – Chrome browser: 17 millions LOC (lines of code) – Office 2013: 45 millions LOC – Facebook: 60 millions LOC • You can’t just put all the lines sequentially and expect someone to understand! – First, it is impossible. – Second, it is torture. • Of course we need extra structures to handle the complexity 11
WHEN THINGS GET BIGGER - MODULARIZE 12 We can define methods in order to make our program mode readable.
MODULARIZE EVEN MORE 13 We can make this method parametric. And use parametrized version whenever needed.
FUN PART 14 Spoiler alert: You will see some code in action!
ENOUGH OF THE LINES, GIVE ME SOMETHING 15 I want to do this 3 times?
MODULARIZING - PARAMETRIZED 16
MODULARIZING – MORE PARAMETRIZED 17 We still have a lot of repetitions.
EPIPHANY • Wouldn’t it be nice if there is a special structure that: – Knows what kind of an animal it is. – Knows how many legs it has. – Can make sound by itself. – Has a name. • So that, I don’t have to write them every time. 18
EPIPHANY 19 So, we are basically talking about a dog? It has a name, knows how many legs it has, can make sound itself.
EPIPHANY • So we need to mimic the real life in our applications. • Because a program is for the developer to write/understand easily, not for the computer. 20*Author of the renowned Structure and Interpretation of Computer Programs book (with Jay Sussman) “First, we want to establish the idea that a computer language is not just a way of getting a computer to perform operations but rather it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute.” Harold Abelson*
EPIPHANY • Wouldn’t it be nice if there is a special structure that: – Knows what kind of an animal it is. – Knows how many legs it has. – Can make sound by itself. – Has a name. • We already have a system for this. – Object-oriented programming.  A.k.a. – Object-oriented methodology – Object-oriented paradigm – Object-oriented design and analysis 21
OBJECT-ORIENTED PROGRAMMING • Basically, we have objects that know: – Its attributes – Its operations • Whenever, I need a square or triangle, I will use them. – I ask for information and they answer. 22 Side Area = side*side Side Height Base Area = (height*base)/2 height base
OBJECT IDENTIFICATION • Car, glass, bike, bus – All can be objects • Dog, cat, person, laptop – Again, can be objects • Running, walking, making a sound etc… ? – No, these are not objects! – These are actions (methods/operations) that can be done by an object  Like a Rectangle can compute its own area  A people can run. • Side, height, numberOfLegs etc… ? – No, these are not objects! – These are properties (attributes) that can be owned by an object  Like side of a Rectangle.  Numberoflegs of a Person. 23
EVERYTHING CAN BE AN OBJECT • It just depends on the context! • Distance between two cities can be an object – If it is the main focus of the problem. • Relationship between two people can be an object – If we are solving a problem about this. 24
LET’S IDENTIFY SOME OBJECTS • Company XYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 25
OBJECTS IDENTIFIED • Company XYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 26
ANY MORE? • Company XYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 27
ANOTHER EXAMPLE • An ATM needs to allow a customer to identify themselves – Each customer has a debit card and PIN • Customers should be presented with some kind of menu to help direct them. • Customers can perform two transactions: – They should be able to deposit funds – They should be able to withdraw funds upto $200  These funds must be withdrawn in units of $20 • The ATM should tell some banking software to update the customers’ account at the end of transaction • The ATM should also give the customer some record of the transaction. 28
OBJECTS IDENTIFIED • An ATM needs to allow a customer to identify themselves – Each customer has a debit card and PIN • Customers should be presented with some kind of menu to help direct them. • Customers can perform two transactions: – They should be able to deposit funds – They should be able to withdraw funds upto $200  These funds must be withdrawn in units of $20 • The ATM should tell some banking software to update the customers’ account at the end of transaction • The ATM should also give the customer some record of the transaction. 29 This PIN number object seems redundant. We can make it a property of either customer or debit card. This small design decision will make a lot of difference.
OBJECT-ORIENTED LANGUAGES • Most modern languages support object-orientation now – Java – C++ (C with classes) – C# – Python – Even Fortran – Any many more 30
A REPRESENTATION SYSTEM • Various languages support object-orientation. • So, there should be a way to represent this system regardless of the language we use. • UML is here. 31
UML BASICS - CLASS 32 Side Area = side*side Side Height Base Area = (height*base)/2 height base
CLASS IN C# 33
WAIT • We were talking about objects, now what the heck is a class? • Time to define the relation between these two. • Basically, we get the properties that all the dogs have and define a Dog class with them. – A class is a generalization of objects. – And objects are just instances of the class. 34
OBJECTS FROM CLASSES • Constructor • Now we can create objects – From classes – Using a constructor 35
CONCEPTS & DESIGN PRINCIPLES IN OBJECT-ORIENTED PROGRAMMING • Concepts: – Encapsulation – Inheritance – Polymorphism – Cohesion – Coupling • Principles – Open-Closed Principle – Don’t Repeat Yourself Principle – Single Responsibility Principle – Liskov Substitution Principle – Interface Seggregation Principle – Dependency Inversion Principle 36 If we don’t use these concepts and obey these principles, we aren’t coding in proper object-oriented way.
ENCAPSULATION • Protecting your information from being used incorrectly. 37 PROBLEM? What if speed if out of limits? What if we want some adjustments before setting the speed?
ENCAPSULATION 38 SOLUTION?
INHERITANCE • A class’ inheriting properties and operations from another class. • Both Dogs and Cats have names, right? – Then, why shouldn’t we create a base class Animal for them? 39
POLYMORPHISM • Having different forms of same operations. 40
POLYMORPHISM 41
COHESION • A class should do one thing really well, and shouldn’t try to do or be someone else. • Strong cohesion means: all methods of a class are more or less related. – A Math class which can compute sqrt, power, exp, cos etc. • If a class has methods for: – Printing a document – Sending an email – Working with trigonometric functions – How should we name it? Complicated, right? 42
COUPLING • The extent to which classes depend on one another. – A class should work independently without being coupled too much to other classes. – This helps us making them modules and available on demand. 43
OPEN-CLOSED PRINCIPLE • Classes should be open to extension, but closed for modification. 44 PROBLEM? Whenever we add a new shape, we should modify the calculateTotalArea method.
OPEN-CLOSED PRINCIPLE 45 SOLUTION?
OPEN-CLOSED PRINCIPLE 46
DON’T REPEAT YOURSELF PRINCIPLE • Avoid duplicate code by abstracting common things out and placing them in a single location. 47
SINGLE RESPONSIBILITY PRINCIPLE • Every object in the system should have one responsibility – Each object has only one reason to change – Doesn’t mean it should only have one method • Usually the violation of this principle can be identified by asking: – Can _______ _________ itself?  Example: Can Automobile changeTires itself?  If it is no, it is violating SRP. 48
SINGLE RESPONSIBILITY PRINCIPLE 49
LISKOV SUBSTITUTION PRINCIPLE • Sub-classes must be substitutable for their base classes. – If you inherit from a wrong base class, then you can’t do this. 50 PROBLEM? We inherit from the board, but we hardly use its methods or properties. Because they don’t match with 3D board.
LISKOV SUBSTITUTION PRINCIPLE • Sub-classes must be substitutable for their base classes. – If you inherit from a wrong base class, then you can’t do this. 51 SOLUTION?
INTERFACE SEGGREGATION PRINCIPLE • A class should never be forced to have some unnecessary methods. 52 PROBLEM? We force Square to have a computeVolume method, which it doesn’t have!
INTERFACE SEGGREGATION PRINCIPLE 53 SOLUTION?
DEPENDENCY INVERSION PRINCIPLE • Entities must depend on abstractions/interfaces rather than actual classes. – So that they can be decoupled. 54 PROBLEM?We have to treat each shape separately. Because we have links to actual classes.
DEPENDENCY INVERSION PRINCIPLE 55 SOLUTION?
WHAT CAN WE ACHIEVE FROM THIS? SOME IDEAS • Read data from various sources but our program can stay the same • Easily switch between different algorithms on the same data • Make any module work independent from others • Make the output of the project independent from the data or the algorithm itself • Test the correctness of each class independently • Model the problem in a human-readable way • And most importantly, reuse and maintain your application better. – Future-proof. 56
OTHER TOOLS & TECHNOLOGIES IN ORDER TO IMPROVE YOUR PROGRAMMING • Versioning (github, bitbucket etc.) – Version your code in order to access any change you made (and backup) • Unit tests – Test each unit independently, be sure it is doing whatever it is supposed to do. – Most languages have support for this. Look for it. • Documentation (Doxygen, Javadoc etc.) – Always comment your code. You can even produce automatic documentation from these comments. • Diagramming – Your code may not be understandable, but your diagrams will be. Learn UML Class diagram and use it in your projects. 57
CONCLUSION • Object-oriented programming provides very flexible structures for our programs. – It can be applied in many languages, as long as the language supports object-orientation. • If we obey the principles, it will be an actual system. – Otherwise, it is just the same code with classes and additional complexity. • Object-oriented system is not a perfect system and it has its own flaws. But it is still the best system. • Always strive for the best design. 58
QUESTIONS? • Thanks for listening… • For offline questions, find my contact info here: www.objectivelook.net 59
SOME RESOURCES • Object-oriented programming with C# (The book itself is nice and free, chapter 20 is OOP): http://www.introprogramming.info/english-intro- csharp-book/read-online/ • For new starters to OOP, this book is fun: https://www.amazon.com/Head-First-Object-Oriented-Analysis- Design/dp/0596008678 • Detailed explanation, nicely done, 2 pages (Java): – https://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.ht ml – https://www.ntu.edu.sg/home/ehchua/programming/java/J3b_OOPInheritan cePolymorphism.html • Same as above but with C++: – https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html • Even though, there are a lot of resources. I suggest to work with someone who you can ask questions immediately. Because OOP requires a change of mindset. 60

Object oriented programming

  • 1.
  • 2.
    PROGRAMMING? • Just writingsome text in a specific format 2
  • 3.
  • 4.
    SO, WHAT ISTRICK HERE? • A compiler/interpreter does the actual job! • Read text file, interpret the contents, output another file more understandable by machines, less understandable by us  4
  • 5.
    MODERN IDES • Previousslide is not a modern day practice. – At least only computer engineers likes to do it. • Now we have IDEs (Integrated Development Environment) – Providing  Text editors  Compilers  And many other useful features 5
  • 6.
  • 7.
  • 8.
    WHAT IS APROGRAM? • Just some sequential lines to instruct the computer what to do! 8
  • 9.
    WHAT IS APROGRAMMING LANGUAGE? • Provides the developer useful constructs – Defining variables – Looping – Conditionals – Many more… • And advanced constructs – Data management – Memory management – Networking – Scheduling 9
  • 10.
    WHICH LANGUAGE • Accordingto Google*: – The fastest language is C++ – BUT  If you don’t optimize your code, you may end up slower than other languages – See C++ Dbg vs other languages 10*Loop Recognition in C++/Java/Go/Scala by Robert Hundt - Google
  • 11.
    OF COURSE… • Theprograms are not small any more! – Chrome browser: 17 millions LOC (lines of code) – Office 2013: 45 millions LOC – Facebook: 60 millions LOC • You can’t just put all the lines sequentially and expect someone to understand! – First, it is impossible. – Second, it is torture. • Of course we need extra structures to handle the complexity 11
  • 12.
    WHEN THINGS GETBIGGER - MODULARIZE 12 We can define methods in order to make our program mode readable.
  • 13.
    MODULARIZE EVEN MORE 13 Wecan make this method parametric. And use parametrized version whenever needed.
  • 14.
    FUN PART 14 Spoiler alert: Youwill see some code in action!
  • 15.
    ENOUGH OF THELINES, GIVE ME SOMETHING 15 I want to do this 3 times?
  • 16.
  • 17.
    MODULARIZING – MOREPARAMETRIZED 17 We still have a lot of repetitions.
  • 18.
    EPIPHANY • Wouldn’t itbe nice if there is a special structure that: – Knows what kind of an animal it is. – Knows how many legs it has. – Can make sound by itself. – Has a name. • So that, I don’t have to write them every time. 18
  • 19.
    EPIPHANY 19 So, we arebasically talking about a dog? It has a name, knows how many legs it has, can make sound itself.
  • 20.
    EPIPHANY • So weneed to mimic the real life in our applications. • Because a program is for the developer to write/understand easily, not for the computer. 20*Author of the renowned Structure and Interpretation of Computer Programs book (with Jay Sussman) “First, we want to establish the idea that a computer language is not just a way of getting a computer to perform operations but rather it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute.” Harold Abelson*
  • 21.
    EPIPHANY • Wouldn’t itbe nice if there is a special structure that: – Knows what kind of an animal it is. – Knows how many legs it has. – Can make sound by itself. – Has a name. • We already have a system for this. – Object-oriented programming.  A.k.a. – Object-oriented methodology – Object-oriented paradigm – Object-oriented design and analysis 21
  • 22.
    OBJECT-ORIENTED PROGRAMMING • Basically,we have objects that know: – Its attributes – Its operations • Whenever, I need a square or triangle, I will use them. – I ask for information and they answer. 22 Side Area = side*side Side Height Base Area = (height*base)/2 height base
  • 23.
    OBJECT IDENTIFICATION • Car,glass, bike, bus – All can be objects • Dog, cat, person, laptop – Again, can be objects • Running, walking, making a sound etc… ? – No, these are not objects! – These are actions (methods/operations) that can be done by an object  Like a Rectangle can compute its own area  A people can run. • Side, height, numberOfLegs etc… ? – No, these are not objects! – These are properties (attributes) that can be owned by an object  Like side of a Rectangle.  Numberoflegs of a Person. 23
  • 24.
    EVERYTHING CAN BEAN OBJECT • It just depends on the context! • Distance between two cities can be an object – If it is the main focus of the problem. • Relationship between two people can be an object – If we are solving a problem about this. 24
  • 25.
    LET’S IDENTIFY SOMEOBJECTS • Company XYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 25
  • 26.
    OBJECTS IDENTIFIED • CompanyXYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 26
  • 27.
    ANY MORE? • CompanyXYZ is a manufacturing company that produces cartoon action figurines for big entertainment companies. • This company is using an inventory and tracking system. • The inventory system keeps track of how many of each figurine is stored in each warehouse. • Figures are stored in cases. • Clients order the figurines and the cases are eventually shipped to clients. 27
  • 28.
    ANOTHER EXAMPLE • AnATM needs to allow a customer to identify themselves – Each customer has a debit card and PIN • Customers should be presented with some kind of menu to help direct them. • Customers can perform two transactions: – They should be able to deposit funds – They should be able to withdraw funds upto $200  These funds must be withdrawn in units of $20 • The ATM should tell some banking software to update the customers’ account at the end of transaction • The ATM should also give the customer some record of the transaction. 28
  • 29.
    OBJECTS IDENTIFIED • AnATM needs to allow a customer to identify themselves – Each customer has a debit card and PIN • Customers should be presented with some kind of menu to help direct them. • Customers can perform two transactions: – They should be able to deposit funds – They should be able to withdraw funds upto $200  These funds must be withdrawn in units of $20 • The ATM should tell some banking software to update the customers’ account at the end of transaction • The ATM should also give the customer some record of the transaction. 29 This PIN number object seems redundant. We can make it a property of either customer or debit card. This small design decision will make a lot of difference.
  • 30.
    OBJECT-ORIENTED LANGUAGES • Mostmodern languages support object-orientation now – Java – C++ (C with classes) – C# – Python – Even Fortran – Any many more 30
  • 31.
    A REPRESENTATION SYSTEM •Various languages support object-orientation. • So, there should be a way to represent this system regardless of the language we use. • UML is here. 31
  • 32.
    UML BASICS -CLASS 32 Side Area = side*side Side Height Base Area = (height*base)/2 height base
  • 33.
  • 34.
    WAIT • We weretalking about objects, now what the heck is a class? • Time to define the relation between these two. • Basically, we get the properties that all the dogs have and define a Dog class with them. – A class is a generalization of objects. – And objects are just instances of the class. 34
  • 35.
    OBJECTS FROM CLASSES •Constructor • Now we can create objects – From classes – Using a constructor 35
  • 36.
    CONCEPTS & DESIGNPRINCIPLES IN OBJECT-ORIENTED PROGRAMMING • Concepts: – Encapsulation – Inheritance – Polymorphism – Cohesion – Coupling • Principles – Open-Closed Principle – Don’t Repeat Yourself Principle – Single Responsibility Principle – Liskov Substitution Principle – Interface Seggregation Principle – Dependency Inversion Principle 36 If we don’t use these concepts and obey these principles, we aren’t coding in proper object-oriented way.
  • 37.
    ENCAPSULATION • Protecting yourinformation from being used incorrectly. 37 PROBLEM? What if speed if out of limits? What if we want some adjustments before setting the speed?
  • 38.
  • 39.
    INHERITANCE • A class’inheriting properties and operations from another class. • Both Dogs and Cats have names, right? – Then, why shouldn’t we create a base class Animal for them? 39
  • 40.
    POLYMORPHISM • Having differentforms of same operations. 40
  • 41.
  • 42.
    COHESION • A classshould do one thing really well, and shouldn’t try to do or be someone else. • Strong cohesion means: all methods of a class are more or less related. – A Math class which can compute sqrt, power, exp, cos etc. • If a class has methods for: – Printing a document – Sending an email – Working with trigonometric functions – How should we name it? Complicated, right? 42
  • 43.
    COUPLING • The extentto which classes depend on one another. – A class should work independently without being coupled too much to other classes. – This helps us making them modules and available on demand. 43
  • 44.
    OPEN-CLOSED PRINCIPLE • Classesshould be open to extension, but closed for modification. 44 PROBLEM? Whenever we add a new shape, we should modify the calculateTotalArea method.
  • 45.
  • 46.
  • 47.
    DON’T REPEAT YOURSELFPRINCIPLE • Avoid duplicate code by abstracting common things out and placing them in a single location. 47
  • 48.
    SINGLE RESPONSIBILITY PRINCIPLE •Every object in the system should have one responsibility – Each object has only one reason to change – Doesn’t mean it should only have one method • Usually the violation of this principle can be identified by asking: – Can _______ _________ itself?  Example: Can Automobile changeTires itself?  If it is no, it is violating SRP. 48
  • 49.
  • 50.
    LISKOV SUBSTITUTION PRINCIPLE •Sub-classes must be substitutable for their base classes. – If you inherit from a wrong base class, then you can’t do this. 50 PROBLEM? We inherit from the board, but we hardly use its methods or properties. Because they don’t match with 3D board.
  • 51.
    LISKOV SUBSTITUTION PRINCIPLE •Sub-classes must be substitutable for their base classes. – If you inherit from a wrong base class, then you can’t do this. 51 SOLUTION?
  • 52.
    INTERFACE SEGGREGATION PRINCIPLE •A class should never be forced to have some unnecessary methods. 52 PROBLEM? We force Square to have a computeVolume method, which it doesn’t have!
  • 53.
  • 54.
    DEPENDENCY INVERSION PRINCIPLE •Entities must depend on abstractions/interfaces rather than actual classes. – So that they can be decoupled. 54 PROBLEM?We have to treat each shape separately. Because we have links to actual classes.
  • 55.
  • 56.
    WHAT CAN WEACHIEVE FROM THIS? SOME IDEAS • Read data from various sources but our program can stay the same • Easily switch between different algorithms on the same data • Make any module work independent from others • Make the output of the project independent from the data or the algorithm itself • Test the correctness of each class independently • Model the problem in a human-readable way • And most importantly, reuse and maintain your application better. – Future-proof. 56
  • 57.
    OTHER TOOLS &TECHNOLOGIES IN ORDER TO IMPROVE YOUR PROGRAMMING • Versioning (github, bitbucket etc.) – Version your code in order to access any change you made (and backup) • Unit tests – Test each unit independently, be sure it is doing whatever it is supposed to do. – Most languages have support for this. Look for it. • Documentation (Doxygen, Javadoc etc.) – Always comment your code. You can even produce automatic documentation from these comments. • Diagramming – Your code may not be understandable, but your diagrams will be. Learn UML Class diagram and use it in your projects. 57
  • 58.
    CONCLUSION • Object-oriented programmingprovides very flexible structures for our programs. – It can be applied in many languages, as long as the language supports object-orientation. • If we obey the principles, it will be an actual system. – Otherwise, it is just the same code with classes and additional complexity. • Object-oriented system is not a perfect system and it has its own flaws. But it is still the best system. • Always strive for the best design. 58
  • 59.
    QUESTIONS? • Thanks forlistening… • For offline questions, find my contact info here: www.objectivelook.net 59
  • 60.
    SOME RESOURCES • Object-orientedprogramming with C# (The book itself is nice and free, chapter 20 is OOP): http://www.introprogramming.info/english-intro- csharp-book/read-online/ • For new starters to OOP, this book is fun: https://www.amazon.com/Head-First-Object-Oriented-Analysis- Design/dp/0596008678 • Detailed explanation, nicely done, 2 pages (Java): – https://www.ntu.edu.sg/home/ehchua/programming/java/J3a_OOPBasics.ht ml – https://www.ntu.edu.sg/home/ehchua/programming/java/J3b_OOPInheritan cePolymorphism.html • Same as above but with C++: – https://www.ntu.edu.sg/home/ehchua/programming/cpp/cp3_OOP.html • Even though, there are a lot of resources. I suggest to work with someone who you can ask questions immediately. Because OOP requires a change of mindset. 60