How to badly indent Java code?

Has anyone seen a tool for messing up Java code indentation? I want to put together some exercises for students where they see badly formatted code, try to figure it out, format it cleanly, then look again.

1 Like


:upside_down_face:

17 Likes

take a perfectly indented code, then removed all indent. really wanted to test them, remove all carriage return too within large blocks of code.

1 Like

…or put it all on one line. I’m over thinking this…

6 Likes

Hmmmm.

To solve the question as asked: A python script which, on every line, replaces leading whitespace with a random set of spaces and tabs?

To solve the question I heard… what if, instead of teaching how to manually unwind poorly formatted code, teach students to use the vscode java formatter (should be installed with the wpilib one?). It could be used as a development exercise - write the code however, apply the formatter, see if the output has indentation that matches students expectations (presumably, if they know how to manually unwind, they’ll also have to have some expectation of what the unwound code should look like).

10 Likes

In my limited experience, VS code works pretty well at that.

9 Likes

mmmm minification

but really, alt+shift+f

I mean, just do the real bad thing.

Pseudo Code

for line in file:
  append "\t" random(0,10) times
  chainedCalls = line.split(".")
  for call in chainedCalls:
     if random(0,1) == 1:
        append "\n" 
        append "\t" random(0,10) times

If that doesn’t make it ugly…

2 Likes

Alas, with Java you can’t use preprocessor tricks to really obfuscate things, which is used by many International Obfuscated C Code Contest entries.

For reference, 2020’s IOCCC awardee for “Most misleading indentation” used an “innovative, right justified, reverse indentation style.”

2 Likes

The way that VSCode indents switch-case blocks gives me cancer sometimes.

Nobody has posted their 2023 robot code so the teams must be getting better at formatting. Now if the students could only eliminate the pasta - spaghetti and lasagna - code.

We have github actions to automatically use clang-format format the code to use 80 character-wide lines. Sometimes this leads to weird looking code.

I’ve haven’t increased the line size yet probably because of laziness, but I tell people it’s to prepare for the upcoming apocalypse where FRC teams will have to use analog CRT monitors. Or, if time travelers come, how will they get over our vastly increased line widths?

If you can’t read our code, it must mean you’re illiterate. :person_shrugging:

Honestly sounds like a chat gpt task.

1 Like

Why use chatgpt when you can use freshmen?

Chat GPT can be deterministic?

1 Like

What, you don’t program your freshmen to deterministically add bad indentations to code?

Hopefully any freshmen I teach walk away with good habits lol.

Don’t laugh. I’ve had colleagues that stretched lines of code out many screen widths. Ugg.

Nearly as bad was another colleague who would put multiple blank lines in code just for the heck of it, I guess.

Put an additional coin flip to use “\n” vs. “\r”
also, go ahead and coin flip for “\t” vs. 4x spaces (or any other non-visible single-ish space white character).
Let’s teach these kids to turn on show white space and use a mono-space font in text editors.

1 Like

I vote filling the file with \u0007 (IYKYK)