 Hello guys, my name is Gustavo. You can call me Gus. This is gonna be like my second talk ever So if I am shaking nervous just bear with me, okay? so Today I'm just gonna talk about like bootstrapping two objects. I call it this way. What's your name again, sir? Michael told me know that this is also called primitive obsession, right? so one of the main reasons what I wanted to talk about this is Based on my experience. I have seen people just Just bootstrapping like creating the this logic if they want to do some calculation so math this developer we just Like kind of write some code here They're right the same code here and they're gonna have an or fire here and they're gonna have the same code here So what happened is like when you have your logic all spiral out of the water in every fire It's really difficult to tackle and really difficult to understand So this is like the main reason why I wanted to talk to you about this so One of a little bit why why this is bad is this is a course mail In computer programming design smell is an instructor the design that indicates violation of fundamental design principle fine Negatively impacting design quality why this is bad, right? So as I was telling you is like Having a bad design in your early process or in your early developer is gonna seriously hurt you At the end of the road So it's pretty important for you like when you are creating this code just to understand if if certain piece of code is It's just grabbing behavior You just need to like that my best suggestion for you is just grab a paper and see what is the behavior that this code Is taking what is the course that this code is taking and just give it a name Give it a name new an object and just encapsulate this logic there because the problem is like if you don't do it early And you have this logic all around the place when you need to talk Tackle it and to the factory is gonna be a really pain in the butt So it's really important for you to just to try to understand or try to visualize those problems at the early stage or if if You you come across the the situation when you are debugging this code and you see Okay, you have this logic here like let's see you are just Quoting the the user information like some address and you need to do some some some calculation with the address You need to to send an email or whatever. So if let's see that you have three endpoints Like see see the user you update the user or update the user and you have this logic in all these three places Like the problem is like when your business logic changes You will have to go on you will really need to know where this logic is So you can tackle it properly and the chances that you miss some validation and you have a Different between your code in these three places is really high So it's really important for you to try to tackle this Information and just to encapsulate it in the proper object and just give the name So that's the reason why this is bad So a little bit about the primitive smell you can see like use of primitives instead of the small objects for simple tasks Such a currency to run just a special stream for phone, etc. You know like for example when This this this was made right in in in my earliest stage I used to just to have a function at this function used to return an array and I used to return everything out of That array so the problem with that array is that you you don't have this this data consistency So you don't have like you are not certain about what information you are getting from this function And you are not certain about what information you are persisting to your database So this is that that is why it's important if you are gonna for example handle Currency like let's see you are selling I don't know. Let's see your SP group bills, right? You need to pay your bill online So if these people they they handle the code and they just treat this code as a float or as a Primitive object in the in their in their code base the chances that they are gonna have a problem in the future It's really high. It's better to have this Currency encapsulated in an object. So this object can take different behaviors. Let's see that SP groups opens a new office in in Indonesia So in Indonesia, they are working on I Think Malay Rubians or something. I don't know what the name is So the issue is like treating this object treating this currency here It's really different from treating this currency in HDD or USD so the the the aim or the the purpose of Encapsulated this information in objects is like these subjects will have behavior and they will attract behavior So if you have this information separated from your business logic, your business logic can work Snotly and the currencies can be a different Iceland So the way how they are gonna know avoid shutters is just a simple interface and you can type in a currency object And you can receive a currency object type hint So another example is for roles Users when we have for users if you are in the LSS stage of your application and you just need to to get a Life or to release it of course you will have a strings right admin super admin employees So the issue with this is like you you you don't have a way to control this this logic Down the road when you like when your application grows Super big and all your logic is just spread around So another problem that we have there and I have seen myself a lot is when we create these classes These objects in PHP and we create this content constants, right? We say okay the role admin is equal one So in all our code base. We are just gonna call this constant admin So we have like we are breaking the encapsulation of these objects and exposing this information out in in the outside world So whenever you need to Refactor this code or whenever you need to know about something about this code It's gonna be really difficult for you as I said in the past like if it's better that you have for your logic in one place I know scattered around so some of the treatment that I have been working literally myself is It's better to introduce value of data objects in place of primitive data like for example If you are querying your database, let's let's piggyback in the in the user example Let's see you need to to have your address information out of the user Right. So you if you are querying with PHP, of course, you will get like an array It's up to you is is there is nothing that's gonna stop you just to returning and working with this array or Do it in a consistent way. You can just knew up a data object. I usually call it like Values address value. I pass in my array my row array and I validate all I suppose the API that I wanted the outside word to know like for example if I Want to know the the address of this user I will have a method in this object called get address and this get address is gonna be type hint And so every time that when I when I am getting this up with this information from this object I am 100% certain that I am getting the right Okay, my boss is breaking Yeah, okay Okay, so here by doing so we encapsulate the related logic within an object and this will allow us to have a place where to operate from Like this is a no important How you say in Improvement to your code base because like for example, let's say that you have an user again simple simple examples Right the user you will have first name as last name But in so many cases in your application You will go through the situation where you need a full name from this user is Nobody's gonna stop you to to be in all the files and just calling first name last name I'm just make a normal concatenation there the advantage of having these objects created is like you can have a full name a Method there and you can operate or do your all the math that you require to in order for this subject to give you this Info and you can type him the return the type So you are guaranteeing like any time you you get this object out of there you have a valid data So another one is of course it will give us the ability to clean up the our code base single Since we will have objects representing our domain logic again like for example another Example I can give you is is one of I work in this Travel company right we work our main entities is a booking so but we have different bookings We have different cell channels. We have different people who really need to see different information from the booking So what it is is like we have in these different channels They they handle different controllers and they use us one all one contract for the booking That's gonna tell me okay any time I need to know something about the booking There is a contract that is telling me this but the information that this booking is gonna show to these different channels Is different so therefore I am cleaning my code outside and I am extracting all my business logic in these objects So everything that happened every calculation every month I am gonna have it here. You can unit test this code and you are 100% sure that every time that you are you know, you are gonna use this object outside. It's working What is it? Okay, so this is just related to what I was telling you is it's not is it's nothing that you will learn in the books Or whatever the books are gonna tell you So much crap right, but my suggestion is just just try to write in a paper the process that you have is like Let's see you are working in this controller Just imagine lasada right we have lasada and they they have these orders anytime you place an order There is there is an event in this application right this application needs to create an order needs to send an email and So on and so on and so on so you just need to go through this process and see what is your logic And how your logic is being bootstrapped and try to see what is the objects are being created But by seeing those lines those repetition let the repetition of your code tell me Tell you what domains and what objects unit just don't go like a straight out. Okay. Yes. I need an object for this no allow yourself to have Like crappy code see it read it and from there You will see what is what are the objects that are required in order for you to claim that particular code One few examples I have here. I think that this is the one that I was telling you about Let's see we have this a small class usually when I work with Value objects I declare my objects like final so nobody's gonna extend this these objects because the main purpose of them Is just to be data holders. So you you really know that what you are calling from there is the information that you need So let's see as I was telling you I usually this is a I'm pretty sure it's not the best practice to go about it But I usually pass in an array and I build my data object here. So because this is a simple Object I mean I didn't do it like super super super secure But you can you can work there is something called is this fences programming, right? What I have is like I was telling you some some of my example that I have been doing in this booking Value objects is like if I need to show a booking for this customer this booking needs to show this info, right? Let's see that the the booking needs to show let's see that we need to show a customer Right and the customer to in order for me to show this customer in my application They need to have a name and last name So what I'm gonna have here in my constructor is gonna have I'm gonna have a validate method that's gonna Check whether the data that is is is coming through is valid. So if it's nobody I turn exception So whatever happens after my constructor is valid data So this is how I build my objects. I how I was telling you I was you can have a name You can type him the the return information and you what you really know that the data that you are getting there is valid how I knew this object it's just simple just pass an array and Simple example as I was telling you about is the full name. Yes, you can hide here now you have If some of you have worked with a lot of them You might see this behavior being driven in the in the models, but yes, that's pretty much. This is an example so Okay, so what I was this is an example about like you can have these objects in order for you to take information from the outside work Or to expose information. Let's see like One of the things that I recently did in my work is I I had to implement like Dynamic payment gateway system right super normal stuff But the thing is like for example if if we if we need to build this this gate with out of an entity You can have an entity Object that you would you really know what is the info that you are getting from there? So you are type hinting an object and you are working with a valley entity here That you can do it in the constructor so you can pretty much do whatever I did here out of an array You can do it with an object. You can just build your object however you want or you can have it in whatever method you need You pass it in just The the the aim for this for this Implementation is just get rid of our primitive right that usually how I used to do it instead having objects here I use just to have simple and plain arrays and Of course an array can take everything and if we have worked in PHP, we know how the mess can be right Okay, we said I have only a small code example here so This one is called object example and this is called primitive example if we see it here The the same information that's being exposed here Increase the font size, I don't know how to do that Common plus it okay, okay better Okay So we have the primitive example which is just built out of arrays and whatever and We have our objects example if you see it here We have like the same information of course you will see like the information here is different It's just because they they have been built randomly But what is important is like we have it contains this string and it says yes Should we get a sex? No, and this one says yes. Okay. This one says yes I we need to check what maybe I have a back. Okay, so Just don't don't don't get over warm about this code is they are just simple controllers That build data. So if we see this object controller or the phone size phone size But it's no PHP storm. I'm sorry Haven't paid for it yet I So we have this simple controller is called object controller and this controller is things from this controller, right? This controller here just builds some item for me, right? It builds if we have object controller We call this guy called items that come from this controller here And this controller is just creates a fake instance and just give me Some object. Okay, so we are we have the same input here, which is the array but we are Encapsulating my logic in the object. Why because as we learned objects are better So if we have here in this controller, we are working with these items Here from the controller and we'll see okay That's the the the object because this guy now is a object is not an array, right? So that's this object has this string This index if it does it will tell you yes or no this this Logic that we see here if it's working properly is the one that says here, right? It will depend like if this is the this is the the whole the raw array and you see like they have this index here Hey, hey, hey, hey, and I says it contains a I said yes, so we are seeing that this logic here is working We have this should reject is We are gonna go through the code in a bit. So it says like okay if it's false it's false So it doesn't have it will check whether it have ccc. So it will tell you here This is the whole output. It's okay can reject the be value is be So it will give you the the information of be which is I don't know where this is is here So we have we're supposed in in our public API in these items objects We have this method being called we have this doesn't have we come from here We check this object also check whether if an index exists or not It also exposes a filter Ability in this subject that we don't know how it works is we just know that you can filter and It can filter also a so This is our object controller, right? So if we go to the primitive controller This guy is being built from this method is called raw If we go to our controller here, it's just a plain array. It's just it's the same Information that's been built here. The only difference is like in the object We are wrapping the information in an object and here we are just getting the raw data Which is the primitive data so you can of course type hinted. There is nothing bad about it But it's better to tap in an object seems you are gonna get more flexibility here, right? So if we see the primitive code We go about it like as we know, right? If you have worked with the race, we check like whether we have that index Whether there is a count there if he doesn't have it so it contains is that each Lake give me the value of the index be and so on and so on so We have this functionality here. We have this filter, but if we say here this filter here is making reference to the same File so which meaning is the logic the filters this array is be a lift in this file So this is something that I was telling you about is like if you have this logic in this file And you and then all of a sudden you need to use the same logic in three four different files That's when objects come come handy, right? So you have all your logic encapsulated your unit test that object and every where in your application where you use the object You are good to go right so pretty much This is a simple filter and how these objects are being built if you go here to the object controller If I see it here, yes, so we have no here here Sorry, so if we have this item here, so this guy just take us on a rate, right? So if we check this object, it's just a plain object So we have a data Information we have a filter data that we use it for some reason We are type hinting that we are depending on on on a day and a rate here to to Be like what object we check. What is the should reject method? Well based on this logic, it says, okay, if I don't have items in my array, okay I should reject this information. Let's see if you are working with Users and this user needs to place an order But you can say you can have like a this user object whatever information you are holding there, right? So you check, okay? Does this user have an email if he does? Okay, keep keep going if it doesn't have a me email You should reject this place order if we go back to the lasada order because it doesn't make sense for an online user to place an Order that who doesn't have an email, right? So we check whether it has it has exposes the has method Which is checks just if they're giving array has an index there But it's because this is a like simple example. You you might see like it's super simple But again, you this thing can can grow exponentially as you want like let's see if Sometimes I have have the opportunity of has or do something I return here and you Check availability, right? And then I pass whatever info in it here. Why? because sometimes our application will will grow right, let's see if Again, I'm gonna give you an example of my my daily days, right? So we we have we sell bookings, right? So these bookings they they have an inventory, but the same inventory can be a virtual inventory or can he or can be a physical inventory so like this check availability will come handy here because This check availability will be like you bootstrapper in order for you to check on availability for a given bookie Booking and listing so you can either go this way or you can just sit here and just write as much code as you want But everything will be here and you will your all of us aren't your application will be coupled to your logic So the main point the main point of all this subject is like you you will write your code in such a way That is easier to remove That's all the aim of having all this information right here if you write some code that can be removed That is a really bad code. So the best code is the one that you can be that can be removed anytime Okay, so we have here and this is the same the same logic that is filtering here It's doing the filter is the same logic that We have in this primitive Filter so the only difference is like we have it encapsulating in these items that does something for us And it will filter the information for us Yes Yes, that's it. So any questions I'm My questions. All right. Thank you