Total members 11889 |It is currently Fri Mar 29, 2024 1:24 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





The purpose of this document is to help you understand the basics of different styles of programming. In my opinion, the best programming style is object oriented programming. In this document I will discuss different kinds of developing, like Functional Programming, Modular Programming and eventually, Object Oriented Programming. Of course, all keywords and relevant information belonging to each style will be discussed.

In this document, different styles of programming will be explained:


  • Functional programming
  • Modular programming
  • Object Oriented Programming

Functional programming
What is functional programming?

Functional programming is a style of programming that emphasizes the evaluation of expressions rather than the execution of commands. This means that a program, written in a functional programming language, consists of a set function definitions and an expression, whose value is output as the program result. There are no side results (like an assignment) to expression evaluation, so an expression will always evaluate to the same value, if its evaluation terminates.
Attachment:
image002.jpg
image002.jpg [ 7.06 KiB | Viewed 4140 times ]

Because of this, an expression can always be replaced by its value without changing the overall result. This is called referential transparency.

The word "function" actually can be replaced with the term "action". A function executes something, for example Drive(brand_of_car)
Referential transparency

Pure functional languages achieve referential transparency by forbidding assignment to global variables. Each expression is a constant or a function application whose evaluation has no side effect, it only returns a value and that value depends only on the definition of the function and the values of its arguments.
Modular programming
What is modular programming?

A module, in modular programming, is a series of functions (or procedures) that are related in some way. This way of programming is used in many applications.
Way of work

Given a problem, you begin with analysis of the problem. You carefully look at the requirements of the problem and you make sure all questions are answered before you begin with design of the modules. Then, you break the problem in sub-problems. Each of these problems you solve in one or more modules. All the modules together solve the complete problem. Modules itself may be divided into smaller modules.
Attachment:
image004.jpg
image004.jpg [ 6.4 KiB | Viewed 4139 times ]

you divide the problem in sub-problems.

In the same way as you break the problem in smaller problems, you break a module in sub-modules.
Attachment:
image006.jpg
image006.jpg [ 5.87 KiB | Viewed 4139 times ]

A program (module) is divided in sub-modules.

As said, each sub-problem will be solved in the different sub-modules. In the end the whole problem is solved in the whole module.

A module is a set of functions (actions, see chapter two) that are related to each other. For example, a module called "Driving" contains the functions drive(brand_of_car), accelerate(speed) and stop() . A modulair program is always more clearly than a pure functional program because the different modules are classified on base of relevance.


Object Oriented Programming (OOP)
What is OOP?

Object Oriented Programming (or OOP) is a revolutionary concept that changed the rules in computer program development. OOP is organized around objects rather than actions. Normally, a program has a simple flow: input data, process the data and output a result. OOP has a different view; what we really care about are the objects we want to manipulate, not the logic required to manipulate them. Examples of such objects can be human beings (for example, described by name, address and so on), or buildings and floors (whose properties can be described and managed) or even little things, like a button or a toolbar.

Object Oriented Programming is an alternative to modular programming. The design technique associated with OOP is Object Oriented Design. In an object oriented program, the modules are classes rather then procedures. A class is a collection of objects.
Objects

As said, OOP is all about objects. An object is actually a container filled with information. You can see an object as a black box, which sends and receives messages. A black box, or object, contains code and data. Code are sequences of computer instructions, and data is information on which the instructions operate. In C ++ units code are called functions and units of data are called structures. Functions and structures are not connected to each other. For example, a function can operate on more than one structure, and a structure can be used in more than one function.

A structure can be public data, these can be accessed by another object. An object can also contain private data. This data can only be accessed by the object itself, and not by another object. The private data is implemented so that an outstanding object cannot modify data while it is not necessary or data that is not supposed to be modified.

Attachment:
image008.jpg
image008.jpg [ 629 Bytes | Viewed 4139 times ]

An object Messages

All the communication between objects, is done by messages. An object sends, but also receives messages. The object, to which the message is sent, is the receiver of the message. Messages define the interface of the object. At least everything an object can do is represented by his messages. Actually, an object usually can do more, because an object also can have private functions.

Attachment:
image010.jpg
image010.jpg [ 2.98 KiB | Viewed 4139 times ]

