python大牛编码样板_什么是样板,为什么要使用它? 编码风格指南的必要性

python大牛编码样板

by Meet Zaveri

通过见面Zaveri

什么是样板,为什么要使用它? 编码风格指南的必要性 (What is boilerplate and why do we use it? Necessity of coding style guide)

In Information Technology, a boilerplate is a unit of writing that can be reused over and over without change. By extension, the idea is sometimes applied to reusable programming, as in “boilerplate code.”

在信息技术中,样板是写作的单位,可以不断重复使用而无需更改。 通过扩展,该思想有时会应用于可重复使用的编程,例如“样板代码”。

Legal agreements, including software and hardware terms and conditions, make abundant use of boilerplates.

包括软件和硬件条款及条件在内的法律协议充分利用了样板。

For instance, a lawyer may give you a five page contract to sign, but most of the contract is boilerplate — meaning it’s the same for everyone who gets that contract, with only a few lines changed here and there.

例如,律师可能会给您提供一份五页的合同,但合同大部分都是样板文件-意思是每个获得该合同的人都一样,只需要更改几行即可。

In computer programming, boilerplate code or boilerplate refers to sections of code that have to be included in many places with little or no alteration. It is often used when referring to languages that are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.

在计算机程序设计, 样板代码样板是指必须包含在很多地方很少或根本没有改动的代码段。 当引用被认为是冗长的语言时,通常会使用它,即程序员必须编写大量代码才能完成最少的工作。

For example, in web development, simple boilerplate for HTML would look like this:

例如,在Web开发中,HTML的简单样板如下所示:

<!DOCTYPE html>                       <html class="no-js" lang="">                           <head>                                 <meta charset="utf-8">                                 <meta http-equiv="x-ua-compatible" content="ie=edge">         <title></title>                                 <meta name="description" content="">                           <meta name="viewport" content="width=device-width, initial- scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="css/main.css"></head>                           <body>                                 <p>Hello world! This is HTML5 Boilerplate.</p>               <script src="js/vendor/modernizr-{{MODERNIZR_VERSION}}.min.js>     </script>
</body></html>

You can view the whole repository here :

您可以在此处查看整个存储库:

h5bp/html5-boilerplatehtml5-boilerplate - A professional front-end template for building fast, robust, and adaptable web apps or sites.github.com

h5bp / html5-boilerplate html5-boilerplate-专业的前端模板,用于构建快速,健壮和适应性强的Web应用程序或网站。 github.com

In the 1890s, boilerplate was actually cast or stamped in metal ready for the printing press and distributed to newspaper presses and firms around the United States. Until the 1950s, thousands of newspapers received and used this kind of boilerplate from the nation’s largest supplier, the Western Newspaper Union. Some companies also sent out press releases as boilerplate so that they had to be printed as written.

在1890年代,样板实际上是用金属铸造或冲压制成的,准备用于印刷机,并分发给美国各地的报纸印刷机和公司。 直到1950年代,成千上万的报纸从美国最大的供应商西方报纸联盟那里接收并使用了这种样板。 一些公司还发布了新闻稿作为样板,因此必须按书面印刷。

Most professional web developers have created a collection of assets and snippets of code that they reuse on projects to accelerate development. There are some universal or near universal patterns that all websites share in common. Rather than continuously rebuild these, most developers start by copying the code they used for a similar project and then start modifying it.

大多数专业的Web开发人员已经创建了资产和代码段的集合,他们在项目上重复使用这些代码段以加速开发。 所有网站都共享一些通用或接近通用的模式。 大多数开发人员不是不断重建这些内容,而是从复制用于类似项目的代码开始,然后开始对其进行修改。

Some developers recognize the value of these boilerplate starter templates and take the time to make the boilerplate more generic and share them online for others to use.

一些开发人员认识到这些样板入门模板的价值,并花时间使样板更加通用,并在线共享它们以供其他人使用。

This is not only limited to web development. It is used beyond in AI/ML as there are more growing frameworks and libraries.

这不仅限于Web开发。 随着越来越多的框架和库的发展,它在AI / ML中得到了广泛的应用。

大型项目所需样板的特性(可用于生产) (Necessary characteristics of boilerplate for large projects (production ready))
  • Good and Readable Documentation ?

    良好且可读的文档?
  • Code structure with a deeper abstraction level

    具有更深抽象级别的代码结构
  • Follows Proper Coding Standard

    遵循正确的编码标准
  • Has CLI tool (for rapid prototyping and setup)

    具有CLI工具(用于快速原型制作和设置)
  • Scalable ?

    可扩展?
  • Easy testing tools

    简单的测试工具
  • Necessary API modules

    必要的API模块
  • Support for Internationalization and Localization ?

    是否支持国际化和本地化?
  • Code Splitting

    代码分割
  • Server and Client code for setup

    服务器和客户端代码进行设置
  • Proper Navigation and Routing Structure ?

    正确的导航和路由结构?

