What are the differences pre condition ,post condition and invariant in computer terminology [closed]

主宰稳场 提交于 2019-11-28 16:01:12

问题


I am reading the book Java Concurrency in Practice and getting a little bit confused with these terms:

  1. Pre condition
  2. Post condition
  3. Invariants

Can someone please explain me them (with an example, if it's possible)?


回答1:


You'll have a lot of problems writing Java, especially multi-threaded code, if you can't understand these simple ideas:

  1. Pre-conditions are the things that must be true before a method is called. The method tells clients "this is what I expect from you".
  2. Post-conditions are the things that must be true after the method is complete. The method tells clients "this is what I promise to do for you".
  3. Invariants are the things that are always true and won't change. The method tells clients "if this was true before you called me, I promise it'll still be true when I'm done".

They're all part of an idea called "programming by contract". It was invented by a guy named C.A.R. Hoare. Bertrand Meyer built an object oriented language called Eiffel around it. No one uses it much, but he had a day in the sun because of it.

Eiffel isn't very popular. There are over four million questions on SO as I write this, but only 32 of them are tagged "eiffel".

Update: There were 11,966,392 question on SO on 29-Jun-2016. Only 92 of them were tagged "eiffel". The percentage of Eiffel questions is staying roughly constant at ~0.00077%.

I stand corrected - thank you, flamingpenguin. I've updated my answer.



来源:https://stackoverflow.com/questions/11331964/what-are-the-differences-pre-condition-post-condition-and-invariant-in-computer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!