If it compliments an answer, code is fine.
But you don't want to end up posting a code-only answer or a short answer with a wall-of-code (100 lines +) appended as you'ld be risking users to mistake Crypto.SE for Stackoverflow and sooner or later you might end up with nasty things like having to answer code-related questions in the comments, or fulfilling sourcecode requests, etc.
So, it's a bit about the balance between actual answer and supplemental sourcecode.
For anything that tips the scale heavy towards sourcecode, there's a sneaky way to work around the wall-of-code problem. When a Q, A, or comment triggers you to actually write a full-fledged implementation with all the bells and whistles (like you describe in your example), you could push your project to something like Github or your own website (if you have one) and simply link to your lengthy sourcecode project in your answer/comment as a "bonus" or "proof of concept" kinda thing. When choosing that path, the sky is the limit and the only no-go would probably be a link-only answer which only points to your code. (OTOH, knowing you as someone who doesn't spare words when answering Qs, I doubt I'll ever see you post a link-only answer.)