After all these minimum specs, you should start editing and altering the code in order to build your project.

在完成所有这些最低要求之后,您应该开始编辑和更改代码以构建您的项目。

There are some Big Tech Companies who even build their own boilerplate. They use it for respective and similar projects throughout time.

有些大型科技公司甚至建立了自己的样板。 他们在整个过程中都将其用于各自和类似的项目。

A perfect example for this would be react.js’s boilerplate:

一个完美的例子是react.js的样板:

react-boilerplate/react-boilerplatereact-boilerplate - :fire: A highly scalable, offline-first foundation with the best developer experience and a focus…github.com

react-boilerplate / react-boilerplate- :fire:具有最佳开发人员经验和重点的高度可扩展,离线优先的基础… github.com

小型项目的样板(脚手架) (Boilerplate for smaller projects(Scaffolding))

These type of boilerplates are generally kind of “Starter Kits” or in professional way it is called “Scaffolding”. Their main target users are novice developers or new early adopters.

这些类型的样板通常是“入门工具包”或以专业方式称为“脚手架折叠”。 他们的主要目标用户是新手开发人员或新的早期采用者。

It focuses on fast prototyping by creating the elements which are necessary only for new projects. These require less functionality and are not scalable over time and project.

它通过创建仅对新项目而言必需的元素来专注于快速原型制作。 这些要求较少的功能,并且不能随时间和项目进行扩展。

Their code structure is not much expanded, and doesn’t involve deeper abstraction layer as users only need to build core features. This eliminates the need for extra utilities.

他们的代码结构没有太大扩展,并且不涉及更深的抽象层,因为用户只需要构建核心功能。 这样就无需额外的实用程序。

The simplest example is Facebook’s create-react-app boilerplate:

最简单的示例是Facebook的create-react-app样板:

facebookincubator/create-react-appcreate-react-app - Create React apps with no build configuration.github.com

facebookincubator / create-react-app create-react-app-创建没有构建配置的React应用。 github.com

样板和模板有什么区别? (What’s the difference between a boilerplate and a template?)

As Joachim Pense clearly states, boilerplate is something that you copy and paste and just add to a document. It comes up most often in contracts where language is used and reused, spelling out things like conditions and caveats.

正如Joachim Pense明确指出的那样, 样板文件是您复制并粘贴并仅添加到文档中的东西。 它最常出现在使用和重复使用语言的合同中,阐明了诸如条件和警告之类的内容。

Writers use templates as models, sometimes with negative effects. In broad terms, a template is a model or pattern used to create new objects. In writing, it is a standardized form of something like a resume that writers can use to flesh out their own versions.

作家使用模板作为模型 ,有时会产生负面影响。 广义而言,模板是用于创建新对象的模型或模式。 在写作中,它是诸如简历之类标准化形式 ,作者可以用来充实自己的版本。

Unlike boilerplates, templates are adapted for a particular use. The problem arose for me when students used Word templates for their resumes, and they all ended up looking the same.

与样板不同,模板适用于特定用途。 当学生使用Word模板作为简历时,对我来说出现了问题,他们最终看起来都一样。

Both templates and boilerplates can make business writing stilted and artificial if used unwisely.

如果使用不当,模板和样板都可能使业务写作变得冗长而虚假。

编写代码的样式指南 (Style Guide for writing code)

Regardless of whether you’re using boilerplate or not, there are some standards followed by companies for writing code . One of them is Style Guide. It attempts to explain the basic styles and patterns that are used in various companies or organizations. It’s generally a rule that employees must adopt the coding style guide of their company.

无论您是否使用样板,公司都会遵循一些标准来编写代码。 其中之一是样式指南。 它试图解释各种公司或组织中使用的基本样式和模式。 通常,员工必须采用公司的编码风格指南。

The Style Guide describes tons of rules for writing code, like indentation of tabs and spaces, naming of variables and functions, writing necessary comments, formatting, source file structures, using proper method of data structures, avoiding hoisting, scoping, control statements and a lot more.

《样式指南》描述了用于编写代码的大量规则,例如,制表符和空格的缩进,变量和函数的命名,编写必要的注释,格式,源文件结构,使用适当的数据结构方法,避免吊起,作用域,控制语句和更多。

Programming styles commonly deal with the visual appearance of source code with the goal of readability. Software has long been available that formats source code automatically, leaving coders to concentrate on naming, logic, and higher techniques.

编程风格通常以可读性为目标来处理源代码的外观。 长期以来一直有可以自动格式化源代码的软件,使编码人员可以专注于命名,逻辑和高级技术。

As a practical point, using a computer to format source code saves time, and it is possible to then enforce company-wide standards without debates. (Source — Wiki).

从实际出发,使用计算机来格式化源代码可以节省时间,然后可以执行公司范围内的标准而无需争论 。 (来源-Wiki)。

These are some common debates like: Tabs v Spaces Holy war, choosing the perfect Code IDE, and so on. The interesting thing is that you can get involved in these debates which mostly happen on Reddit. You can also participate in some of the stackoverflow Q&A’s.

