<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Saurav Ghimire</title>
    <description>The latest articles on DEV Community by Saurav Ghimire (@sauravvgh).</description>
    <link>https://dev.to/sauravvgh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F761379%2Fb8844376-ca88-4c60-812c-9aa056cee656.jpeg</url>
      <title>DEV Community: Saurav Ghimire</title>
      <link>https://dev.to/sauravvgh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sauravvgh"/>
    <language>en</language>
    <item>
      <title>OOP and it's use cases</title>
      <dc:creator>Saurav Ghimire</dc:creator>
      <pubDate>Fri, 26 Nov 2021 18:46:45 +0000</pubDate>
      <link>https://dev.to/sauravvgh/oop-and-its-use-cases-114a</link>
      <guid>https://dev.to/sauravvgh/oop-and-its-use-cases-114a</guid>
      <description>&lt;p&gt;OOP is an abbreviation for the word "Object oriented programming".&lt;br&gt;
it is based on the concept of "objects", which basically contains data and code:&lt;br&gt;
data :-&amp;gt; data is in the form of fields (such as attributes and properties) &lt;br&gt;
code:-&amp;gt; in the form of procedures/processes also known as methods.&lt;/p&gt;

&lt;p&gt;a feature of objects is object's own procedures which can access and often modify the data fields or itself(objects have a notion of "this" or "self").&lt;br&gt;
The four basics of object oriented programming are:&lt;br&gt;
1.Encapsulation&lt;br&gt;
2.Abstraction&lt;br&gt;
3.Inheritance&lt;br&gt;
4.Polymorphism&lt;/p&gt;

&lt;p&gt;1.Encapsulation&lt;br&gt;
-&amp;gt;The word 'encapsulate' means to enclose something. just like a pill "encapsulates" or contains the medication inside of its coating. In similar way, in OOP: by forming a protective barrier around information contained within a class from the rest of the code.&lt;br&gt;
what is encapsulation used for?&lt;br&gt;
-&amp;gt;Object oriented programming uses this aspect for hiding sensitive data and displaying only that particular data that is necessary for the user at that particular time of work.&lt;br&gt;
Encapsulation can work with a property known as "Access Modifiers". Access modifiers are there to modify one's access towards certain data or method within the class, outside the class, within derived the class etc. &lt;br&gt;
These modifiers are mainly private, public and protected access modifiers and are specified along with class specification.&lt;/p&gt;

