DEV Community

Discussion on: How do you write your code?

Collapse
 
meanin profile image
Paweł Ruciński • Edited

From my experience it is good to have a clear requirements before you start to write anything. So I usually think about what is needed, write it down somewhere, on paper or in notepad. I work with that until I have a nice point of view of what I need to create. Sometimes it is not necessary, because ticket or my own assumptions are clear enough to start coding. But I do not start with writing production code.

After I get all requirements, I start to write unit test for general classes. On this level I can clarify my dependencies, mock it and test general purposes objects. While writing tests, I am working on high-level production code with injected mocked dependencies. Next, I am getting deeper to more detailed classes. I implement them with the same way.

And this is how I work with all the code. I move from general to specific. Always test first, then work on production code. This approach is called test-driven development. It pays of in a long term projects. For some proof of concept application, or the small ones it is not necessary to follow this way. But I use it as a habit.

Refering to MVC, it is also possible to use TDD. Starting from testing controllers, then going deeper to services, models. Views are a little bit different topic and I do not have much experience with testing it. From my point of view it is enough to run an app and go through all the paths.

I hope you will find it helpful.