 Happy Friday So my name is Enrique I Worked for a company called Salesforce. I'm a software engineer there My team is called trailhead and we are the learning platform from Salesforce So you want to learn how to use Salesforce? You can just go to trailhead.com and learn from there and today I want to tell you a story and the story is about Once upon a time in a country for a way Maybe called Rubinia. Maybe not There was a King who really liked to look at the moon and this country was a better in modern country Trains were really fast and arrive on time Some people even say that there were magic trains because they were really fast Elevators run on time But for all the automation and all the benefits of that come with it There was a problem with the moon and the moon of course is written in ruby and runs on the cloud and the problem with the moon was that Sometimes Wasn't predictable. He would come up on the east Sometimes he wouldn't you couldn't see it at all sometimes you could come up on the west and Sometimes he would even hide behind the Sun So one night the king was trying to look at the moon and he waited and waited And he couldn't see the moon But he was super sad That he fell ill So the next morning the royal doctor called all the king's family and when they Their family came the first one to show up was Elizabeth whoever you want calls just Ellie and Elizabeth was Junior developer he is starting to get into ruby And she said hey uncle. I'm here. How can I help you? Oh Elizabeth please help me fix the moon Uncle but I'm just a junior developer. Can you ask anyone else? Of course, I can ask I'm the king. I can ask whoever I want, but I want you to fix it Okay, uncle. I'll go ahead and start fixing the moon so she went and Did a good plan of the solar system and she started looking at the code and She looked and she looked But she couldn't find a moon class not a file for the moon so She started Amplifying her search to the position that she was able to find a jammel file for the moon and As you can see like the jammel file has the descriptions of the properties for the moon But then she started thinking how how does this became a class that interacts with the real world She starts looking where are jammel files being used and she got to this code The star factory Now, of course, it starts with a big man And what basically this code does it blows the data from all the jammel files for all the stars Then once you have all the stars you create one class per star So far very simple, but then when she started to look into Where can she find the issue for the moon? She discovered this method called visibility from earth Which is a complicated calculation It basically tells you where are you and then we're supposed to be the moon But we're gonna simplify it because it's a ruby conference. So basically The moon visibility from the earth is equal to the earth position Multiplied by the moon orbit all right So there's the method to find visibility that tells you earth you find the earth and then you Tell the position of the earth and multiply it by the star of orbit and in this case the star would be the moon so the next thing that she thought well There's probably an issue in that the start of orbit. So I just need to find the Orbit method for the moon, right? Well, we are wrong again Why because of course the method is defined dynamically. So there's no Death of it. So you have to go and find it again this way and as you can see there is a Star finder that defines the Sun and then we use the gravitational field of the Sun Multiply by the velocity escape velocity for the moon and that will give you the orbit for the moon All right But you couldn't figure out where was the issue why the moon was behaving so unpredictable So what she did was what everyone does she looked at the code and started looking who was the last person interacted with this code and Of course, it was the metaprogramming minister of magic So at least I went into the office of the metaprogramming minister of magic and she found Multiple screens some screens have code other screens had a graphics that were showing the performance of the systems other screens had a shopping list and one screen had console where he would run commands on the solar system and Well, I want to tell you that the minister of metaprogramming. He's a Very important developer in the kingdom. He's one of the wise developers, especially because he helped Design and build the solar system He also wrote code that could write code to test the train system And of course, he also wrote code that would test the code that he wrote the first time So in other words, he's a very smart developer and The first thing that Ali said to the metaprogramming minister was Hey, can we create a new class for the moon? I'm having this problem and I find it very difficult to change the solar system to fix the moon And the metaprogramming minister said No, I don't think it's a good idea Maybe just tweak a little parameters on the yaml file and see what happens No, but I don't think the issue is on the parameters. I think the issue is on the actual method No, no, the code doesn't have Errors it has been running for a while. It could not have errors Well, if that were to the king wouldn't be ill and he is ill right now So, can you please help me? No, and if you want to create a new class go talk to someone else for example go talk to the object or in the general So at least a bit Left their office and went into the office of the object or in the general and her office was filled of Computers laptops cameras Spaceships bikes drones. Yes, that's a spaceship interesting Well, and the object or in the general she is a Very important person and also on the on the kingdom. She is one Person who designed and build the first automated jumping robot She also helped design a drone that was able to deliver a pizza within five minutes of leaving the oven So she's one of the wise developers of the kingdom and she's going to help Ellie with the problem Hi, Ellie. How are you? I was expecting you. Oh, yeah, the king sent me and I'm here to solve the problem with the moon And as they started talking about the problem Elizabeth described the issue with the moon how She couldn't find a class how the metaprogramming minister didn't want to create a new class and The object or in the general Started talking to Ellie and they talk about the benefits of objects or in the programming and how Defining on translating the rules from the world into a computer. Just basically using building blocks to compose a system and you start by Adding a simple piece and then you add another one and another one until you have a very complex system Or in this case the system that created the metaprogramming minister was very complicated to understand I'm very complicated to change So what they did is they divide and conquer or they come up with a first version of the moon class It was very simple and it has just a few properties and we are just going to focus on the method visibility from earth And again, it's the visibility from earth that we talked to the orbit and we'd get the air position so Where is the bug? How can we find the bug and They started looking and they found out that in the Yamao file There is a part that defines gravitational forces gravitational forces and there are two objects. It's not just the Sun who affects the how the moon behaves so it's the Sun and the Earth and this is something that the code was not taken into consideration If you remember the Sun or the orbit of the moon was being just affected by the song gravitational field So the object or intergeneral tells Tully that she has to complete the class, but she also in order to fix this has to Create a method orbit or find a way to define the orbit in a way that is That satisfies these two classes two issues the first one is that We need to find out where the moon is going to be and also where the moon has been And this is something that we cannot currently do with the current code and the only issue is that there is more than one gravitational source and The object or intergeneral she suggested that the first person to help Elizabeth with this issue is the Royal functional mathematician, so Elizabeth goes into the office of the object or internal mathematician and This office is just different from everyone else. It's just white everywhere just one screen one computer and one whiteboard And you have to know that the Royal functional mathematician. She's a very smart person She helped design and build the timing system that allows the trains to arrive on time But she also calculated the speed on which the King changes her mind and The last thing that she was working on was the distance between here and there No matter where you are and where here is she has a function for that And as Elizabeth was getting closer she realized that there were notes on the whiteboard about the moon. Oh Hi, Elizabeth. How are you? I was suspected you Yes, I see that. Why is there a moon diagram over there? Oh I thought you were going to see me because no one else could help you with the orbit of the moon. Oh Yeah, thank you, but how do you know that? Well, I just calculated the probabilities that someone else helped you with that And also, I don't think the object oriented general will help you with that and the metaprogramming minister is too busy She said Elizabeth started talking again about the problem how she created a small class But she found that the orbit method that she wanted to create Was very difficult for her to fill out to that's where the reason why she was there and the Royal functional mathematician ask Elizabeth, you know, where's the moon? Where right now is in the sky, but we cannot see it because it's daylight Yeah, we cannot see it you're right It's something here that is missing and we cannot see it because the time is not the right and Time is also a dimension So a better question to ask would be Where is the moon and what time is? What time is the moon in that plate? so one thing that they immediately do is they add time as a parameter to the Visibility from the earth and we are going to pass that parameter to the orbit and we're going to pass that parameter to the air position So that's the issue number one So now let me ask you again where is the moon and The moon doesn't have static x y or c coordinates. The moon is constantly moving So the best way to describe the moon is with a mathematical function In other words the best way to describe the moon could be with a proc for example So now we have an orbit method Where returns a proc and you kind of pass a parameter time to this proc This would be really useful for example to draw a map of the stars First we define a list of all the stars that you want to put on your map And then we already calculated the functions and then you just need to pass the parameter of When do you want to see the map of those are? So that's why an approach with this Like this that is very functional would work really well The next thing that they want to to work on is what objects are affecting the moon and Because of we already saw the Jamel versus the Sun and then the earth so they work on something else the Royal functional mathematician poses that they first they get a list of a collection of the stars that will affect and We friendly would call it the gravitational friends because they'll they'll They use their influence on to the star and then when we want to Calculate the total gravitational force. We just need to Go to all that collection Call the gravitational field for each of those stars and then soon all that up So as you can see the code that has been generated so far is a smaller is easier to understand and it's somehow functional and The Royal functional mathematician tells to Elizabeth Your homework now is to extract the orbit method into a separate class because other stars can also have Orbits you can also extract the gravitational fields because other stars also have Gravitational fields and the goal your goal is to have the moon class only whole methods that are unique That make the moon unique and just as a comparison the class moon is so much easier to change Than the solar system and the other thing is that if you modify the solar system You could crash the entire the world or the entire set of systems But if you modify the moon, you will reduce the risk of things that could go wrong So at least have it goes back into her desk and started doing her homework and She finally finishes Runs the test gets pull request stroke and even the Metaprogramming ministry of magic approves the pull request Surprisingly Wow Who can I talk about that? But since it is just a class and the king is ill it's high priority So she at least have it deploys the code and she's well she's deploying the code She's thinking about how metaprogramming is a convention tool. It allows us to abstract behavior it allows us to think into What would be a framework like if we had the opportunity to build one and this is something that rails for example is Technically good at it but when you're trying to Specialized object or when they're trying to find special cases or modify Something that has been already created then is when you get into problems Because the specialization in metaprogramming is not easy in the other hand object-oriented programming is creating Building blocks that will reuse and it will use the single repensability principle for example to create a bigger and bigger system and lastly Functional programming Focuses on behavior and there were a lot of people or there are a lot of people who talk about how functional programming Try to focus is on avoiding a state mutation But I think team said it very good earlier today that we have to focus on objects that be or objects that do that objects that have behavior and that's why Functional programming. I think the important part is focusing on the behavior of the object and as what Elizabeth was thinking about this The moon was rising in the sky So she run away to see the king and the king was standing looking at the window. Oh Elizabeth Thank you for fixing the moon You have been ordered to the kingdom and happiness to my heart. Let me talk to you about performance How come I have to wait 28 days to see the full moon? But that my friends is a story for another time