【整理】KISS原则介绍

353 篇文章 88 订阅

KISS原则介绍

 

KISS 原则是用户体验的高层境界,简单地理解这句话,就是要把一个产品做得连白痴都会用,因而也被称为“懒人原则”。换句话说来,“简单就是美”。同样在SAP系统的二次开发中适用,其核心思想就是“解耦,解耦,再解耦”,职责单一化,功能单一化为以后的维护和重构都会带来极大的便利。

Background

The KISS principle says that you should always choose the simplest solution for a problem. KISS is an acronym and can have any of the following meanings (the list is not exhaustive):

KISS 原则是说你应该总该为一个问题选择最简单的解决方案。KISS其实是一个缩略词,可以有以下任何含义(列表并不详尽):

  1. Keep it simple, stupid.
  2. Keep it small and simple.
  3. Keep it sweet and simple.
  4. Keep it simple and straightforward.
  5. Keep it short and simple.
  6. Keep it simple and smart.
  7. Keep it strictly simple.

The basic statement of the KISS principle is similar to Occam’s razor, which says that in science the preferred theory is the one that makes fewest assumptions to explain observations (see Wikipedia entry on the KISS principle).

关于KISS 原理的基本陈述类似于奥卡姆剃刀定律,它是说在科学中,首选的理论是用最少的假设来解释观察结果的理论(参见维基百科关于KISS原理的条目)。

Tip:奥卡姆剃刀定律(Occam's Razor, Ockham's Razor)又称“奥康的剃刀”,它是由14世纪英格兰的逻辑学家、圣方济各会修士奥卡姆的威廉(William of Occam,约1285年至1349年)提出。这个原理称为“如无必要,勿增实体”,即“简单有效原理”。正如他在《箴言书注》2卷15题说“切勿浪费较多东西去做,用较少的东西,同样可以做好的事情。”

举个例子,日本最大的化妆品公司收到客户抱怨,买来的肥皂盒里面是空的。于是他们为了预防生产线再次发生这样的事情,工程师想尽办法发明了一台X光监视器去透视每一台出货的肥皂盒。同样的问题也发生在另一家小公司,他们的解决方法是买一台强力工业用电扇去吹每个肥皂盒,被吹走的便是没放肥皂的空盒。同样的事情,采用的是两种截然不同的办法,你认为哪个更好呢?

 

Rule

Follow the KISS principle

Follow the KISS principle, and limit the complexity of your programs as far as possible.

遵循KISS原则

遵循KISS原则,并尽可能地限制程序的复杂性。

 

Details

The best solution to a problem is usually the one that is as simple, minimalist, and easy to understand as possible, while ensuring stability, understandability, and maintainability in addition to functional correctness.

There are plenty of bad examples of the KISS principle. Why is this?

1、Programs are too complex right from the start. This can be due to poor design or simply a rash, undisciplined programming style.

2、Programs are maintained for lengthy periods. Instead of creating new implementations for old and new functions together, new functions are simply added (usually using IF control structures) to old functions. Programs that were initially simple thus become ever more complex, although this is not justified by the complexity of the task at hand.

To develop according to the KISS principle, you should ensure right from the start that the complexity of the program remains manageable. Rules that support this approach relate to the structure and style of programs, in particular comments and complexity.

一个问题的最佳解决方案通常是尽可能简单、极简和易于理解的,同时除了功能正确性之外,还要确保稳定性、可理解性和可维护性。

KISS原则有很多不好的例子。这是为什么?

1、程序从一开始就太复杂了。这可能是由于糟糕的设计,或者仅仅是草率的、无纪律的编程风格。

2、程序要长期维护。新函数不是同时为旧函数和新函数创建新实现,而是简单地添加到旧函数(通常使用IF控制结构)。最初简单的程序因此变得越来越复杂,尽管这并不是由手头任务的复杂性所证明的。

要根据KISS原则进行开发,您应该从一开始就确保程序的复杂性和可控性。支持这种方法的规则要与程序的结构和样式相关,特别是注释和复杂性。

 

Note

If existing programs do not follow the KISS principle, and these programs need to be further developed, we recommend refactoring as appropriate. Refactoring refers to the process of manually or automatically improving the structure of programs while retaining the observable program behavior. It improves legibility, understandability, maintainability, and extensibility, as well as considerably reducing the related effort for troubleshooting and functional enhancements (see Wikipedia entry on refactoring). The (incremental) refactoring of an existing program is not only useful for following the above rule, but also for all following rules.

The refactoring of existing code is supported by the required coverage by unit tests. Comprehensive unit tests can ensure that a program behaves in the same way after the refactoring process.

如果现有的程序不遵循KISS原则,并且这些程序需要进一步开发,我们建议根据需要进行重构。重构是指在保留可观察到的程序行为的同时,手动或自动地改进程序结构的过程。它提高了易读性、可理解性、可维护性和可扩展性,并大大减少了故障排除和功能增强方面的相关工作(请参阅关于重构的维基百科条目)。现有程序的(增量)重构不仅适用于遵循上述规则,而且适用于所有遵循的规则。

单元测试所需的覆盖范围支持对现有代码的重构。全面的单元测试可以确保程序在重构过程之后以相同的方式运行。

 

Example

The figure below shows the structure of a method that does not follow the KISS principle. The method consists of approximately 160 statements and reaches a nesting depth of 12 levels. The method, which is only illustrated schematically, is a real example from a live ABAP program, which reached the state shown on the left after continuous additional developments. The method had become so complex that it was practically impossible to make another necessary change, and the developer was forced to refactor in line with the KISS principle.

下图显示的结构就是一个不遵循KISS原则的方法,该方法由大约160条语句组成,嵌套深度达到12层。该方法只是象征性地说明了一个来自实时ABAP程序的例子,经过不断的额外开发,该程序达到了如左侧所示的状态。这个方法变得如此复杂,以至于几乎不可能再做一次必要的修改,开发人员被迫只能按照KISS原则进行重构。

The result is illustrated on the right of the figure. By splitting the method M into three methods, each with less than 100 statements and a maximum nesting depth of 5 levels, manageable modularization units were generated, which follow the rules for complexity, and allow the required modification to be made. Ideally, however, the state shown on the left side of the figure should never occur.

结果显示在图片的右边,通过将方法M分解为三个方法,每个方法的语句少于100条,最大嵌套深度为5层,生成了可管理的模块化单元,这些单元遵循复杂性规则,并允许进行所需的修改。然而,理想情况下,图片左侧显示的状态不应该出现。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SAP剑客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值