When to make class and function?

nicky picture nicky · Feb 20, 2010 · Viewed 8.1k times · Source

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?

Answer

Thilo picture Thilo · Feb 20, 2010

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.