r/badcode • • Feb 05 '23

java MY EYES (an old SQL formatter project) NSFW

Post image
206 Upvotes

9 comments sorted by

39

u/_ROEG Feb 05 '23 edited Feb 05 '23

can you tell I just learned how to use/call methods? Also shout out to me 6 years ago for trying to add a cool loading cursor for extra immersion.

This is an old SQL formatter "tool" I tried (I say tried, it actually worked pretty well) to build about 6 years ago. I just added more and more to it until I got my desired output.

The rest of it is just as bad...

edit: The rest of the code...if you dare.

21

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Feb 05 '23

Image Transcription: Code


// Format button functionality
JButton btnFormat = new JButton("Format SQL");
btnFormat.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    
        changes = 0;
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        sqlIn = txtPaneIn.getText().toLowerCase();
        sqlIn = sqlIn.replace("\n", " ").replace("\t", " ").replace("\r", " ").replace("    ", " ")
                .replace("   ", " ").replace("  ", " ").replaceAll("\\s+(?=[),])", "");
        sqlSplit();
        sqlCaseChange();
        colNameIndent();
        sqlIndent();
        sqlIn = Arrays.toString(words).replace(", ", " ").replace("[", "").replace("]", "").replace("\t ", "\t")
                .trim();
        // Hacky formatting to remove some blank lines
        txtPaneOut.setText(sqlIn.replaceAll("(?m)^[ \t]*\r?\n", ""));
        frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
    }
});

I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!

28

u/thebubblesort Feb 05 '23

It's good that there wasn't any hacky formatting until the end.

22

u/morimo Feb 05 '23

I like that it starts with the declaration of an unused variable.

11

u/Gautley Feb 05 '23

Thank god you marked as 18+

1

u/darkpyro2 Feb 06 '23

I thought you were talking about the light theme at first...I dont even want to read the post because of the blaringly white background

0

u/propostor Feb 06 '23

It isn't high quality but even as a non Java dev I can see exactly what's going on from start to finish. By that metric alone, I don't class this as truly bad code.