Messages

Normally, messaging is done between the objects. One object receives a message from an other object, and sends a message back to the same, or to an other object.
Attachment:
image012.jpg
image012.jpg [ 4.47 KiB | Viewed 4139 times ]

This is just a simple presentation of messaging between the objects. Even in a small program, the amount of objects can be very large. In this example, the first object receives input. This can be a message from another object or, for example, something the user types. The first objects processes this input, sends a message to the second object. This message can be a result of a function, for example. The second objects processes the message and sends a result back to the first object. With this result, the first object does a new calculation, for example, and sends the result of this to the third object. This object also processes the data and gives output. This output can be a new message to another object, or simply print something on the users monitor.
Information hiding

Providing access to an object by sending and receiving messages, while keeping the details of the object private, is called information hiding. Another word, which means the same, is encapsulation. Information hiding is a good thing; one object should only know that things about another object that are relevant. Make members in an object private or protected when possible.
Methods

An object is defined by his class. A class determines everything about the object. Objects are individual instances of a class. It is possible to have, for example, more buttons on one dialog. Each button is an instance of the buttonclass. Another example: consider the class "Dog". An object from this class is called "Spot". The "Dog" class defines what the object is. It is possible to have more than one object of this class; you might want to call them "Fido", "Rover" and so on. Each one of them is an instance of the class "Dog". Now, let's say that the "Dog" class defines they can understand messages like "Bark" and "Roll-over". The action that the message carries out is called a method. This is, in fact, the code that is getting executed when the message is received by an object.

Attachment:
image014.jpg
image014.jpg [ 2.36 KiB | Viewed 4139 times ]

spot.bark();

Arguments are often supplied as a part of a message. For example, the message "Roll-over", may need the arguments "how fast" and a second argument, "how many times". Arguments specify the way an action behaves.
Reuse

Inheritance also means, that you reuse. You don't have to write all the code again, you just reuse one or more classes that have the behavior you want.

Now, what if we want to create a class (named "Wolf") that can do exactly what "Dog" does? We don"t have to rewrite the entire class; we just create a class derived from "Dog". This class inherits all the existing messages of the so-called base-class; it has the same behavior.

In the class "Wolf" we may create new (extra) messages, for example "hunt". This class supports all the messages of "Dog", plus some new ones, including "hunt".
Attachment:
image016.jpg
image016.jpg [ 9.24 KiB | Viewed 4139 times ]

Data modeling

Data modeling is a first step in designing an object oriented program. As a result of data modeling, you can define the objects. A simple approach to creating a data model that allows you to visualize the model, is to draw a square to represent each individual data item that you know about, and then to express relationships between each of these data items. The relationships between the data items, can be expressed with words like is part of , or is used by or uses and so forth. From this total description, you can create a set of classes and subclasses that define all the general relationships.

With this data model, you can easily create a set of classes or even a complete program, because this data model defines the working of the program.
Differences between OOP and functional & modular programming

The difference between OOP and functional and modular programming, is that the objects in OOP only manipulate their own condition. For example: Peugeot::Drive(), Citroen::Drive(), Car::Accelerate(speed), and car::Stop() . The condition of all the objects together specify the condition of the complete program. An object oriented program is always easier to read than a modular program, because all the objects represents their information. The way an OO program works can be defined by drawing the relations between the objects. That is exactly why it is always a good idea to draw a data model with the objects, and their relations, you need.

Besides that, an OO program is easier to maintain, because changing the functionality of an object will (usually) have no effect on the state of other objects. For implementing new functionality, it might be enough to simply add on or more objects to the application and define their relations.

The fact of reusing code can be seen with the possibility to base specialized information (Peugeot, Citroen) on general information (car).
Nonsense

In C we had to code our own bugs. In C++ we can inherit them.



_________________
Please recommend my post if you found it helpful. ,
java,j2ee,ccna ,ccnp certified .


Author:
Expert
User avatar Posts: 838
Have thanks: 2 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Different Styles of Programming
 websites use codemiles styles     -  
 Line Styles in java     -  
 phpbb3 styles version upgrade     -  
 ALP programming     -  
 C++ programming     -  



Topic Tags

C++ Basics






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com