Posted inProgramming

what is solid principles? and solid 5 principles c#

Hi and welcome to the Pakainfo Engineering Tech Talks show I’m sure you’ve all heard about Solid principles for so many times but it’s not always clear what they mean right?

Hey it’s Jaydeep if you’ve ever released apiece of software at your company and another feature broke and no one knew why it broke or no one expected it to break, stay tuned I have a potential solution for that.

Hi Everybody first things first I have a new video editing software so if you noticed a difference a difference in the article let me know because it should be easier for me to figure out more on that on another article today I want to talk about a concept called SOLID but but first let me start with a little back story several weeks ago I talked to agentleman who was the SVP of engineering and he was making a differentiation in his company between engineers and programmers.

solid principles, solid design principles, solid principles c#, c# solid principles, solid principles java
solid principles

my name is Jaydeep and I’ma software developer at Pakainfo and today I’m going to show you those principles with a bunch of simple examples I’m sure you can all understand those five simple principles were originally written by Robert Martin and since then they’ve helped a lot of developers to become a much better object-oriented programmers.

SOLID stands for:

SOLID is an important solid principles information of object oriented programming languages. The 5 principles are below.

  • S – Single-responsiblity principle
  • O – Open-closed principle
  • L – Liskov substitution principle
  • I – Interface segregation principle
  • D – Dependency Inversion Principle

now if we put aside the fact that to be legally called an engineer you must pass the Board of Registration for the state you live in which is similar to a doctor or a lawyer engineers designs bridges and stuff are all licensed and requirements for that but separate from that I asked him I said well how was an engineer first program are different in your company any he’s like what engineers can solve real problems and for example do you know how to reverse a string without using a library and I’m like yeah you just convert to a character a and look to the they’re ready backwards I mean you don’t get in the door as a programmer at my company without knowing that and I don’t know if you like that answer.

and then It talked a little bit about surrogate pairs how you can make that interview question a little bit harder but then he then I mentioned a term solid and I think it went right over his head and solid is a principle from object oriented design and most of you watching aren’t gonna know this I don’t expect you know it I do expect someone who’s an SVP of software development to understand this concept and solid is an acronym for the single responsibility principle, the open-closed principle Liskov substitutioninterface segregation and dependency inversion.

solid, solid principles, solid definition, oop meaning, solid design
solid-principles

I’ll put some solid principles links underneath the video series so you can follow up on that and if you’re leading a company or if you’re an executive and you have developers and development managers working for you ask them to explain it to you and see if they understand it because unfortunately many people in software development don’t understand these principles and that is why a lot of times bugs happen because you’re not following the basic principles of encapsulation and inheritance and polymorphism as outlined in these five principles and you change a piece of code over here and it breaks something over-there and no one had any idea that these things were so tightly coupled and that’s a real problem.

 

Now how did this probably happen? Some people go to school for computer science and if you go for a decent program at a college you’re going to take a course on object-oriented design, you are going to take a course on data structures and such but but many people including myself, we came from different careers I came from civil engineering which is roadway and bridge design and I learned programming on my own and I had enough success to be to get a job at Microsoft as a programmer (SDET) but I didn’t know all these real concepts and you know I’ve learned them since but people who are self-taught or people who are programmers in maybe other language sand now have you know on-the-job training they’ve learned other you know new languages they some things don’t know these principles and you can learn them on Plural Sight (or Udemy, etc.)

Single-responsibility Principle (solid principles)

the first solid principle is single responsibility principle a class should have only one reason to change a class is what a class does the more class does the more likely it will be changed the more class changes the more likely it will introduce bugs this is why we need to keep in mind that the class should have only one responsibility this code was absolutely fine until the day.

class Circle {
public $interval;

public function construct($interval) {
$this->interval = $interval;
}
}

class Square {
public $length;

public function construct($length) {
$this->length = $length;
}
}
class LandConverter {

protected $shapes;

public function __construct($shapes = array()) {
$this->shapes = $shapes;
}

public function sum() {
// logic to sum the areas
}

public function output() {
return implode('', array(
"",
"Sum of the areas of provided shapes: ",
$this->sum(),
""
));
}
}
$shapes = array(
new Circle(2),
new Square(5),
new Square(6)
);

$areas = new LandConverter($shapes);

echo $areas->output();
$shapes = array(
new Circle(2),
new Square(5),
new Square(6)
);

$areas = new LandConverter($shapes);
$output = new SumCalculatorOutputter($areas);

echo $output->JSON();
echo $output->HAML();
echo $output->HTML();
echo $output->JADE();

I wanted to add rights to the shape I want that right method to write the shape into a file now our shape is doing two entirely separate things drawing and writing when the later changes all the class was compromised despite the fact the drawing was the same the solution is to make drawing and writing two separate classes thanks to this separation it will be easy to change shape whether writing or drawing logic needs to be changed.

Open-closed principle

the second solid principle is open closed principle software entities should be open for extension and closed for reduction it means that fresh behaviour comes with fresh source code not by updating the old code one back to my example.

public function sum() {
foreach($this->shapes as $shape) {
if(is_a($shape, 'Square')) {
$area[] = pow($shape->length, 2);
} else if(is_a($shape, 'Circle')) {
$area[] = pi() * pow($shape->interval, 2);
}
}

return array_sum($area);
}
class Square {
public $length;

public function __construct($length) {
$this->length = $length;
}

public function area() {
return pow($this->length, 2);
}
}
public function sum() {
foreach($this->shapes as $shape) {
$area[] = $shape->area();
}

return array_sum($area);
}
interface SRPinterconnection {
public function area();
}

