Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use OO Perl?

Tags:

oop

perl

I'm just learning Perl.

When is it advisable to use OO Perl instead of non-OO Perl?

My tendency would be to always prefer OO unless the project is just a code snippet of < 10 lines.

TIA

like image 541
hourback Avatar asked Nov 18 '08 17:11

hourback


People also ask

What is OO Perl?

Object-oriented programming: As the name suggests, Object-Oriented Programming or OOPs refers to languages that uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc in programming.

What is OO used for?

It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages including JavaScript, C++, Java, and Python.

When should you not use OOPs?

These include: design patterns, abstraction, encapsulation, modularity, polymorphism, and inheritance. When not to use OOP: Putting square pegs in round holes: Don't wrap everything in classes when they don't need to be. Sometimes there is no need and the extra overhead just makes your code slower and more complex.

What are OO principles?

Abstraction, encapsulation, polymorphism, and inheritance are the four main theoretical principles of object-oriented programming.


2 Answers

From Damian Conway:

10 criteria for knowing when to use object-oriented design


  1. Design is large, or is likely to become large

  2. When data is aggregated into obvious structures, especially if there’s a lot of data in each aggregate

    For instance, an IP address is not a good candidate: There’s only 4 bytes of information related to an IP address. An immigrant going through customs has a lot of data related to him, such as name, country of origin, luggage carried, destination, etc.

  3. When types of data form a natural hierarchy that lets us use inheritance.

    Inheritance is one of the most powerful feature of OO, and the ability to use it is a flag.

  4. When operations on data varies on data type

    GIFs and JPGs might have their cropping done differently, even though they’re both graphics.

  5. When it’s likely you’ll have to add data types later

    OO gives you the room to expand in the future.

  6. When interactions between data is best shown by operators

    Some relations are best shown by using operators, which can be overloaded.

  7. When implementation of components is likely to change, especially in the same program

  8. When the system design is already object-oriented

  9. When huge numbers of clients use your code

    If your code will be distributed to others who will use it, a standard interface will make maintenence and safety easier.

  10. When you have a piece of data on which many different operations are applied

    Graphics images, for instance, might be blurred, cropped, rotated, and adjusted.

  11. When the kinds of operations have standard names (check, process, etc)

    Objects allow you to have a DB::check, ISBN::check, Shape::check, etc without having conflicts between the types of check.

like image 110
Aristotle Pagaltzis Avatar answered Sep 28 '22 02:09

Aristotle Pagaltzis


There is a good discussion about same subject @ PerlMonks.

Having Moose certainly makes it easier to always use OO from the word go. The only real exception is if compilation start-up is an issue (Moose does currently have a compile time overhead).

like image 39
draegtun Avatar answered Sep 28 '22 02:09

draegtun