DEV Community

Discussion on: Wishful Coding

Collapse
 
thorstenhirsch profile image
Thorsten Hirsch • Edited

Great tip! I also do that all the time. When I'm focused and deep in the domain I don't want to jump around in the code or between the files. No, I want to write a function, component, ... in one go. That way it all makes sense later when I or someone else will read it.

In some of my programs this programming style has lead to two layers of code:

  • domain layer, here's where I've coded along the requirements in one go and everybody (with domain knowledge) should be able to read it like a book
  • library / helper layer, here are the technical details, you don't need to look here in order to understand what the program does, but most of the bugs are in here, so this is where to look if you want to know HOW it's done

However it's not always a good idea to introduce these layers into the structure of a given framework, because most frameworks give you a (more complex, more technical) structure already. Let's take MVC for example. In order to keep the controllers lean, your domain code should be in the models. But do you need to split your model layer into a domain layer and a library layer? Maybe. But if you need just a few functions in the library / helper layer a simple helper library (not part of the model layer) might be enough. Or think about concerns - normally they should contain domain code (shared behaviour between multiple models), but it's also a good place to fully implement your functions in order to keep them out of your models, so you can use them as a library layer as well.