Functional programming has the following problems.
- Reusability
- Extensibility
- Simplicity
- Maintainability
Reusability:
In Functional Programming, we need to
write the same code or logic at multiple places which increases the code
duplication. Later if we want to change the logic, then we need to change it at
all places.
Extensibility:
It is not possible in functional programming
to extend the features of a function. Suppose you have a function and you want
to extend it with some additional features then it is not possible. You have to
create a completely new function and then change the function as per your
requirement.
Simplicity:
As extensibility and reusability are
not possible in functional programming, usually we end up with lots of
functions and lots of scattered code.
Maintainability:
As we don’t have Reusability,
Extensibility, and Simplicity in functional Programming, so it is very
difficult to manage and maintain the application code.
How we can overcome Functional
Programming Problems?
We can overcome the functional
programming problems (Reusability, Extensibility, Simplicity, and
Maintainability) using Object-Oriented Programming. OOPs provide some
principles and using those principles we can overcome the functional
programming problems.
What Is Object-Oriented Programming?
Let us understand Object-Oriented
Programming i.e. OOPs Concepts using C#. Object-Oriented Programming (OOPs) in
C# is a design approach where we think in terms of real-world objects rather
than functions or methods. Unlike procedural programming language, here in
oops, programs are organized around objects and data rather than action and
logic. Please have a look at the following diagram to understand this better.
Reusability:
To address reusability, object-oriented
programming provides something called Classes and Objects. So, rather than
copy-pasting the same code again and again in different places what you can do
here is, create a class and make an instance of the class which is called
object, and reuses them whenever you want.
Extensibility:
Suppose you have a function and you want to
extend it with some new features that were not possible with functional
programming. You have to create a completely new function and then change the
whole function whatever you want. In OOPs, this problem is addressed by using
some concepts called Inheritance, Aggregation, and Composition. In our upcoming
article, we will discuss all these concepts in detail.
Simplicity:
Because we don’t have extensibility and
reusability in functional programming, we end up with lots of functions and
lots of scattered code. In OOPs, this problem is addressed by using some concepts
called Abstraction, Encapsulation, and Polymorphism.
Maintainability:
As OOPs address Reusability, Extensibility,
and Simplicity, we have good maintainable code and clean code which increases
the maintainability of the application.
What are the OOPs Principles or OOPs Concepts
in C#?
OOPs, provide 4 principles. They are
- Encapsulation
- Inheritance
- Polymorphism
- Abstraction
OOP stands for Object-Oriented Programming.
Procedural programming is about writing procedures
or methods that perform operations on the data, while object-oriented
programming is about creating objects that contain both data and methods.
Object-oriented programming has several advantages
over procedural programming:
- OOP is faster and easier to execute
- OOP provides a clear structure for the
programs
- OOP helps to keep the C# code DRY "Don't
Repeat Yourself", and makes the code easier to maintain, modify and
debug
- OOP makes it possible to create full reusable
applications with less code and shorter development time
·
What are Classes and Objects?
·
Classes and objects are the two main
aspects of object-oriented programming.
Class :- Car
Object volvo ,Audi,Toyota
Accept
a class is a template for objects, and an object is
an instance of a class.
When the individual objects are created, they
inherit all the variables and methods from the class.
C# is associated with classes and objects,
along with its attributes and methods. For example: in real life, a car is an
object. The car has attributes, such as weight and color, and methods,
such as drive and brake.
A Class is like an object constructor, or a
"blueprint" for creating objects.
Methods normally belongs to a class, and they
define how an object of a class behaves.
Just like with fields, you can access methods with
the dot syntax. However, note that the method must be public. And remember that we use the name of the method
followed by two parantheses () and a semicolon ; to call (execute) the method:
The public
keyword is called an access modifierThe public
keyword is an access modifier, which is used to set the access level/visibility for
classes, fields, methods and properties.
Modifier |
Description |
public |
The
code is accessible for all classes |
private |
The
code is only accessible within the same class |
protected |
The
code is accessible within the same class, or in a class that is inherited
from that class. |
internal |
The
code is only accessible within its own assembly, but not from another
assembly. |