 So I did a quick search there. I think I remember it used to be a community addition of sugar CRM Yes, there's a community addition which is free to download free to use Yeah, so anyway, so let me try and get my slides up Right, so for those of us who have been programming for a while, you might have heard about this principle for those who are new to this, we'll try to introduce a bit more about what it is. It will not be very detailed it will not be very in depth because I feel many more smarter people on the internet has actually talked about it and has given more great examples about this. So what is solid? Solid is basically, it stands for single responsibility, open-close, list-cops, substitution, interface over interface segregation and dependency inversion. These are essentially guidelines that helps you in designing your code This is great. It was coined by Michael Fedders and based on the first five principles of object-oriented programming and design by Robert C. Martin or affectionately called Uncle Bob. So Uncle Bob wrote a bunch of books about this, about object-oriented design and programming and design and he coined a couple of terms and talked about some basic principles and Michael Fedders felt that it could be summarized and organized into a word called solid. It's basically a guideline that helps you in designing your code, removing things that could smell and can cause use in a way or to refactor your code. It helps you in making your code a bit more readable and more extendable because as developers work in a team we want to make sure our code is readable by our fellow developers and we can easily make changes to it. Essentially the problem we have with legacy code is the cost of change. The cost needed to make changes to your code is this can be very high if your code is not designed properly, it's not designed well rather. So solid is usually and basically a guideline of how you should look at your code as you create it or as you design it or refactor it. So these are some ways you can look at your code that you have right now how you can reorganize your code and refactor your code. So yeah again single responsibility open close. Today I'll just be touching on the first two is part one of a three part series hopefully. So first part will be about single responsibility and the next I'll be talking about open close. So what is the single responsibility principle says? This is taken from the wiki record Wikipedia entry. So every module or class should have a responsibility of its own. Basically it's over a single part of the application of the software and the responsibility should only be entirely encapsulated in that class or method. Essentially each class and method or module should only have one job. So the class should have only one reason to change as Uncle Bob says. So all services should be narrowly aligned with that kind of responsibility. So each class, each function, each thing that you do, each module that you create should only do one thing right and the class should only be doing one specific have one specific responsibility because when you mix responsibility to set into one class you may end up have you may end up running code which is so intertwined together that it's difficult to break them apart if the use case changes right. Here's an example of a simple class of shapes. So it's a circle class and a square class. Each of them implements its own specific thing that on the circle has radius that's constructed it passes it that in same goes for square it knows about the length so square has to look square like length and breadth are the same right. So this does that. So our application here is an area calculator given an array of different shapes calculate the total surface area of all the shapes in their array. So the array has a circle it's a square and you have also a certain length but you have a way you need to have a way of generating and summing up the total total area of all the shapes. So this is the the functionality of this particular class here. So you look at it it takes in in a constructor the array of shapes there's a function called sum which basically has some magic logic which we'll cover later that brings all of them together and of course the third part will be the output. So once I instantiate this class I would say hey here's an array of squares and circles give me a total total area and it will basically generate a text each one say you know this is all the all the areas to it. It's very simple my example is kind of contrived I can actually rip this off some website on the internet just to show you guys but I'll show you the source later I can go and look at the blog post. So this is how it's implemented very simple the array of shapes of radius 2 square of length 5 square of length 6 pass right into the a new instance of area, area calculator which then will output which will be total size. So that's pretty cool right so you look at implementation what is it doing it's actually generating you know strings so you have quite a kind of like this is here's a good example of each of this function doing a specific thing one responsibility what some do some basically sums up everything and output is the one that kind of like outputs the string right pretty simple. So what if you want to say instead of generating a HTML header whether you give me a JSON or XML or some j-template stuff so that's where it becomes tricky when you look at this class again right because the presentation layer is somehow quite it's somehow intertwined into the output so in this case the output only does is doing output is doing one thing correct it's showing you the output but then if we need to change the use case so here's where the cost of change comes in right if you have designed our code in this way without anticipation of what could possibly be useful in the future the cost of making this change to this class later it's going to be quite costly in terms of meta hours so one way to we can look at this is to how we have all been doing with things like library model view controller with separation of presentation logic and and everything else right so we can we can do that by breaking it up into another another class we have another class that is there just simply does the sum calculated output so basically that's that this particular class itself is basically doing that presentation layer the first logic is still the same it goes into the error calculator but at the result of that object it becomes a dependency in the next class which is the sum calculated output and with this output class we can actually implement various methods that generates JSON, Hamel and so on and so forth right so this is kind of like how you break up into different things like the calculation of the area is the domain or the responsibility of the error calculator and the presentation layer is now brought out into another class called a call the sum calculated output and this is do you have an example do you have an example yeah so essentially that's how we could kind of like break it up into a single responsibility per class rather than so you can go back your code in your sum legacy code you've done in the past you can look at it and say what have I been doing you know sometimes you can do you can ask yourself a simple question how many things is this function doing how many things is this class doing right so that little simple question can help you understand and maybe break it up into smaller things and of course the next the next problem you have will be what do I name the functions for the class one of the few things that are difficult in computer science naming things and all by one and cashing in my right so open close friends will any questions about single responsibility principle um have you you guys kind of have a general understanding okay so next thing uh as we have covered as an all is open and open close principle what does that mean it means that um your software entities should be open to extension and close to multiplication open the extension as in we should be able to extend any class through inheritance or to um become making a power but another uh function another a dependency in another in another class and it should be close to multiplication basically if you design your your class in those ways that it's doing only one thing you can have a mixture of these classes put together and you can have different functionalities coming out of it right so that's where so when you so you want your classes to remain unchanged as much as you can and that's where you need to think about how you should design your this will be quite instructive how you should design your classes so class should be extendable without modifying the class itself i know it's really hard it's not easy um i've tried this i look at a code that i just returned i just and i look at it again another day and like oh i should i really should i really shouldn't be doing this here um yeah because sometimes you're right you're once you've reached a code you could if you have time which is a luxury you have time you can think about so how would this code be used by others in the future hmm yeah so there'll be the questions you can kind of get to start you thinking um right so you can mix classes by inheritance or dependency injection it's basically to create new behaviors so let's go back to our example so here we are basically showing you an example already the earlier example didn't show uh any i didn't implement the sum so here's the sum uh some implementation written by somebody so what is it doing right now in the sum uh remember the function we had earlier the area calculator takes an array of shapes and this in this dodge this shapes is basically passing that in and every single time is actually checking for square are you a square oh yeah do this are you a circle yeah so there's a if else kind of matrix and why it makes an oval or trapezoid yeah so which means you have to start now they find this code to add more cases we can say ah very simple ah we just switch to the switch class a bit of switch function it's easy la switch la not really yeah so which means your code could potentially grow in this piece of code here right now could potentially grow and grow and grow so is this a good way of doing things yes no okay i'm glad you guys you guys agree it's not good yes okay so let's take one step back and say look at our code again say area area is should the shape should aren't isn't that the the idea of an area something that only is the responsibility of the shape itself a shape class a shape type right should kind of like maybe you should implement some you should know about his own dimensions he knows all his own attributes so he could easily implement some form he should know about things like his own area right so this is how we can kind of refactor this and say let's go back to our square class and say no you shouldn't actually you should know about how big you are you should know about your service area so let's implement a new method called area right and which in this case you come out area of this of the square and once he knows that you can pass it in and in every time we you basically instead of asking that question in the area doing that calculation in the area calculator we're basically asking the shape to tell us that information hey shape what is your area and we have that information we also think it's a big step one step further because we know that there could be some there could be new shapes that we will create in the future we will create a trapezoid pentagram pent pentagram and many other shapes oval and all that stuff so all these things they should probably all use the same interface they should all implement the the area attribute right should all be implemented area attribute so how do we enforce this the way that we the way that we enforce this is to use a why did your own project okay okay it's great a while so yeah you understand right so so interface so instead of instead of actually checking every single class that you create we kind of use a programmatic will and forcing that you must implement a area function right so using an interface class is basically telling you you should actually do that let's give it a while right so but it still requires a programmer to actually implement the shape so that it just like we have a new we create a new shape it must shape must implement shape interface but why if some programmers in our team are quite new to the team they haven't actually learned about the you have to use the word interface you implement this particular interface why you have someone your team that has doesn't know about that so which means you have to do the other thing which is to code defensively right so basically write some code later on okay cool it's okay right so uh yeah so once again we have the shape interface the shape interface it's shape interface uh now the circle and square should move into both implement the shape interface and it can't mean that it must because the way that we derive area it differs from shape to shape so which which means we have to implement this in each of our shape and the interface will be found and sure that we actually use this if we miss it out or we create a we implement we create a new shape without uh you without clearing an area function the it should basically break it should basically cause an exception somewhere right and the code defensively is to basically help help us ensure that uh in the future if anyone else uh accidentally create a new shape without implementing the interface uh this defensive piece of code over here will basically make sure that are you a shape interface do you implement the shape interface interface you don't i will throw an error or i'll sweep bloody hell you know the good thing about this is also you'll be it's very cool that in the future what if you in the future you need to oh right instead of show i also need to implement something new like say how much it costs to buy that shape for example you can implement a shopping cart and you want to be able to come up with uh you want to say the price to get the price of each of the shape is a contrived example but come a barrier there so how do you how do we extend this how do we extend this without making any changes well we could actually you know go go there have a more interface function and say the price and which means you can go down to all your implementations and add the price function right just kind of like helping you defensively fix this um and of course after it doesn't implement the shape uh interface which will show that a an exception which is a rather long exception i need to write this yes store it on the internet internet is never wrong anyway yeah so um coding defensively uh that was actually that's actually all of all that i have so once again the recap we will talk about uh single responsibility and then we will just talk about uh open and open ghost principle right i'm supposed to talk about this probably with to be continued in the next next round uh and here are the references you want to read more about this the blog post where i stole all the code samples from um yeah so yeah i can take a picture yeah cool so right now questions any questions for me yes quite a bit like based on some previous uh let's say we have a set of requirements that allows that makes us as the mission class for interfaces and it means that necessary that must consist of uh some class kind of what happens when uh requirements changes so we have to be actually the current class like typically like maps do you talk about abstract class or interbase class um i would say abstract classes yeah of course abstract classes it's actually um correct me am i wrong what happened i've been doing i've not been doing p3 for a bit too long so i keep forgetting what and if there's been an interface and abstract abstract class is a class that has it cannot be it cannot be instantiated while you implement the interface so all the other classes were so-called s10 the abstract class i want to comment yeah you are very careful about single whether multiple inheritance are accepted if it's not that only p3 doesn't have multiple inheritance um you have a problem that you might preferably want to prefer using inheritance rather over the shared classes yeah that's true uh but inheritance only makes sense if the way that we derive the area is the same but in this case the way if it's the way that we derive the the implementation of area is different for each shape right so there's no way we can do an inheritance in this case but if we know that the price will always be the same for all of them we can make it inherit a parent class which has a price uh method for example if we know that the price for all the shapes are the same make just make them all uh inherit um extend from a a class which has a common uh method can we really is abstract class actually no abstract class can instantiate it but you can have abstract class right some past there's some past it just that maybe at at least one of the functions with abstract there's no implementation right yeah so in the case every every function will be and stretch okay we can try to come to the right side yeah but the composition is favored to inheritance of because uh once we go down to say uh we have grandchildren classes then after that you will you have a nightmare this uh you have an abstract class that implements this interface then a extend uh abstract class b extend c extend b then after that it's cool to be i i'm in favor of i'm in favor of base class you can implement the base class i'm in favor of that i think in php five point something they introduce traits traits is also nothing you can use in in extending functionality so traits will be a small chunk of code that has only a responsibility and you can make you can add your traits to the to each of the class that they require is that that additional functionality yes yeah but for me it's right because right it's it's like mutual inheritance so virtual narrative but uh you can do that with composition uh independently which makes sense so so because with threads you you could have a class with several responsibility and i think you need to try a bit of traits uh the thing is uh you can't really you think at least like you can't really modify let's say the signature i say i have a method that takes in two arguments so let's say when i use it in another class i want to change the type of the arguments or change the three arguments like there will be some it's a bit problematic uh can't you overwrite okay overwrite the i know i'm sure the band i was trying to see that yeah i go hey with new fangirl stuff use judiciously yeah i'm kind of still kind of old-fashioned yeah um again if you use any of that just be careful about using it remember about single responsibility use maybe if you're doing that you're adding traits into your class it could be an indication that your class might be doing a bit too much right you might want to think about maybe make a different class and having a way of emerging or compositionally combining them together in some other some other class or some other way to get the functionality that you need rather than extending it or adding new stuff to it maybe it's about time to break it up to smaller classes to think about how you could use them compositionally to get the results that you want right you know all these boys out there could could design the experience yep more comment yeah a way that can you tell how long your code your class century is yes you really want to go to that level i can't you don't need to say you don't need to read if i can tell how the label tries to block them we really want to go down the level okay fine i'll tell you that was a keyword solid right this is a bunch of guidelines uh written by prepared by sandy mats in the roomie world and smoke up world but i'll just show it here so some uh all rules la rules and these are the rules oh i'm good yeah okay right so i wouldn't say it's bad i'm just thinking that it could be a way where that you can enforce certain programming um standards in your team i wouldn't i wouldn't recommend this totally it's just a guideline but it's a very interesting way of helping you think about how okay so it's like if your class is above 100 lines of code which means you've got screw a little bit too much right it's an indication that maybe your class is doing it too much um if your methods are more than five lines so so maybe an indication it might be doing it too much okay i'll i'll i'll this is this is applied in the ruby world so i would i would actually maybe the factor of two of this would be uh too much optimization we're talking about optimization versus readability right so right so four programs for method uh one instant variable for rails view this is actually what it's like it's in the ruby world right so for like maybe there's another like uh and two two class two two class names for again these are rails one mentions but yeah um but look at the first three yeah so um yeah it's a simple rules because you know do where you again when your class gets a bit too big it might be an indication that you're doing too much in the class you know so you might think about breaking it up uh one and five lines or ten lines maybe also by communication we're doing too much again these are guidelines but it's more like from based on this you can look at your code again and say are you doing too much yeah are you doing more than one thing um yeah sometimes having more classes is actually a bad thing okay one question but it's quite easy to find code right it's about your your that that boils down to your proficiency with your if your coding tool right you will using PHP storm it's very easy to do fuzzy search and find files even with supply and text it's very easy to find files that you need right uh if you're using PHP storm it gets command click go to the class there it's more proficiency in your in your in your weapon of choice right which is a whole other topic together yes uh doesn't it mention the close for modification uh any mention would be uh i know ruby has this uh feature where your class is already fine in one class today after that you can so-called like so-called modify the class outside right something like that you you quite busy for some data mix it yeah mix it you can but mix it is very much like traits yeah some of the traits yeah some mixings in ruby is very much like traits in PHP so yeah which is how you modify the behavior so yeah okay one yes uh purpose of uh open-source principles is the when you use uh you know it's very dense we use uh uh dependency manager uh library manager and when we use we use one of our library tools uh one one that you're very well made and respected that open-source we can easily extend that also they do uh without the doctors it's good i think it's also one of the main purpose of this program dependency injection that's eventually okay this is a symphony right although there's something else all the famous have dependency uh injection managers yeah i haven't done php in a while earlier my age is showing yeah but um thanks so uh any more questions about or comments or refutations so you feel that i'm not done enough justice to these topics okay the last one um about uh yeah uh defensive coding defensive coding yeah okay uh yeah why uh why do you do that uh and i know in the system because uh uh there's a school of thought that says constructors should be lightweight you shouldn't do too many things in the constructor yeah right the validation and this is what you're talking about is what we're doing here is a validation which i'm even of i'm even one to say the validation of this thing should be done in a different function right right now is it's going defensively yes but there should be a validation that maybe happens above this before the 4h you should actually do a validation for all the uh the classes all the all the objects we passed it because actually for his calculator he's constructed sticking in an array of uh shapes so in order for php like you come we don't really have a uh type of array so in order for you to validate you to go through a node so you mustn't do it here if it's like passing in one object like a shape collection class like that competes are done then the compiler will actually catch you so not compiler okay yeah but it's a good point and uh and uh and now with the actually fiber we can type a seven yeah the injection in fiber also oh for for classes then for 237 you can type in the uh the scalar type string in the constructor but if it's a real you can check the contents of the way no cannot cannot cannot cannot cannot okay fine unless you have a collection uh class so there's a number spr no wait that's a good idea then you can put the validation inside the yeah so what they're saying is instead of uh using take of this cup is a constructor taking in an array of shapes uh we should make it uh another class which is a collection class which could have then implement its own validation inside when you add which uh shape to it right so yeah add shape and add add function or into that collection will actually do a validation that'd be a good thing that's actually a good design pattern to go for which is uh you look at my video in the live a few meetups ago what i talked about a primitive obsession right so it's about not doing not not have not don't stop stop passing arrays around but look at how you can use an actual class which has uh functionalities and it's going no bad oh my god the mines okay it's fine i got i but i'm fine all right uh are we done i think i'm i think i'm done i'm done so uh what do you guys think you did i give a good enough exposition about uh the the first two alphabets of of solid okay great thank you very much um and i'll be called uh talking about the other three stuff uh soon uh before i uh there's one more speaker pasindu is to be sharing with us a little bit about something you say it needs what 10 minutes