Designing Classes
A class is a template for objects that share the same structure and behavior. In Clean Language, you declare properties at the top of the class, write a constructor that sets them, and add methods that operate on them. The compiler enforces everything.
A class with properties, a constructor, and multiple methods:
class Rectangle\n number width\n number height\n\n constructor(number w, number h)\n width = w\n height = h\n\n number area()\n return width * height\n\n number perimeter()\n return 2.0 * (width + height)\n\n string describe()\n return "Rectangle " + width.toString() + "x" + height.toString() + ", area: " + area().toString()\n\nstart:\n Rectangle r = Rectangle(5.0, 3.0)\n print(r.describe())\n print("Perimeter: " + r.perimeter().toString())Rectangle 5.0x3.0, area: 15.0\nPerimeter: 16.0Properties (width, height) are declared at the top of the class. The constructor assigns them. Methods access properties by name — no self. or this. needed. Methods can call other methods directly: describe() calls area() to avoid duplicating logic.
Each instance is independent — they share the template but have their own data:
start:\n Rectangle small = Rectangle(2.0, 3.0)\n Rectangle large = Rectangle(10.0, 8.0)\n\n print("Small: " + small.describe())\n print("Large: " + large.describe())\n\n boolean fits = small.area() < large.area()\n print("Small fits inside large: " + fits.toString())\n\n number total = small.area() + large.area()\n print("Combined area: " + total.toString())Small: Rectangle 2.0x3.0, area: 6.0\nLarge: Rectangle 10.0x8.0, area: 80.0\nSmall fits inside large: true\nCombined area: 86.0small and large are independent instances. Calling .area() on small uses 2.0x3.0; on large it uses 10.0x8.0. You can use instance method results in expressions, comparisons, and other calculations just like any other value.
Quick recap
- Properties are declared at the top of the class body with type and name
- constructor() assigns initial values — called when you create an instance
- Methods access properties by name directly — no self or this keyword
- Each instance is independent — its properties are separate from every other instance