什么时候制作类和函数? [英] When to make class and function?

查看:77
本文介绍了什么时候制作类和函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程的初学者,当我开始编写代码时,我才开始编写并解决问题。
我在单个主函数中编写了整个程序。
我不知道什么时候上课和上课。

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?

推荐答案

一个非常普遍的问题,因此只需遵循以下几条经验法则即可:

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:每个类或功能都应该仅一项特定任务。并非为此目的核心的所有内容都应委托给其他类/函数。

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.

这篇关于什么时候制作类和函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