Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Class invariant
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Programming language support== ===Assertions=== Common programming languages like Python,<ref>[https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assert-stmt Official Python Docs, ''assert statement'']</ref> PHP,<ref>{{Cite web|title=PHP assert function|url=https://www.php.net/manual/en/function.assert.php|url-status=live|archive-url=https://web.archive.org/web/20010321003333/http://www.php.net:80/manual/en/function.assert.php |archive-date=2001-03-21 }}</ref> JavaScript,{{citation needed|date=February 2021}} C++ and Java support [[Assertion (computing)|assertions]] by default, which can be used to define class invariants. A common pattern to implement invariants in classes is for the constructor of the class to throw an exception if the invariant is not satisfied. Since methods preserve the invariants, they can assume the validity of the invariant and need not explicitly check for it. ===Native support=== The class invariant is an essential component of [[design by contract]]. So, programming languages that provide full [[Design by contract#Languages with native support|native support for design by contract]], such as [[Eiffel (programming language)|Eiffel]], [[Ada (programming language)|Ada]], and [[D (programming language)|D]], will also provide full support for class invariants. ===Non-native support=== For [[C++]], the [[Loki (C++)|Loki Library]] provides a framework for checking class invariants, static data invariants, and exception safety. For Java, there is a more powerful tool called [[Java Modeling Language]] that provides a more robust way of defining class invariants.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)