Topic Requested By: red_4900This article is going to cover three methods of programming: Object Oriented, Aspect Oriented, and Procedural. Of course, these styles have their benefits and their liabilities. But solutions often combine all these styles, in order to create the most managed code.
To those of you who know me: You know that I'm not much of a writer. I keep it on more of a "layman's terms" basis, keeping it short and to the point. This is why I'll be offering a demonstration, using the widely known "fizz buzz" example: Print numbers 1 to 100, printing "fizz" if the number is a multiple of 3, "buzz" if it is a multiple of 5, and "fizz buzz" if it is divisible by both 3
and 5.
NOTE: The solution offered
will be a little impractical. I need to break it down into more functions than necessary, to demonstrate the various styles. At the end of this entry, I'll provide a final solution, which is the simplest possible.
So what are they, put simply?Object Oriented Programming: A style of programming which is based around objects. This is the most common style of programming in the industry.
Aspect Oriented Programming: Often written to complement an OOP solution. Hard to explain in short, but basically, it's main use is to keep your OOP code organised, without having to scatter various
aspects of code across several objects.
Procedural Programming: This is the "old school" style of programming. Less commonly used now~ The main language (which is still used within the industry) which still focuses on this style is
C.
These sound interesting. How do I know which is best for me?Like I already said, solutions often combine these styles. The whole purpose is to create code that is well managed. Time to get into the more nitty-gritty stuff~
Procedural ProgrammingLike I said, this is
much less common than object oriented programming nowadays. However, this doesn't mean that procedural programming is without benefits! In fact, the main reason that I like C++ is that it allows the programmer to combine procedural programming with OOP. procedural programming is also known as "Functional Programming."
Basically, procedural programming is entirely a 1-way system. There's no major data grouping (the main benefit of OOP) so you're basically working with nothing more than variables, loops, and functions. (Everything but objects, basically)
The obvious benefit to procedural programming is that you don't
need objects for everything. Take a look at Java, as an example. Everything has to be contained within a class. What about if you just want one function to do something really simple? (Obviously, this is also a drawback of OOP, so you'll be reading this again in a bit)
The main drawback is that there is no major data grouping. With an object oriented language, it's easy to keep everything organised~ Variables and methods go in the appropriate classes.
Here's the previously mentioned example. Remember, this is
not the best way to tackle this problem. It's been broken down into various functions to create a better demonstration.
The key point to notice here is that our variable has to be passed around to various functions. OK, so this could be solved by a global variable, but no one likes global variables...

Procedural Demonstration
#include <stdio.h>
// no objects here!
void FizzBuzz(); // our function to perform the fizz buzz operation
bool ByThree(int); // a function to check if a number is divisible by 3
bool ByFive(int); // a function to check if a number is divisible by 5
void PrintFizz(); // a function to print the word "Fizz"
void PrintBuzz(); // a function to print the word "Buzz"
int main() {
FizzBuzz();
return 0;
}
void FizzBuzz() {
int i = 1;
for (i; i <= 100; i++) {
if (ByThree(i)) PrintFizz();
if (ByFive(i)) PrintBuzz();
if (!ByThree(i) && !ByFive(i))
printf("%d", i);
printf("\n");
}
}
bool ByThree(int x) {return (x % 3 == 0 ? true : false);}
bool ByFive(int x) {return (x % 5 == 0 ? true : false);}
void PrintFizz() {printf("fizz");}
void PrintBuzz() {printf("buzz");}
For more information:
http://en.wikipedia.org/wiki/Procedural_programmingObject Oriented ProgrammingOOP is a great way to manage your data. Put simply, it's a way to store variables, and methods together, to create an object. When creating your objects, you'll have to take into account how this object is going to need to interact with various other objects. This
could be considered a disadvantage, since it takes more planning. Also, it can sometimes be awkward to come up with the best way for your object to interact with another.
Remember the key benefit of procedural programming? Well it's time for a reminder.
When programming in a language which is
purely based around OO concepts (such as Java) then you might run into a bit of an annoying problem. What if you have a method to do something, which doesn't specifically
belong in any of your classes? Maybe it can be applied to more than one of your classes~ Why rewrite it in each class? However, there
are solutions to this. One way would be to use a "general" class, and call the methods from this class each time. Another way is to create a base class, containing these general methods. Then, each of your objects which require the method will inherit it from the base class.
Time for that example again. The key thing to notice now is that we no longer need to pass that variable around everywhere. It simply gets accessed from within the object.
Object Oriented Demonstration
#include <cstdio>
class FizzBuzz {
public: // our publicly accessable stuff
void Execute(); // our function to perform the fizz buzz operation
public: // our private stuff, that only the class really needs
int i; // the variable which we will use in our loop
bool ByThree(); // a function to check if a number is divisible by 3
bool ByFive(); // a function to check if a number is divisible by 5
void PrintFizz(); // a function to print the word "Fizz"
void PrintBuzz(); // a function to print the word "Buzz"
};
void FizzBuzz::Execute() {
this->i = 1;
for (i; i <= 100; i++) {
if (this->ByThree()) this->PrintFizz();
if (this->ByFive()) this->PrintBuzz();
if (!this->ByThree() && !this->ByFive())
printf("%d", i);
printf("\n");
}
}
bool FizzBuzz::ByThree() {return (this->i % 3 == 0 ? true : false);}
bool FizzBuzz::ByFive() {return (this->i % 5 == 0 ? true : false);}
void FizzBuzz::PrintFizz() {printf("fizz");}
void FizzBuzz::PrintBuzz() {printf("buzz");}
int main() {
FizzBuzz *fb;
fb->Execute();
return 0;
}
For more information:
http://en.wikipedia.org/wiki/Object_oriented_programmingAspect Oriented ProgrammingBasically, AOP is used to separate your solution into various
aspects. It best complements OOP concepts, as previously stated.
AOP is used to allow an application to grow as time progresses. It allows a programmer to modify a static object-oriented solution to create a system which is capable of growing and meeting new requirements.
Consider AOP to be a way to meet requirements which are later added to a specification. You will develop an object-oriented solution for a client, and a few years later, your client comes back to you, asking you to alter the application to meet some new requirements. Rather than going through all the old code, you will dynamically change the application.
No demonstrations here. The best way to explain is with an example of something
other than code.

The best way to explain AOP is to compare it to an object in the real world. Consider a packet of crisps. (That's potato chips, if you're in America

)
The manufacturers are always reducing the salt content, and making them taste better, right? Do you think they start from scratch when they make these changes?
More likely, they would make
alterations, instead of completely re-inventing the flavour.
For more information:
http://en.wikipedia.org/wiki/Aspect_oriented_programmingAnd after all that, we're finally done! Thanks for reading!