什么是一些通用的“干净编码”约定? [英] What are some universal "clean-coding" conventions?

查看:109
本文介绍了什么是一些通用的“干净编码”约定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了特定于语言的结构,对于使用PHP,javascript,java和python编程的人来说,前10个干净的编码必须做什么?我想保持我的代码尽可能干净,尽可能可读。例如,将函数体打开括号放置在与函数声明相同的行上对新行有什么好处?此外,事物之间的间距是什么,例如(x == 10)vs(x == 10)?

解决方案

以下是几个可能有帮助的信息:


  1. 为变量和方法提供直观的名称

  2. 使用特定语言例如在Java和C ++中
    在括号上使用不同的样式;根据特定的
    语言约定选择

  3. 始终对发生的事情进行评论。 / li>
  4. 但不要使用注释来污染代码。

  5. 如果可以,尝试集成设计模式。它有助于可重用性
    和维护

  6. 不要将一切都放在一个方法中。每个方法应该一个东西
    ,并真的很好(这也使该方法更容易阅读/理解/测试/调试)

  7. 不要在代码中加入硬编码字符串

  8. 分隔应用程序数据和应用程序代码。也就是说尝试不要硬
    代码配置你的代码。

  9. 不要试图比编译器更聪明。编写代码,其他
    的人可以理解它,让编译器做任何
    优化

  10. 编写代码以一种方式显示你的意图。编写代码保持
    在你的头后面,在7个月你可能需要调试它,
    或别人可能需要修复/增强它。这是不可能的
    记住你为什么你做了一些事后7个月(和评论是
    好​​,但有时不够好)。


  11. 我相信还有更多,但我相信这些可以可用于任何语言


    Aside from language-specific constructs, for someone who is programming in PHP, javascript, java, and python, what are the top 10 clean coding must-do's? I would like to keep my code as clean as possible and as readable as possible. For example, what's the take on placing the function body opening bracket on the same line as the function declaration vs a new line? Also, what's the take on spacing between things for example (x==10) vs ( x == 10 )? Any helpful hints for clean coding will be appreciated!

    解决方案

    Here's a few that may help:

    1. Give intuitive names to variables and methods
    2. Use your specific language coding style (e.g. in Java vs C++ you use different style on brackets; choose according to specific language conventions)
    3. Always put comment on what/why something is happening.
    4. But don't pollute code with comments.
    5. Try to integrate design patterns if you can. It helps in reusability and maintainance
    6. Don't put everything in one method. Each method should do one thing and do it really well (this also makes the method easier to read/understand/test/debug)
    7. Don't put hard-coded strings in your code
    8. Separate application data and application code. I.e. try to not hard code configuration of your code.
    9. Don't try to be more clever than the compiler. Write code that other people can understand it as well and let the compiler do any optimizations
    10. Write code in a way that shows your intentions. Write code keeping in the back of your head that in 7 months you may need to debug it, or someone else might need to fix/enhance it. It is impossible to remember why you did something after 7 months (and comments are good but sometimes not good enough). Let alone the poor guy trying to fix your code

    I am sure there are a lot more but I believe these can be useful in any language

    这篇关于什么是一些通用的“干净编码”约定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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