Dev Seminar Clean Object oriented programming Caveat ● Everything said in this lecture is true – Or so I'm claiming – at least for java/c++ style object – But some languages (python, rust, golang) don't have that object model and therefore work differently. – Many Principles still hold Abstraction ● The whole point of Object Oriented Programming/Design: – Abstraction through encapsulation – Encapsulation: hiding object state through private instance variables – Abstraction: hiding the details from users – it just works – Font rendering example in pgs 126-129 of code complete – Example from 94 in clean code ● Note the jab at java. ADT Advantages ● Classes are Abstract Data Types (ADTs) ● What are the advantages of these? ADT Advantages ● Classes are Abstract Data Types (ADTs) ● What are the advantages of these? – As a hint, why do we say global variables are bad? ADT Advantages ● Classes are Abstract Data Types (ADTs) ● What are the advantages of these? – As a hint, why do we say global variables are bad? – Every time we access one in a new place, have potential to muck up every other place the variable is used. ADT Advantages ● Classes are Abstract Data Types (ADTs) ● What are the advantages of these? – Now we can use whatever internal representation is efficient/convenient without the client code needing to know – We can change the implementation without anyone knowing/caring – We can make the code more readable/self documenting We see this in language design ● Long long ago, in a world quite different than our own…. – C was a new and shiny language – How did it manage text? See ADT advantage in language design ● Long long ago, in a world quite different than our own…. – C was a new and shiny language – How did it manage text? ● As an array of characters with a special end character ● This lived on to early versions of C++ See ADT advantage in language design ● Long long ago, in a world quite different than our own…. – C was a new and shiny language – How did it manage text? ● As an array of characters with a special end character ● This lived on to early versions of C++ – How about any modern and semi modern langage? ● Java,C#,python3,Go,Rust,Kotlin,Swift? See ADT advantage in language design ● Long long ago, in a world quite different than our own…. – C was a new and shiny language – How did it manage text? ● As an array of characters with a special end character ● This lived on to early versions of C++ – How about any modern and semi modern langage? ● Java,C#,python3,Go,Rust,Kotlin,Swift? – Text strings are an ADT Aside ● Python2 died Jan 1 2020 ● What is the biggest difference between python2 and python3? Aside ● Python2 dies Jan 1 2020 ● What is the biggest difference between python2 and python3? – Text handling. Python2 really can’t handle Unicode/UTF-8 characters sanely. If you want more than just English need UTF-8 Advantage of ADTs II ● Easy separation of multiple objects of same class ● See page 132 in clean code for example of id/handle approach. Accessors and Mutators ● We use accessors and mutators all the time – Why? Accessors and Mutators ● We use accessors and mutators all the time – And why are we letting clients access our private data? – Accessors maybe – Mutators?!?!?! – Why do we use accessors even? Is there a better way? – (I Know some languages love them) – “Richard” gives us another reason not to abuse mutators – http://mcfunley.com/from-the-annals-of-dubiou s-achievement Data Structures vs Objects ● Book: – Objects hide their data and expose operations – Data structures expose their data and have no operations. ● (think an array or a linked list) – Now we can try to make object oriented data structures ● but there is a reason for c++ struct – How about a Java 'class' that is really a data structure? – Python? Law of Demeter ● Law of Demeter – Ian Holland 1987 Northeastern Univ – Don't talk to strangers only talk to friends (in the c++ sense of the word) – AKA: principle of least knowledge – common version: ● A module should not know about the innards of the objects it uses – Podcast guest (changelog version) notes this ‘law’ is more like ‘rule of thumb of Demeter’ Law Of Demeter ● Law of Demeter Specifics: – A class C with a method M, M can only call: ● Methods in class C ● Methods in an object created by M ● Methods from objects passed as parameters to M ● Methods from an instance variable of C ● Do not call methods on objects returned by any of the above – Buys us automatically reduced coupling – By which I mean? ● Train Wrecks ● Often referred to as optional part or optional corollary of Law of Demeter: avoid train wrecks ● Any one know what train wrecks are? Train Wrecks ● Often referred to as optional part or optional corollary of Law of Demeter: avoid train wrecks ● Any one know what train wrecks are? ● this.configuration.getLocation().toString().toUpperC ase().equals(otherString) ● Wow!1? – Perfectly syntactically correct – I’ve done a lesser version myself. ● Law of Demeter violation? Train Wrecks ● Often referred to as optional part or optional corollary of Law of Demeter: avoid train wrecks ● Any one know what train wrecks are? ● this.configuration.getLocation().toString().toUpperC ase().equals(otherString) ● Wow!1? – Perfectly syntactically correct – I’ve done a lesser version myself. ● Law of Demeter violation? – No – but how long did it take you to tell? Train Wrecks ● Main problem with train wrecks is that it is hard to tell if there is a law of Demeter violation. ● How about this: – self.priceLabel.text = self.media.ad.price.value; – Is this a Law of Demeter violation? – And this? Train Wrecks ● Main problem with train wrecks is that it is hard to tell if there is a law of Demeter violation. ● How about this: – self.priceLabel.text = self.media.ad.price.value; – Is this a Law of Demeter violation? – No – if you can access the data members directly it is a data structure – not a class – So no Law of Demeter Violation. – Difficulty in languages like java and frameworks like java beans that demand all data structures use private instance variables and accessors. Data Transfer Objects ● Data Transfer Objects (DTO) – Pure data structures – Public instance variables, no methods – structs from C++ – Named tuples from python ● Next Step ● The Java Bean: – 'quasi-encapsulation' – Private instance variables – Accessors and mutators for all – Robert Martin (Uncle Bob) refers to this as: – “to make some OO purists feel better but usually provides no other benefit” – Were everywhere 10-ish years ago (in heyday of java) ● Seem to be less widely used these days ● Lots of legacy code to support. Active Records ● Refers to a 'special type of DTO ● DTO with 'navigation methods' like save and find ● Designed by Martin Fowler ● Don't add other methods – Like business rules. ● These days almost synonymous with ruby on rails – Which wasn’t a thing when Clean Code and code complete were written. Composition vs Inheritance ● Did Dr. Liang or Dr. Kim cover this? – Lots of different approaches in recent languages – Composition ● Has-a relationship, instance variable of another object – Inheritance ● Is-a relationship ● Lives in many OO languages ● Less so go/rust/objective-c Now ● Assignment reminder next slide ● After that, lets take some time and let the groups get together to see what they need to do next on this project. Assignment ● ● https://changelog.com/podcast/339 ● Or ● https://www.se-radio.net/2019/02/se-radio- episode-357-adam-barr-on-code-quality/ ● The software engineering radio episode is hosted by a professor, the changelog version is hosted by a former industry practitioner. Both of them interview the same guest about the same topic. He is more polished in the changelog episode