这些是一些常见的辩论,例如: Tabs v Spaces神圣的战争选择完美的Code IDE等等。 有趣的是,您可以参与主要在Reddit上发生的这些辩论 您还可以参加一些stackoverflow问答。

For web developers, the most common style guide for JS is Airbnb’s javascript style guide. It’s open source and everyone can contribute.

对于Web开发人员而言,最常见的JS样式指南是Airbnb的javascript样式指南。 它是开源的,每个人都可以做出贡献。

airbnb/javascriptjavascript - JavaScript Style Guidegithub.com

airbnb / javascript javascript-JavaScript样式指南 github.com

If anyone is in doubt as to why Javascript needs a style guide, then read this issue’s second answer by Harrison Shoff, who is programmer at Airbnb .

如果有人对Java为什么需要样式指南有疑问,请阅读Airbnb程序员Harrison Shoff的第二个答案。

Why does JavaScript need a style guide? · Issue #102 · airbnb/javascriptOne of my favorite parts about the JavaScript community is that people choose to write it in so many different ways…github.com

为什么JavaScript需要样式指南? ·第102期·airbnb / javascript ,我最喜欢JavaScript社区之一是人们选择以多种不同的方式编写它…… github.com

Here are some style guides for some of today’s more popular languages:

以下是一些当今流行语言的样式指南:

DotNet Code Formatter

DotNet代码格式化程序

Java: Google-Java-Format

Java:Google Java格式

Javascript Standard Style (different from airbnb’s javascript)

JavaScript标准样式 (与airbnb的javascript不同)

PHP Coding Standards Fixer

PHP编码标准修复程序

Python: Google’s YAPF

Python:Google的YAPF

Ruby: Rubocop

Ruby:Rubocop

Boilerplate的更多内容:面向对象的概念 (More from Boilerplate: Concept for OOP)

In object-oriented programs, classes are often provided with methods for getting and setting instance variables. The definitions of these methods can frequently be regarded as boilerplate.

面向对象的程序中 ,通常为类提供获取和设置实例变量的方法。 这些方法的定义通常可以视为样板。

Although the code will vary from one class to another, it is sufficiently stereotypical in structure that it would be better generated automatically than written by hand.

尽管代码在一个类之间会有所不同,但其结构具有足够的陈规定型观念,因此与手动编写相比,自动生成更好。

For example, in the following Java class representing a pet, almost all the code is boilerplate except for the declarations of Pet, name, and owner:

例如,在以下表示pet的Java类中,除了Petnameowner声明之外,几乎所有代码都是样板:

public class Pet {    private String name;    private Person owner;
public Pet(String name, Person owner) {        this.name = name;        this.owner = owner;    }
public String getName() {        return name;    }
public void setName(String name) {        this.name = name;    }
public Person getOwner() {        return owner;    }
public void setOwner(Person owner) {        this.owner = owner;    }}

Boilerplate definition is becoming more global in many other programming languages nowadays. It comes from OOP and hybrid languages that were once procedural but have become OOP. They now have the same goal of repeating the code you build with a model/template/class/object, so they adopt this term. You make a template, and the only things you do for each instance of a template are the individual parameters.

如今,样板定义在许多其他编程语言中正变得越来越全球化。 它来自OOP和混合语言,这些语言曾经是程序性的,但现在已经成为OOP。 现在,他们的目标是重复使用模型/模板/类/对象构建的代码,因此他们采用了这个术语。 您创建了一个模板,并且您对模板的每个实例所做的唯一操作就是各个参数。

This part is what we call boilerplate. You simply re-use the code you made a template of, but with different parameters.

这就是我们所说的样板。 您只需重复使用作为模板的代码,但要使用不同的参数。

样板作为API (Boilerplate as an API)

Since you’re simply re-using the template code with different parameters, this implies that we could build reusable API’s that only need a change of inputs and outputs.

由于您只是简单地使用具有不同参数的模板代码,这意味着我们可以构建只需要更改输入和输出的可重用API。

结论 (Conclusion)

“Boilerplate code” is any seemingly repetitive code that shows up again and again in order to get a result that seems like it ought to be much simpler.

“样板代码”是任何看似重复的代码,它们会反复出现以得到看起来应该更简单的结果。

I wrote this article because I was recently instructed by a Team Lead to learn about the many varieties of boilerplate that might be suitable for our project. So I had to go on a search for the perfect boilerplate.

我写这篇文章的原因是,最近我受团队负责人的指导,学习了许多适用于我们项目的样板。 因此,我不得不继续寻找完美的样板。

Any type of feedback will be appreciated! Hustle On!

任何类型的反馈意见将不胜感激! 忙吧!

翻译自: https://www.freecodecamp.org/news/whats-boilerplate-and-why-do-we-use-it-let-s-check-out-the-coding-style-guide-ac2b6c814ee7/

python大牛编码样板

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值