Many Advanced Developers Forget This

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey this is kaya from essentia developer.com [Music] today we would like to discuss the importance of the basics for beginners and advanced programmers so what we're teaching we notice that many developers like to rush in to the cool stuff and they end up getting too attached to acronyms and language features and they forget the basic concepts behind it and we believe that if we don't get the basics right we can all use our skills optimally so in this first video let's discuss objects so we normally ask what is an object and we get good answers for example it's a reference type its value in memory reference by minute in fire or a pointer then we ask ok what can we do with an object but we can capsule a data and we can expose methods to manipulate the data ok what else well we can enable polymorphism through inheritance hmm interesting ok so how do we create such object and things start getting interesting here you normally get the reply well you need an object-oriented language if you need to use the class keyword to define the object interface or template and then initialize the object with the class name and you pass an initial state okay so let's put those ideas in practice and stress the concept of an object and see what we can learn from that so I got here an empty project so let me create a test file and let's do something simple that's great like a user class okay so let's define our first class of course we cannot compile this let's create the class let me open the tests on the right pane make some more space okay so using the class keyword we can define our class user now we can compile okay but let's do something interesting with this user let's give it a first name in a less name again they cannot compile so let's give it an initializer okay to compile again but now let's create an assertion that sut the full name should be the combination of first and last name and again it doesn't compile so let me create a function that returns a string let's return just empty for now I want to see a failing test first okay you failed so let's capture our first name and our last name in the full name function we return the combination of both names okay that works so let's name this full name the combination of first and last names let me make some more space here okay so let's have a look at this class so we have data that is encapsulated so it's private no one has access to it outside the scope of this class we exposed an internal function that is some operation with this data and return the value and what else can we do here let's let's new takes this object let's do something like set first name so let me create a test for that can update first name copy these now when I call set first name ciao I want to change my name to Cholula let's see what happens it failed so I need to set my first name to be the new first name I need to make these 'var so now we are performing operations on this class that mutates the internal state through methods or messages rather than exposing the data great now let's investigate inheritance let's say we have a class premium user that inherits from user and let's create a test human user enhance a star to the full name so let's create a premium user here let me delete this and I expect the name to have let's say a star there you go it's a premium user let's run the tests and look at that it failed but I didn't get any compiler error because the premium user inherits all of the methods from the superclass including the initializer in all the other constraints like the internal data so when I called full name in the premium user I'm getting the behavior from the user class so to change the behavior of the full name I need to override the method so let's do that so in these new implementation of the method I can return the super implementation of the method that is the behavior defined in user and now I can append my star great there you go so just for the sake of it let's add another test to make sure that can update first name of a premium user so if I make this a premium user I would might start with the changed name let's run this there you go we got this behavior for free even though we overwrite implementation of the full name we don't have to re-implement the set name ok that's good now my question is do I need the class keyword to define an object and to create an object do I need an object-oriented language can have all these behaviors weed out the class keyword let's say first of all let's do some refactoring here let me create a method to create a user we first and last name you know their factory method to create the premium user let me run to make sure that everything is still running and it is okay so let's try to recreate this behavior read out the class keyword so let's start with the initializer well the initializer as you can see looks pretty much like a function their receivers to parameters so let's start by creating an function let's call it make user object and it needs to return something you should return something that exposes some methods just like an object is we have a set method and a foo named method so for now let's return a tuple with a set first name closure that receives the string and returns void and let's have another called full name actually the full name returns a string just like here great that's great estoppel set first name is a closure the receivers a new string let's call it a new first name and the full name is a closure that returns a string so here let's return first name plus last name just like in the previous implementation ok compile so let's try to replace or user class instance with the new make user object type so here I'm going to replace the user with make user object and now I need to return the same type in here you're going to reflector this later no problem let's try to build so of course we need to change this to be set first name ok the first test passed by the second one didn't because we didn't implement a set first name so here things start getting interesting because I need to mutate this first name it needs to be captured by disclosure so I'm gonna create a local variable here first name because first name and now since this is a var I can changed is inside disclosure and I'm gonna use the underscore first name in my full name implementation let me run this again any passes all right same behavior okay let's do the same thing for the premium user object let me copy and paste this very quickly make Freeman user object first-name lastname this is first name plus last name plus the start let me replace this in the test usage and the return type okay all the test passes but there is a lot of repetition here look they're implementing these again and these again and in the previous class based solution we inherited those things for free so how can we have polymorphism without the class keyword or without inheritance let's try something here what if we create a user here and now we return another top oh let me reuse the user set first name function and in the full name implementation you're gonna use the user fuel name plus the star can make this a let's now run the test again that's right so look how close these looks to the premium user looks pretty similar actually I even gonna call these something else so what if I call these super does it make sense now let's run the test once again they all pass great so I'm gonna delete the class based solution let's do some refactoring here and create some type Alice's in the test as well that's one of the tests they all pass and we have created an object without the class keyboard because there is an object is a concept and object-oriented languages make it that first-class citizen and you have the class keyword to define those interfaces and then we can define inheritance know that but an object can also be just ACTA or it can be a function if we understand what an object is we can notice that I may be using these concept I was not calling it an object but behind the scenes I was using an object let me show you what I mean by changing this type alias to be a struct now I can just initialize these with the right name I can do the same here with the premium user let me run the test they pass there you go I have created an object by using this struct keyword isn't that crazy a lot of people get surprised by this because we get so attached to the semantics and we forget the basics so I show this trick to a lot of people and they say wow I'm saying that objects that are bad thing but I've been creating them as function or as a tuple or as a struct and I had no idea I was creating an object so objects are not evil depends how we used them and a lot of people that think objects are evil they are actually using objects all the time to achieve their goals I think anyone can learn something from this we definitely learned a lot by doing those exercises and we noticed that most advanced programmers we meet they are very very good at the basics the simple stuff they continually practice Qatar's they learn new languages that we learned old concepts so never stop learning ok we hope you enjoyed this video and learned something today don't forget to subscribe to the channel and I see you next time [Music] you
Info
Channel: Essential Developer
Views: 5,670
Rating: undefined out of 5
Keywords: ios, swift, professionalism, ios development, ios engineering, ios app development, xcode, tdd, modular design, architecture, agile, advanced ios development, iphone, advanced swift, clean code, unit testing, testing, swift framework, xctest, framework, screencast, course, advanced, solid principles, code coverage, swift4, swift 4, uikit, ios testing, advanced ios, advanced testing, structs, classes, swift structs, swift classes, struct or class, struct, class, functional programming
Id: nh5LipqIt4g
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Fri Jul 13 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.