See all glossary terms

Declarative programming

Declarative programming is a way of programming where you describe the result the code should produce without giving step-by-step instructions on how the computer should produce this result.
In Godot, creating scenes in the editor is a form of declarative programming, even though it is visual and done in the editor. A scene represents a node structure; you don't have to tell the engine exactly how to create each node or in which order to create them. Instead, the scene represents the final structure you want.
The following scene has four nodes: a CharacterBody2D node at the root, and child CollisionShape2D, Sprite2D, and Timer nodes. You don't have to write code to create these nodes, add them as children of one another, or set their properties. You just organize them and set their properties in the editor.
A scene with three nodes
Imagine that you've changed a few properties in the editor, like the radius of the CollisionShape2D node, the texture of the Sprite2D node, and the wait time of the Timer node.
When you run the scene, the scene structure reproduced by Godot may be equivalent to running this code:
var character_body := CharacterBody2D.new()

var collision_shape := CollisionShape2D.new()
collision_shape.shape := CircleShape2D.new()
collision_shape.shape.radius := 32.0

var sprite := Sprite2D.new()
sprite.texture := preload("res://character.png")

var timer := Timer.new()
timer.wait_time := 2.0
timer.one_shot := true

get_tree().root.add_child(character_body)
character_body.add_child(sprite)
character_body.add_child(timer)
This code is imperative because it gives step-by-step instructions to create the nodes and add them as children of one another. When you use declarative code, there's always more code behind the scenes written in a different paradigm that makes it work.
In Godot, you can combine imperative and declarative programming styles. The GDScript programming language is mostly imperative and object-oriented, while the editor gives you tools that are like declarative programming.
If you wonder what declarative programming can look like, some languages are designed to be more declarative than others. For example, SQL and Prolog are two languages that are highly declarative. In SQL, you write queries that describe the data you want to retrieve, and the database engine figures out how to get it. Here's how the same query would look in SQL:
SELECT id FROM inventory WHERE item_name = 'sword';