Hi Folks , Hope you all programming geeks are doing good. Today, we will discuss about the basic programming design principles and concepts that everyone should be aware about while developing a software . Well, Software Development ain’t easy. Programming/coding can be a real daunting task if not done in the right way. Hope you guys will like this post . So, without wasting time lets get started . [ i will be using JavaScript in here but you can use whatever language you are comfortable with ! ]
In this post, we will discuss about :
1. Classes and Objects
2. Constructors
3. Static Methods
4. Recursion
5. Loops
6. OOPS
7. Libraries/Modules/Packages (All mean the same thing)
A class is a blueprint for creating objects . It has member functions and member variables. And an object is an instance of class . One can access class member functions and variables with the help of object .
It is a special method that is used to initialize the newly created object and is called just after the memory is allocated by the object.
class Person{
constructor(name){
console.log('Hello from' + ' ' + name);
}
}
const p = new Person('karan'); // Output Hello from karan
They are indeed one of the most important methods that i use . They belong to the class itself. They can be used without the instance (object) of a class. They don’t modify the behaviour of our class but is very powerful when want to view certain data etc Just add static in-front of a function .
Recursion is a technique where a function calls itself . Their is a termination condition called the base condition which stops the recursive action .
class MathematicalOperation{
constructor(){
console.log('Mathmatetical operation class');
}
square(x){
return x*x;
}
factorial(x){
if(x==0 || x==1){
return 1;
}
return x*this.factorial(x-1);
}
static available_methods(){
console.log('This class has two methods. Square and factorial.');
}
}
const m = new MathematicalOperation; // output Mathmatetical operation class
console.log(m.square(5)); // output 25
console.log(m.factorial(5)); // output 120
MathematicalOperation.available_methods(); // his class has two methods. Square and factorial.
Loops in programming languages is a feature which facilitates the execution of a set of instructions repeatedly while some condition evaluates to true.
a = [1,2,3,4,5,6,7,8,9,10];
// For of Loop
for (let i of a){
console.log(i);
}
// While loop
let j = 0;
while(j < 10){
console.log(a[j]);
j++;
}
It is a methodology or paradigm to design a program using classes and objects. Main Components of OOPS are Encapsulation , Abstraction , Inheritance and Polymorphism . Lets look at them closely —
Encapsulation: Encapsulating every thing about an entity inside a class. ie. Putting functions and data members inside a class.
Abstraction: Abstraction is a very important concept in oops . It simply means that you are hiding the important things inside the class and provides only the necessary or relevant information that you want to show through a object . (It hides the details of implementation)
Inheritance : It is a concept where a class acquire properties of a child class . We can extend this child class and use already defined methods that are in the base class / Parent class . The main purpose of inheritance is to provide reusable code and help you achieve DRY
class Person{
constructor(name,gender){
this.name = name;
this.gender = gender;
}
getPersonName(){
return this.name;
}
getPersonGender(){
return this.gender;
}
}
class Male extends Person{
constructor(name){
super(name,"male");
}
}
class Female extends Person{
constructor(name){
super(name,"female");
}
}
// creating objects ...
const m = new Male("karan");
const f = new Female("Anna");
// checking results ...
console.log(m.getPersonGender()); // male
console.log(m.getPersonName()); // karan
console.log(f.getPersonGender()); // female
console.log(f.getPersonName()); // Anna
Polymorphism: It generally means ability to take many forms . There are generally 2 types of Polymorphism — compile time and run time .
1 . Compile time : Function Overloading is an example of compile time polymorphism. Same function name different parameter data types .
2. Run time : Function Overriding is an example of run time polymorphism . Let’s look at an example.
class Overriding{
print(){
console.log('this is print function');
}
print(){
console.log('this will override the above print function');
}
}
const override = new Overriding();
override.print(); // output : this will override the above print function
It is just a function or list of functions that are wrapped around or are written for repetitive work . Generally, SDE either worked with libraries or they create their own . Automating the boiler plated code .[ Most Important ]. Every programmer should know how to create a library else he would spent a lot of time in writing same code again and again and again and again ….
That’s it for this post. I hope you liked it . Once again, thank you for taking your time and reading this post. Good bye | Take Care | and keep coding