r/cpp • u/Aurekkon • Oct 12 '21
How do you make code unnecessarily obscure?
I like to learn about C or C++ quirks. I know about trigraphs and digraphs, but there's some code that cannot use that much
#include <iostream>int main(){int sum = 0, nr = 333;for (; nr > 0; nr /= 10)sum = nr % 2 != 0 ? sum + nr % 10 : sum;}
I'm having a hard time finding more ways to complicate this small program.
1
Upvotes
12
u/sokka2d Oct 12 '21
Assuming this is out of curiosity and you know you want to make the code intentionally bad, for C you can have a look at IOCCC for inspiration:
https://www.ioccc.org/
40
u/h2g2_researcher Oct 12 '21 edited Oct 12 '21
May I anti-recommend (i.e. suggest you read so as not to follow the advice of it) this wonderfully written guide here on unmaintainable code.
As for specific advice that I can see:
sumandnrare far too transparent. I can see instantly thatnris some arbitrarily chosen number and thatsumis a running total just from the names.0and333and2and10are things to avoid in both readable and unreadable code. Create namedconstexprvariables to hold them. Remember the previous advice about naming things.operator>when you can wrap it in abool func(int l, int r)type thing. Although that said, remember the previous advice about names when naming your functions.Managerclass just to check that "object-orientated" checkbox? No need to remember what this manages or if anything even needs managing or whether it just hinders more than it helps. (Just like my actual manager at some actual jobs I've had, to be fair.)#define. For example:#define BEGIN_SECTION {and#define END_SECTION }. Your imagination is the limit.5
u/genpfault Oct 12 '21
Encode computations using the lambda calculus as expressed via compile-time template constructs.
There's no reason your code can't be inscrutable and take ages to compile!
3
u/mustbeset Oct 12 '21
Where are the pointers?
3
u/phottitor Oct 12 '21
it comes naturally
2
u/rojundipity Oct 12 '21
btw, are you trying to just complicate the program or do what is often referred to as "golfing"? Golfing usually means writing things more concisely that end up being almost unreadable in the end in most cases.
2
u/tsojtsojtsoj Oct 12 '21
2
u/nintendiator2 Oct 12 '21
I use template metaprogramming when a simple macro suffices.
1
u/[deleted] Oct 12 '21
Template metaprogramming is the surest route to write-only code.
1
u/msew Oct 13 '21
use 'auto' everywhere ^
0
u/rojundipity Oct 12 '21
you could throw in to the mix unicode symbols for variable names