It's difficult to control the time and energy cost in real coding project. As you find a goad and work toward it, there would be a lot of traps in the way. The trap can be bug, new study or research work, wrong direction and weird problem (can't be sovled by google). When you want to get out of the trap, you just find youself getting deeper and deeper.

For example, yesterday I did a coding project, we use bokeh to visiualize data in webpage, but it is really bad and not developer-friendly because no much example or demos and document is not clear. Basically I find an example project and do some change on it to make my project. But its really bad document gives me a million of bugs, I change different way to make it work(actually I just guess), this procedure cost me huge time. I spend almost 6 hours and it just basically works. The worse thing is that it makes me stressed, and I have to fix the bug with diffrent ways(i.e. edit code and run the server to see if it works repeatly), which is so horrible and makes me non-motivated to work in another day.

Ok, now I find two principles. Firstly, a mature framework is really important, that is well organized document, developer-friendly coding way, full of examples and demos. Otherwise, it will drive you crazy and helpless. Secondly, set a time limit and a reasonable goad before coding. When time is up or goal is achived, stop and turn off your computer.