Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to make class and function?

I am a beginner to programming when I start to code I just start writing and solve the problem. I write whole program in a single main function. I don't know when to make class, and functions.

What are good books which I read to learn these concepts?

like image 783
nicky Avatar asked Feb 20 '10 06:02

nicky


People also ask

Should I make a function or a class?

In general, a function needs well defined inputs and outputs. Classes, in essence, collect functions and data together. Much like you should use a function to collect operations into concise, well defined collections of operations, classes should organize functions and data relevant to be stored together.

When should you create a function?

Groups of statements that appear more than once in a program should generally be made into a function. For example, if we're reading input from the user multiple times in the same way, that's a great candidate for a function.

When would you use a class over a function?

The main reason people use classes (that I've read) is for re-usability, but you can call a function any number of times. The main other reason I've seen is because you can create different instances of classes, but you can call functions with different parameters.

When should I create a class in python?

As a rule of thumb, when you have a set of data with a specific structure and you want to perform specific methods on it, use a class. That is only valid, however, if you use multiple data structures in your code. If your whole code won't ever deal with more than one structure.


3 Answers

A very general question, so just a few rules of thumb:

  • code reuse: when you have the same or very similar piece of code in two places, it should be moved to a function

  • readibility: if a function spans more than a single page on screen, you may want to break it apart into several functions

  • focus: every class or function should do only one specific task. Everything that is not core to this purpose should be delegated to other classes/functions.

like image 149
Thilo Avatar answered Oct 12 '22 13:10

Thilo


I think the canonical answer here is that you should organize your code so that it's readable and maintainable. With that said, it's also important to consider the cost of organizing your code, and how long you expect your code to live.

More directly in response to your question: functions should be used to replace repetitive or otherwise well contained pieces of code. If you apply the same 10 operations over and over again on the same kinds of elements/data you might want to think about collecting all that information into a more concise and clear function. In general, a function needs well defined inputs and outputs.

Classes, in essence, collect functions and data together. Much like you should use a function to collect operations into concise, well defined collections of operations, classes should organize functions and data relevant to be stored together. That is, if you have a bunch of functions that operate on some things like a steering wheel, brakes, accelerators, etc. you should think about having a Vehicle class to organize these relevant functions and data/objects.

Beyond acting as an organizational element, classes should be used to enable easy reuse and creation of multiple "things" - suppose you wanted a collection of those Vehicles. Classes allow you to tie meaning or at least some semantics to your program.

The point of all this, though, is to make your life and the lives of others easier hen it comes to authoring and maintaining your program. So, by all means, when you need a solution to a problem in less than ten minutes and you think it's a one-time use program, ignore all this if you think it'll let you accomplish what you need to faster. Bear in mind, all this organization, semantics and ease of repetitve operation exists to make it easier to accomplish your objectives.

like image 28
Mark Elliot Avatar answered Oct 12 '22 13:10

Mark Elliot


Have a look at

Procedure, subroutine or function?, Object-oriented programming

An object is actually a discrete bundle of functions and procedures, all relating to a particular real-world concept such as a bank account holder or hockey player in a computer game. Other pieces of software can access the object only by calling its functions and procedures that have been allowed to be called by outsiders.

Basically, you use a function/procedure/method to encapsulate a specific section of code that does a specific job, or for reusibility.

Classes are used to encapsulate/represent an object with possibly its own data, and specific function/procedure/method that makes sense to use with this object.

In some languages classes can be made static, with static function/procedure/method which can then be used as helper function/procedure/method

like image 31
Adriaan Stander Avatar answered Oct 12 '22 15:10

Adriaan Stander