Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use classes instead of functions? [closed]

I do know some advantages to classes such as variable and function scopes, but other than that is just seems easier to me to have groups of functions rather than to have many instances and abstractions of classes. So why is the "norm" to group similar functions in a class?

like image 258
sdfadfaasd Avatar asked Jun 25 '11 21:06

sdfadfaasd


People also ask

Why do we use class instead of function?

Classes group methods (functions) AND data together, based on the concept of encapsulation. For lager projects it often becomes easier to group things this way. Many people find it easier to conceptualizes the problem with objects. The point of OOP is not to group functions.

Are classes better than functions?

While classes are useful and powerful, they don't have to be used all the time. Functions, IMO, are easier to work with and understand than objects. (This is talking about functions as part of procedural programming and not functional programming.) One of the key benefits of classes is that they allow inheritance.

Why use a class and not a function in Python?

By using classes, you're ensuring that methods are only used on one set of data. This adds to the security of the code because you're less likely to use functions where they don't belong.

Why do we want to use classes for programming?

Classes are required in OOPs because: It provides template for creating objects, which can bind code into data. It has definitions of methods and data. It supports inheritance property of Object Oriented Programming and hence can maintain class hierarchy.


1 Answers

Simple, non-OOP programs may be one long list of commands. More complex programs will group lists of commands into functions or subroutines each of which might perform a particular task. With designs of this sort, it is common for the program's data to be accessible from any part of the program. As programs grow in size, allowing any function to modify any piece of data means that bugs can have wide-reaching effects.

In contrast, the object-oriented approach encourages the programmer to place data where it is not directly accessible by the rest of the program. Instead the data is accessed by calling specially written functions, commonly called methods, which are either bundled in with the data or inherited from "class objects" and act as the intermediaries for retrieving or modifying those data. The programming construct that combines data with a set of methods for accessing and managing those data is called an object.

Advantages of OOP programming:

  • MAINTAINABILITY Object-oriented programming methods make code more maintainable. Identifying the source of errors is easier because objects are self-contained.
  • REUSABILITY Because objects contain both data and methods that act on data, objects can be thought of as self-contained black boxes. This feature makes it easy to reuse code in new systems.Messages provide a predefined interface to an object's data and functionality. With this interface, an object can be used in any context.
  • SCALABILITY Object-oriented programs are also scalable. As an object's interface provides a road map for reusing the object in new software, and provides all the information needed to replace the object without affecting other code. This way aging code can be replaced with faster algorithms and newer technology.
like image 63
evilone Avatar answered Oct 02 '22 17:10

evilone