Sugar itself will not save your coding. It's best not to have lengthy heavy lunches either, since then it takes much more effort to tune the brain to coding wave.
Something small, light, consumed by keyboard within 10 minutes guarantees continuous productivity. Go get a heavy lunch, spend 60-90 minutes out and you'll face lots of Whiskey-Tango-Foxtrots when you get back and try to get back to a coding mood...
Personally, I don't consume chocolate bars (well, maybe 1 or two in month) since they have a crapload of calories and sugar.
Consuming them on a daily basis is definitely one of best ways to become a life supporter of pharmaceutical companies (i.e. insuline, pump/pen, test strips, routine endocrinology visits, expensive routine blood tests, ...).
If that's aligned with your long-term goals, then sure - why not ?
BTW, what you're describing sounds awfully lot like a craving for a chocolate bar. That's a proper addiction right there, just like craving for a cigarette, having to drink a diet coca-cola daily (or anything else infused with lots of chemicals that enslave your consuming habits).
You can test it by exchanging your chocolate bar for something else with same amount of sugar, but that does not taste so good.
Trust me, you won't get the endorphine kick, even though the sugar intake will be identical.
On a more serious note, if you truly get an energy kick from a chocolate bar, and you are sure it's not just an endorphine boost, you gotta visit your endocrinologist and get checked for Diabetes. These days, you can survive even a decade with a Diabetes !