Nothing wrong with that, at all. We do whatever is the simplest, that works.
Having said that, what you describe in your code is a means to bound objects
into a rectangular playing field, eg. Robotron or Gremlins, or to keep a bouncing ball inside the screen. On the other hand, if you want to collide with many separate boundaries, or for objects to collide with other objects, you need to look into collision detection.
There are two broad approaches to detecting collisions:
- Cell/tile-based collision detection
- Geometric collision detection
Some games, like PacMan, use cell-based collision detection. The entire map is represented as cells which are either filled or open(boolean value). The characters (ghosts & PacMan) can only move between cells that are open.
However, nearly all collision detection you see in modern games is geometric. The geometric collision detection category breaks down into:
- Axis-aligned rectangular collision detection, one application of which you've described above; however your whole world may be made out of axis-aligned rectangles which either keep things in, or keep things out, or both.
- Circular collision detection, where a collision occurs when the distance between Circle1 & Circle2 is less than their combined radii (extends to spherical collision detection in 3D).
- Polygonal (line-based) collision detection (e.g. Box2D); this also works for non-axis-aligned rectangles / boxes. Extends (with a considerable amount of extra mathematical effort) to polyhedral (line- & plane-based) collision detection in 3D (eg. ODE). It is costly because it takes processing time proportional to the number of lines in each of the two colliding polygons.
- 3D parametric surface collision detection, which is a whole other ball game and isn't really used in games but rather in eg. CAD software and raytracers. There is probably a parallel for this in 2D (using splines and the like) but I've never seen it's like in use.
It is also worth noting that there is a clear hierarchy of cost in the list above. Axis-aligned rectangular is cheaper than circular collsion detection is cheaper than polygonal collision detection is cheaper than more accurate parametric line or surface methods. For this reason, collision detection algorithms often consist of a "broadphase" and a "narrow phase", the broadphase being where a cheaper algorithm is used to check for "early fail". For instance, in 3D it is far easier to first determine what the maximum spherical bounds of two objects are, and see if these overlap, and then only if they do overlap, to check the individual planes, edges and vertices to see if a real overlap occurs.