Both are possible, but it is not a case of being formal or informal.
If you say 'a piece of', you mean one section of something larger. For example, if you have one large birthday cake which you cut into slices, you would offer each of your guests 'a piece of cake' or 'some cake' (uncountable). But if you have a plate full of small individual cupcakes, each person would take 'a cake', or several 'cakes' (plural). It's as simple as that.
The same goes for chocolate, for example. If you are handing round a box of little individual chocolates, these are countable, and you'd offer people 'a chocolate' or a couple of 'chocolates' (plural). But if you have a big bar of chocolate which you need to break into squares to share around, you'd say 'a piece of chocolate' or 'some chocolate' (uncountable).
As for 'candy', both forms, 'a candy' and 'a piece of candy' are possible. This is US English, so I'm not 100% sure, but I believe that the uncountable form is more common nowadays and the countable form is a little outdated. What I do know, however, is that it's the grammar that's important - you can use either form, providing the grammar of the sentence makes sense. For example:
How much candy is there in the box? Right
How many candies are there in the box? Right
What you can't say is 'How many candy' or 'How much candies' or 'Some piece of candy' , for example. The singular/plural elements of the sentence have to match up - that's what matters most.