Why Solidity’s constant Is Deprecated and How to Use view & pure
This article explains the purpose of the constant keyword in Solidity, its equivalence to view, the deprecation timeline, and why developers should replace constant with view or pure to avoid state‑modifying functions and unnecessary gas costs.
Why Use constant?
Functions can be declared constant to promise they will not modify the blockchain state, allowing callers (e.g., web3.js) to use eth_call instead of a transaction.
Functions can be declared constant in which case they promise not to modify the state.
When a function is marked constant, it typically does not consume gas because no state change occurs; otherwise, a non‑constant function may generate a transaction and incur fees.
Difference Between constant and view
In Solidity 0.4.16 the documentation described constant for functions as an alias of view:
constant for functions: Same as view.
Later versions state:
constant on functions is an alias to view, but this is deprecated and will be dropped in version 0.5.0. Getter methods are marked view.
Thus, constant behaves like view, but it is being removed because it can be misleading—functions marked constant may still return varying results depending on inputs.
Changes After Replacement
Use the view keyword to mark functions that do not modify state; such functions cannot use SSTORE, send/receive ether, or call non‑view/pure functions.
Use the pure keyword for functions whose output depends only on their parameters; they cannot use SSTORE, SLOAD, ether transfers, msg, or block data, and may only call other pure functions.
The constant keyword is ineffective for functions in newer Solidity versions.
Variables marked constant become immutable and may be optimized into memory or bytecode.
Conclusion
The article outlines the role of constant, its upcoming deprecation, and introduces view and pure as the preferred modifiers for read‑only and deterministic functions in modern Solidity development.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Senior Brother's Insights
A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