&lt;p&gt;Eg. of Encapsulation&lt;br&gt;
Class Car{&lt;br&gt;
private: Engine E; //private data&lt;br&gt;
public:&lt;br&gt;
bool unlock: //public data&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
.&lt;br&gt;
car(){ // public method &lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
encapsulation is a good coding practice and it increases the security of your data.&lt;/p&gt;

&lt;p&gt;2.Abstraction&lt;br&gt;
-&amp;gt;Abstraction is an extension of encapsulation. It is the process of selecting data from a larger pool to show only the relevant details to the object. Suppose you want to create a dating application and you are asked to collect all the information about your users. You might receive the following data from your user: Full name, address, phone number, favorite food, favorite movie, hobbies, tax information, social security number, credit score. This amount of data is great however not all of it is required to create a dating profile. You only need to select the information that is pertinent to your dating application from that pool. Data like Full name, favorite food, favorite movie, and hobbies make sense for a dating application. The process of fetching/removing/selecting the user information from a larger pool is referred to as Abstraction. One of the advantages of Abstraction is being able to apply the same information you used for the dating application to other applications with little or no modification.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Inheritance&lt;br&gt;
Inheritance is the ability of one object to acquire some/all properties of another object. For example, a child inherits the traits of his/her parents. With inheritance, reusability is a major advantage. You can reuse the fields and methods of the existing class. In Java, there are various types of inheritances: single, multiple, multilevel, hierarchical, and hybrid. For example, Apple is a fruit so assume that we have a class Fruit and a subclass of it named Apple. Our Apple acquires the properties of the Fruit class. Other classifications could be grape, pear, and mango, etc. Fruit defines a class of foods that are mature ovaries of a plant, fleshy, contains a large seed within or numerous tiny seeds. Apple the sub-class acquires these properties from Fruit and has some unique properties, which are different from other sub-classes of Fruit such as red, round, depression at the top. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Polymorphism&lt;br&gt;
Polymorphism gives us a way to use a class exactly like its parent so there is no confusion with mixing types. This being said, each child sub-class keeps its own functions/methods as they are. If we had a superclass called Mammal that has a method called mammalSound(). The sub-classes of Mammals could be Dogs, whales, elephants, and horses. Each of these would have their own iteration of a mammal sound (dog-barks, whale-clicks).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Use case of Object oriented Programming:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Client-Server Systems&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Object-oriented client-server systems provide the IT infrastructure, creating Object-Oriented Client-Server Internet (OCSI) applications. Here, infrastructure refers to operating systems, networks, and hardware. OSCI consist of three major technologies:&lt;/p&gt;

&lt;p&gt;The Client Server&lt;br&gt;
Object-Oriented Programming&lt;br&gt;
The Internet&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object-Oriented Databases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;They are also called Object Database Management Systems (ODBMS). These databases store objects instead of data, such as real numbers and integers. Objects consist of the following:&lt;/p&gt;

&lt;p&gt;Attributes: Attributes are data that define the traits of an object. This data can be as simple as integers and real numbers. It can also be a reference to a complex object.&lt;/p&gt;

&lt;p&gt;Methods: They define the behavior and are also called functions or procedures.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Object-Oriented Databases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These databases try to maintain a direct correspondence between the real-world and database objects in order to let the object retain its identity and integrity. They can then be identified and operated upon.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Real-Time System Design&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Real-time systems inherent complexities that make it difficult to build them. Object-oriented techniques make it easier to handle those complexities. These techniques present ways of dealing with these complexities by providing an integrated framework, which includes schedulability analysis and behavioral specifications.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Simulation and Modeling System&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It’s difficult to model complex systems due to the varying specification of variables. These are prevalent in medicine and in other areas of natural science, such as ecology, zoology, and agronomic systems.  Simulating complex systems requires modeling and understanding interactions explicitly. Object-oriented programming provides an alternative approach for simplifying these complex modeling systems.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Hypertext and Hypermedia&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;OOP also helps in laying out a framework for hypertext. Basically, hypertext is similar to regular text, as it can be stored, searched, and edited easily. The only difference is that hypertext is text with pointers to other text as well.&lt;/p&gt;

&lt;p&gt;Hypermedia, on the other hand, is a superset of hypertext. Documents having hypermedia not only contain links to other pieces of text and information but also to numerous other forms of media, ranging from images to sound.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Neural Networking and Parallel Programming&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It addresses the problem of prediction and approximation of complex time-varying systems. Firstly, the entire time-varying process is split into several time intervals or slots. Then, neural networks are developed in a particular time interval to disperse the load of various networks. OOP simplifies the entire process by simplifying the approximation and prediction ability of networks.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Office Automation Systems&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These include formal as well as informal electronic systems primarily concerned with information sharing and communication to and from people inside and outside the organization. Some examples are:&lt;/p&gt;

&lt;p&gt;Email&lt;br&gt;
Word processing&lt;br&gt;
Web calendars&lt;br&gt;
Desktop publishing&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;CIM/CAD/CAM Systems&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;OOP can also be used in manufacturing and design applications, as it allows people to reduce the effort involved. For instance, it can be used while designing blueprints and flowcharts. OOP makes it possible for the designers and engineers to produce these flowcharts and blueprints accurately.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;AI Expert Systems&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These are computer applications that are developed to solve complex problems pertaining to a specific domain, which is at a level far beyond the reach of a human brain.&lt;/p&gt;

&lt;p&gt;It has the following characteristics:&lt;/p&gt;

&lt;p&gt;Reliable&lt;br&gt;
Highly responsive&lt;br&gt;
Understandable&lt;br&gt;
High-performance&lt;/p&gt;

</description>
    </item>
    <item>
      <title>SOLID Principles</title>
      <dc:creator>Saurav Ghimire</dc:creator>
      <pubDate>Fri, 26 Nov 2021 18:33:49 +0000</pubDate>
      <link>https://dev.to/sauravvgh/solid-principles-86p</link>
      <guid>https://dev.to/sauravvgh/solid-principles-86p</guid>
      <description>&lt;p&gt;SOLID principles: Solid principles are the combination of 5 major principles. They are as follows:&lt;br&gt;
1.Single responsibility principle&lt;br&gt;
2.Liskov substitution principle&lt;br&gt;
3.Open/closed principle&lt;br&gt;
4.Interface segregation Principle&lt;br&gt;
5.Dependency Inversion principle&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Single responsibility principle
-&amp;gt; Robert C. Martin expresses the principle as "A class should have only one reason to change" which means that every module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Code for single responsibility principle:&lt;br&gt;
//this is a bad practice&lt;br&gt;
class Usersettings{&lt;br&gt;
constructor(user){&lt;br&gt;
this.user= user;&lt;br&gt;
       }&lt;br&gt;
} changesettings(settings){&lt;br&gt;
if(this.verifycredentials()){&lt;br&gt;
//.....&lt;br&gt;
   }&lt;br&gt;
}&lt;br&gt;
verifycredentials(){&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;//the good practice here would be&lt;/p&gt;

&lt;p&gt;class UserAuth{&lt;br&gt;
constructor(user){&lt;br&gt;
this.user = user;&lt;br&gt;
}&lt;br&gt;
verifycredentials(){&lt;br&gt;
//...&lt;br&gt;
}&lt;br&gt;
class Usersettings{&lt;br&gt;
constructor(user){&lt;br&gt;
this.user = user;&lt;br&gt;
this.auth = new UserAuth(user);&lt;br&gt;
}&lt;br&gt;
changesettings(settings){&lt;br&gt;
if(this.auth.verifycredential()){&lt;br&gt;
//........&lt;br&gt;
   }&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
here a class has been assigned a single responsibility, that is one class is responsible for usersettings of adding new user, and the other class is responsible for verifying the user credentials.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Liskov substitution principle
-&amp;gt; Introduced by Barbara liskov, the principle states that "objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program. It means that if a program module is using a Base class, then the reference to the base class can be replaced with a derived class without affecting the functionality of the program module.
We can also state that the derived types must be substitutable for their base types.
Bad example where Liskov substitution is not used:&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;class Rectangle{&lt;br&gt;
constructor(){&lt;br&gt;
this.width = 0;&lt;br&gt;
this.height = 0;&lt;br&gt;
}&lt;br&gt;
setColor(color){&lt;br&gt;
//...&lt;br&gt;
}&lt;br&gt;
render(area){&lt;br&gt;
///....&lt;br&gt;
}&lt;br&gt;
setWidth(width){&lt;br&gt;
this.width = width;&lt;br&gt;
}&lt;br&gt;
setHeight(height){&lt;br&gt;
this.height = height;&lt;br&gt;
}&lt;br&gt;
getarea(){&lt;br&gt;
return this.width * this.height;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
class square extends Rectangle{&lt;br&gt;
setWidth(width){&lt;br&gt;
this.width = width;&lt;br&gt;
this.height= width;&lt;br&gt;
}&lt;br&gt;
setHeight(height){&lt;br&gt;
this.width = height;&lt;br&gt;
this.height = height;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
function renderLargeRectangles(rectangles){&lt;br&gt;
rectangles.ForEach((rectangle=&amp;gt;{&lt;br&gt;
rectangle.setWidth(4);&lt;br&gt;
rectangle.setHeight(5);&lt;br&gt;
const area = rectangle.getArea(); //Bad , returns 25 for square, this returns 20&lt;br&gt;
rectangle.render(area);&lt;br&gt;
});&lt;br&gt;
}&lt;br&gt;
const rectangles = [new Rectangle(), new Rectangle(), new Square()];&lt;br&gt;
renderLargeRectangles(rectangles);&lt;/p&gt;

&lt;p&gt;//The good way to do this would be ,&lt;/p&gt;

&lt;p&gt;class Shape{&lt;br&gt;
setColor(color){&lt;br&gt;
//......&lt;br&gt;
}&lt;br&gt;
render(area){&lt;br&gt;
//....&lt;br&gt;
  }&lt;br&gt;
}&lt;br&gt;
class Rectangle extends Shape{&lt;br&gt;
constructor(width, height){&lt;br&gt;
super();&lt;br&gt;
this.length = length;&lt;br&gt;
}&lt;br&gt;
getArea(){&lt;br&gt;
return this.length * this.length;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
function renderLargeShapes(shapes){&lt;br&gt;
shapes.forEach((shape)=&amp;gt;{&lt;br&gt;
const area = shape.getArea();&lt;br&gt;
shape.render(area);&lt;br&gt;
});&lt;br&gt;
}&lt;br&gt;
const shapes = [new Rectangle(4,5), new Rectangle(4, 5), new Square(5)];&lt;br&gt;
renderLargeShapes(shapes);&lt;/p&gt;

&lt;p&gt;3.Open/Closed principles:&lt;br&gt;
-&amp;gt;"Software Entities should be open for extension but closed for modification"&lt;br&gt;
The design and writing of the code should be done in a way that new functionality should be added with minimum changes in the existing code.&lt;br&gt;
The design should be done in a way to allow the adding of new functionality as new classes, keeping as much as possible the existing code unchanged.&lt;/p&gt;

&lt;p&gt;The bad way of the code writing in this would be:&lt;br&gt;
//Bad&lt;br&gt;
var iceCreamFlavors = ["chocolate", "vanilla"];&lt;br&gt;
var iceCreamMaker={&lt;br&gt;
makeIceCream(flavor){&lt;br&gt;
if(iceCreamFlavors.indexOf(flavor)&amp;gt;-1){&lt;br&gt;
console.log("Great success, there is icecream for you")&lt;br&gt;
}else{&lt;br&gt;
console.log("there is no icecream for you, epic fail")&lt;br&gt;
       }&lt;br&gt;
    }&lt;br&gt;
}&lt;br&gt;
//good way&lt;br&gt;
var iceCreamFlavors=["chocolate", "vanilla"];&lt;br&gt;
var iceCreamMaker ={&lt;br&gt;
makeIceCream(flavor){&lt;br&gt;
if(iceCreamFlavors.indexOf(flavor)&amp;gt;-1){&lt;br&gt;
console.log("Great Success, there is icecream for you")&lt;br&gt;
}else{&lt;br&gt;
console.log("There is no icecream for you , epic fail")&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
addFlavor(flavor){&lt;br&gt;
iceCreamFlavors.push(flavor);&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
export default iceCreamMaker;&lt;br&gt;
//if we see, then in the first version of the code, we dont have a place for adding an ice cream flavor, in order to add a flavor in the first code, we have to add an element in the  array list, which means that the code gets modified, but our open/close principle doesn't allow that to happen, a code can be extended but not modified. So, in the second code, we added a function 'addFlavor(flavor)' with the parameter flavor inside the bracket which can push a different flavor of icecream inside the array,which resulted in the addition/extension of the code but not modification. So the good code, or the second follows the Open/closed principle.&lt;/p&gt;

&lt;p&gt;4.Interface segregation principle&lt;br&gt;
-&amp;gt; Many client specific interfaces are better than one general purpose interface.&lt;br&gt;
We should not enforce clients to implement interfaces that they donot use. Instead of creating one big interface, we can break it down to smaller interfaces.&lt;/p&gt;

&lt;p&gt;//BAD&lt;br&gt;
class DOMTraverser{&lt;br&gt;
constructor(settings){&lt;br&gt;
this.settings = settings;&lt;br&gt;
this.setup();&lt;br&gt;
}&lt;br&gt;
setup(){&lt;br&gt;
this.rootNode = this.settings.rootNode;&lt;br&gt;
this.animationModule.setup();&lt;br&gt;
}&lt;br&gt;
traverse(){&lt;br&gt;
//....&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
const $= new DOMTraverser({&lt;br&gt;
rootNode:document.getElementByTagName('body'),&lt;br&gt;
animationModule(){} // Most of the time we don't need to animate while traversing&lt;br&gt;
});&lt;/p&gt;

&lt;p&gt;//GOOD way to do this&lt;/p&gt;

&lt;p&gt;class DOMTraverser{&lt;br&gt;
constructor(settings){&lt;br&gt;
this.settings = settings;&lt;br&gt;
this.options = options;&lt;br&gt;
this.setup();&lt;br&gt;
}&lt;br&gt;
setup(){&lt;br&gt;
this.rootNode = this.settings.rootNode;&lt;br&gt;
this.saveOptions();&lt;br&gt;
}&lt;br&gt;
setupOptions(){&lt;br&gt;
if(this.options.animationModule){&lt;br&gt;
//.....&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
traverse(){&lt;br&gt;
//.....&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
const $= newDOMTraverser{&lt;br&gt;
rootNode: document.getElementByTagName('body'),&lt;br&gt;
options:{&lt;br&gt;
animationModule(){}&lt;br&gt;
}&lt;br&gt;
});&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Dependency Inversion Principle
-&amp;gt;One should depend upon abstractions and not concretions.
Abstractions should not depend upon details whereas details should depend upon abstractions.
High level modules should not depend upon low level modules.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;//BAD code writing:&lt;br&gt;
class InventoryRequester{&lt;br&gt;
constructor(){&lt;br&gt;
this.REQ_METHODS = ['HTTP'];&lt;br&gt;
}&lt;br&gt;
requestingItem(item){&lt;br&gt;
//....&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
class InventoryTracker{&lt;br&gt;
constructor(items){&lt;br&gt;
this.items = items;&lt;/p&gt;

&lt;p&gt;//BAD: we have created a dependency on a specific request implementation.&lt;br&gt;
//we should have requestItems depend on a single request method:'request'&lt;br&gt;
this.requester = newInventoryRequester();&lt;br&gt;
}&lt;br&gt;
 requestItems(){&lt;br&gt;
this.items.forEach((item)=&amp;gt;{&lt;br&gt;
this.requester.requestItem(item);&lt;br&gt;
});&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
cost InventoryTracker = new InventoryTracker(['apples', 'bananas']);&lt;br&gt;
inventoryTracker.requestItems();&lt;/p&gt;

&lt;p&gt;//GOOD &lt;br&gt;
class InventoryRequester{&lt;br&gt;
constructor(){&lt;br&gt;
this.REQ_METHODS = ['HTTP'];&lt;br&gt;
}&lt;br&gt;
requestingItem(item){&lt;br&gt;
//....&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
class InventoryRequesterv2{&lt;br&gt;
constructor(){&lt;br&gt;
this.REQ_METHODS = ['WS'];&lt;br&gt;
}&lt;br&gt;
requestItem(item){&lt;br&gt;
//....&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;//By constructing our dependencies externally and injecting them, we can easily substitute our request module for a fancy new one that uses websockets.&lt;br&gt;
const InventoryTracker(['apples', 'bananas'], new InventoryRequesterV2());&lt;br&gt;
InventoryTracker.requestItems();&lt;/p&gt;

&lt;p&gt;What happens if we do not follow solid principles?&lt;br&gt;
-&amp;gt; Some of the cases may occur if we do not follow solid principles. They are:&lt;br&gt;
a. We may end up with tight or strong coupling of code with many other modules/applications.&lt;br&gt;
b.Tight coupling causes time to implement any new requirement features or any bug fixes and sometimes, it creates unknown issues.&lt;br&gt;
c. We may end up with a code that is not testable in nature.&lt;br&gt;
d. There would be a chance of duplication of code.&lt;/p&gt;

&lt;p&gt;what are the advantages of following the solid principles ?&lt;br&gt;
-&amp;gt; Following the solid principles helps us to:&lt;br&gt;
a. Achieve reduction in the code complexity&lt;br&gt;
b. Increase readability, extensibility and maintenance.&lt;br&gt;
c. Reduces error and implementations and reusability&lt;br&gt;
d. Achieves better testability&lt;br&gt;
e. Reduces tight coupling.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
