CA004 - Command-Query Separation


A function should never change the state of an object. A function containing a procedure call, an assignment to an attribute, or creating an attribute is a strong indication that this principle is violated. This rule applies exactly in these three cases.

There are rather exceptional but sometimes useful class designs in which the externally visible state of an object (i. e. the values of exported queries) does not change even though the function contains a rule-violating instruction.

Scope Class
Status Enabled
Severity Warning
Applicability All
Score 60

Example of violation

height: INTEGER -- Height in pixel of Current. width: INTEGER -- Width in pixel of Current. do height := height + 10 Result := 100 - height end


Ensures that no query changes the state of the current object.

In the example, one could replace the routine width by an attribute and calling update_width before querying width where update_width would be: update_width -- Update `height' and `width' with .... do height := height + 10 width := 100 - height end

Or you can remove the line height := height + 10 from the body of width.

cached: 07/14/2024 2:13:12.000 PM