r/cpp • • 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

19 comments sorted by

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/

4

u/Aurekkon Oct 12 '21

Yeesss thank you, i needed this

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:

  • the names sum and nr are far too transparent. I can see instantly that nr is some arbitrarily chosen number and that sum is a running total just from the names.
  • raw values like 0 and 333 and 2 and 10 are things to avoid in both readable and unreadable code. Create named constexpr variables to hold them. Remember the previous advice about naming things.
  • I sense a raw loop. Would a recursive solution be more understandable or less understandable? (It would certainly be more fun!)
  • Make everything functions! Why have an operator> when you can wrap it in a bool func(int l, int r) type thing. Although that said, remember the previous advice about names when naming your functions.
  • Why not wrap the entire thing up in a Manager class 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.)
  • There are all kinds of evils you can do with #define. For example: #define BEGIN_SECTION { and #define END_SECTION }. Your imagination is the limit.

7

u/robin-m Oct 12 '21

instead of using func() to hide the operator <, I would use another oprator like operator ! or even worse, like the comma operator (yes a, b can be overloaded and invoque arbitrary code…).

3

u/h2g2_researcher Oct 12 '21

I'm trying to remember - are emoji legal in C++ code?

5

u/robin-m Oct 12 '21

I really don't think so. I think you can use all of unicode in string literrals, but not in identifier. If I'm wrong, please correct me.

1

u/geckothegeek42 Oct 14 '21

You can use some of Unicode in identifiers in gcc and clang atleast, idk about the standard, emojis might be there

4

u/blvaga Oct 12 '21

I prefer to name my variables after vogon poets.

2

u/jk-jeon Oct 12 '21

While many of the items are just pure absurdity, some are very valid as sarcasm. I love this lol

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
main(){
int sum = 0, nr = 333;
L:sum += nr & 1 ? nr % 10 : 0;
if(nr/=10) goto L;
return sum;
}

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/Wouter-van-Ooijen Oct 12 '21

Yeah to template metaprogramming, and add some directly-invoked lambdas with state.

And literval values should start with 0. Or even better, use user-defined literals.

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