In a small room, the floor you walk on is worth more than the shelf you fill. A bookcase that is 40 cm deep takes a 40 cm strip off the room along its whole length. One that is 20 cm deep takes half of that. Along a 2 m wall the difference is 0.4 m² of floor, which is about the size of a small stool and the space around it. It also decides whether you can pass the sofa without turning sideways.
The trick is that most of what we put on shelves is much shallower than the shelf it sits on. Shelf depth is only useful to the extent that something actually needs it.
The plan below looks down on a room from above. A bookcase 200 cm long stands against the left wall, a sofa about 90 cm deep stands against the right wall, and the gap between them is where you walk. Slide the depth, change the width of your room, and pick what you want to store.
Shelf depth only has to match the dimension of the thing that points into the shelf. For a book standing on its spine, that is the width of the book. Common paperback sizes run from about 11 cm wide for a pocket paperback to around 14–15 cm for a novel, and hardbacks and textbooks are typically up to roughly 22 cm. An A4 magazine or folder is 21 cm wide.
So a 15 cm shelf holds most novels with room to spare, and a 22–25 cm shelf holds almost every book and magazine you own. A common ready-made bookcase is about 28 cm deep, which is already more than almost all of those need. A 40 cm deep unit is for boxes and baskets, not for books.
The exception is the one thing that makes depth earn its keep: stacked or doubled-up storage. If you put books two rows deep, you will be moving the front row every time you want the back one, so most people end up with a messy front and a forgotten back. One row, well chosen, is calmer.
A common rule of thumb is to leave roughly 60 cm for a side passage and closer to 90 cm along a route you use every day. Treat these as starting points, not laws: a hallway you walk once an hour can be tighter than the route from bed to door. If you want to test your own room, put the plan above at your wall-to-wall width and see what is left.
Measure the wall length, the ceiling height, and the distance from the wall to whatever stands opposite. Include skirting boards, which push furniture a little further out, and mark where doors, radiators and sockets are. Then tape the depth on the floor with masking tape and walk the route. Any tall bookcase should be fixed to the wall with the anchors supplied, so it can't tip if a child climbs it or you lean on it.