Subscribe to gabehabe's on-topic ramblings        RSS Feed
-----

Styles of Programming: Managing your Code

Icon 4 Comments
Topic Requested By: red_4900

This 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 Programming
Like 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... :P

#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....ral_programming

Object Oriented Programming
OOP 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.
#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....ted_programming

Aspect Oriented Programming
Basically, 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. :o

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....ted_programming




And after all that, we're finally done! Thanks for reading! :)

4 Comments On This Entry

Page 1 of 1

girasquid Icon

02 November 2008 - 11:07 PM
I think you may have missed a few styles...functional and imperative spring to mind.
0

gabehabe Icon

03 November 2008 - 05:13 AM
Procedural, functional, and imperative programming styles are very similar.

They all involve breaking the application up into procedures~ (functions, methods, whatever you want to call them)

More information: http://en.wikipedia....ive_programming

:)
0

acerola Icon

10 November 2008 - 05:34 PM

gabehabe, on 3 Nov, 2008 - 04:13 AM, said:

Procedural, functional, and imperative programming styles are very similar.

Not really.

There is imperative programming and declarative programming. They are different.

Imperative programming tells the computer how to do something. It involves algorithms, a step by step description of what is to be done.

Declarative programming tells the computer what to do, but not how to do it in details.

Procedural and Object Oriented programming are Imperative.

Functional programming is Declarative.

Procedural and OO are very similar. Just replace "variable" with "attribute", "procedure or function" with "method", etc. Just look at the code and you will see they are almost the same.

A pure functional language has no side effects. A side effect would be something like storing a value in some place in memory or printing something on the screen. This means that each function takes parameters and gives back results, only that. And a program is a chain of functions where the results of one are the parameters of another.
0

Raynes Icon

01 February 2010 - 12:57 AM

gabehabe, on 3 Nov, 2008 - 04:13 AM, said:

Procedural, functional, and imperative programming styles are very similar.

They all involve breaking the application up into procedures~ (functions, methods, whatever you want to call them)

More information: http://en.wikipedia....ive_programming

:)


This is really old, but this: "Like 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." is really, really wrong.

I'm not sure you quite understand what functional programming is (or at least you didn't at this time). I'm a bit surprised you haven't corrected this by now. It could seriously mislead people on this site.
0
Page 1 of 1

September 2014

S M T W T F S
 123456
78910111213
14151617181920
21222324252627
2829 30     

Request A Topic!

Want me to blog about something? Perhaps a language? A piece of software? A specific topic? Let me know! Even guests can post here on my blog!

If you would like to request a topic, please post a comment here and I'll get on it right away! smile.gif

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)

gabehabe's off-topic ramblings

Follow me on Twitter!
lol, my other blog died a horrible lonely death. Ah well.

Smiley of the [however often I change it]

IPB Image

Contact Me

e-mail: gabehabe@gmail.com

Google Talk: gabehabe@gmail.com
MSN: gabehabe@hotmail.com
Yahoo: gabehabe (rarely used)
AIM: gabehabe (rarely used)

Skype: gabehabe

Want me to work for you? [click]