class Circle implements SRPinterconnection {
public $interval;

public function __construct($interval) {
$this->interval = $interval;
}

public function area() {
return pi() * pow($this->interval, 2);
}
}

let’s take a look on how writer is implemented what if I would like to change the library I used to write with whatever life to print to the screen instead of writing to a file in either of those options concrete implementation details of writer would have to change so how do we achieve it rely on abstractions whether it’s full abstraction like interface or partial obstruction like abstract file the solution would be to abstract writer.

public function sum() {
foreach($this->shapes as $shape) {
if(is_a($shape, 'SRPinterconnection')) {
$area[] = $shape->area();
continue;
}

throw new LandConverterInvalidShapeException;
}

return array_sum($area);
}

here we can see that interface writer with two different data the permutations remember obstructions are always better than referencing concrete classes.

Liskov substitution principle

the third solid principle is Liskov substitution principle sub types must be substitutable for their base types well basically it means that if you want to replace a class with a subhead class the behaviour should stay the same let’s go back to our example here we have rectangle that has two properties width and height and also one method called calc area now for this square although a square is a rectangle.

class VolumeCalculator extends AreaCalulator {
public function construct($shapes = array()) {
parent::construct($shapes);
}

public function sum() {
// logic to converter the volumes as well as then return with array of output
return array($summedData);
}
}
class SumCalculatorOutputter {
protected $calculator;

public function __constructor(LandConverter $calculator) {
$this->calculator = $calculator;
}

public function JSON() {
$data = array(
'sum' => $this->calculator->sum();
);

return json_encode($data);
}

public function HTML() {
return implode('', array(
'',
'Sum of the areas of provided shapes: ',
$this->calculator->sum(),
''
));
}
}

it has some different behaviour Square has the same value for width and height take a look at the test here do you see what’s wrong although the client expected to get 18 he gets nine and this is exactly Liskovsubstitution violation square and rectangle are not substitutable a proper solution solid principles will be that both square and rectangle would implement interface shape.

$areas = new LandConverter($shapes);
$volumes = new LandConverter($solidShapes);

$output = new SumCalculatorOutputter($areas);
$output2 = new SumCalculatorOutputter($volumes);
public function sum() {
// logic to converter the volumes and then return and array of output
return $summedData;
}

here shape has only calc area and it won’t hold any size properties like width and height then the square could have only one property.

Interface segregation principle (solid principles)

the fourth solid principle is interface aggregation principle classes that implement interfaces should not be forced to implement methods they do not use another way to say it use small interfaces not fat ones let’s illustrate it suppose shape has all of these different abilities colour move rotate and suppose not all shapes have them clients should not be forced to depend upon interfaces that they do not use instead of creating a shape and throwing an exception when it doesn’t have the specific ability we just need to separate all these abilities into separated interfaces we can easily compose the right behaviour for each shape with composing those interfaces together.

interface SRPinterconnection {
public function area();
public function volume();
}
interface SRPinterconnection {
public function area();
}

interface SolidSRPinterconnection {
public function volume();
}

class Cuboid implements SRPinterconnection, SolidSRPinterconnection {
public function area() {
// converter the surface area of the cuboid
}

public function volume() {
// converter the volume of the cuboid
}
}
interface ManageSRPinterconnection {
public function converter();
}

class Square implements SRPinterconnection, ManageSRPinterconnection {
public function area() { /Do stuff here/ }

public function converter() {
return $this->area();
}
}

class Cuboid implements SRPinterconnection, SolidSRPinterconnection, ManageSRPinterconnection {
public function area() { /Do stuff here/ }
public function volume() { /Do stuff here/ }

public function converter() {
return $this->area() + $this->volume();
}
}

Dependency Inversion principle

the fifth solid principles and last principle is dependency inversion principle high-level modules should depend on abstractions and not on details now back to our example remember writer writer was dependent on how we write into a file in Java the solution was to abstract out writer and separate it from the specific logic.

class PasswordReminder {
private $dbConnection;

public function __construct(MySQLConnection $dbConnection) {
$this->dbConnection = $dbConnection;
}
}
interface DBConnectionInterface {
public function connect();
}
class MySQLConnection implements DBConnectionInterface {
public function connect() {
return "Database connection";
}
}

class PasswordReminder {
private $dbConnection;

public function __construct(DBConnectionInterface $dbConnection) {
$this->dbConnection = $dbConnection;
}
}

this source code inverting the dependency instead of depending on a specific implementation we’re dependent on an interface this allows us to swap implementation or even more creative class very easily so to conclude this last principle sums it all up so if you like my presentation so far there is a bunch of simple principles.

you can learn them on My great Website it’s not that hard to kind of put the investment in and learn solid principles it so I’m gonna kick and include some links down below including some talks from Bob Martin who is one of the signers of the Agile Manifesto.

He wrote some books on this and some else include some links from him but it’s great it’s a great concept solid principles to know and it helps it really help your company if you can get your programmers understanding this and coding to this for these principles so stay tuned for more please comment below – if you have questions if you want to hear more about these principle I didn’t really want to get too technical in this Article but if you do wanna I mean just split them up into different videos if that will help let me know let me know what else you want to see thanks you

I hope you get an idea about solid principles. I’m sure you’ll be thrilled to read the boat and remember use those principles wisely in real life situations are much more complicated so hold those in mind and come up with the best solution you can hope you enjoyed this articles and if you have any question or a comment just leave it here below and I will be happy to answer see you at the next Pakainfo Talk Shows bye bye.

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype