Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

language agnostic - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...