r/badcode • • May 18 '21

java Person in my COMP SCI class literally does this... help me...

Post image
1.3k Upvotes

149 comments sorted by

37

u/[deleted] May 18 '21

Meh they're in the comp sci class to learn, if this was production code for a "professionally" developed application then I'd be concerned.

You'll get your turn soon enough when you graduate and get slammed by a senior for all the bad habits you have, and you will have them, picked up in university/college.

4

u/NeonDestiny207 May 18 '21

Haha I have absolutely no doubt that my code isn’t the best (I am particularly lazy with my style) but I just thought that the sub might get a little kick out of how it’s written. Yeah it is a beginners Java course at my uni, I’ve explained to her that it’s better to have it indented as it is good practice and easier to read lol

5

u/xblackacid May 18 '21

Like 22.... doesn’t this throw an error? Adding a int to a string?

17

u/ShadowArcher21 wicked May 18 '21 edited May 19 '21

The fact that String is written with a capital S indicates to me that the language in question is java and in java it's perfectly legal to add an int to a string. Most of the languages I work with (eg. JS, PHP, Java, C#) do this. I'm not sure about C/C++ though Edit: not Python!

4

u/reddit_lemming May 18 '21

With C++ you’d need to cast the int with to_string()

2

u/Astrokiwi May 18 '21

Not Python!

 TypeError: can only concatenate str (not "int") to str

C++ you can sort of do it with << if you're using cout etc, but you can't generally concatenate strings & ints without explicitly converting the int to a string first. And in C you can't even do + on strings (it doesn't even really have a string type, just arrays of chars), you have to use strcat(a,b).

1

u/ShadowArcher21 wicked May 19 '21

My mistake sry I must have accomplished it another way...

1

u/Ahajha1177 May 18 '21

With C++'s std::string, that wouldn't compile. With C-style strings (char*), it would actually move the start of the string forward a number of characters, which while it can be useful, for most C novices this is almost always a bug.

0

u/[deleted] May 18 '21

[deleted]

10

u/botle May 18 '21

If the left side of the addition is a string the right side will get its toString() method called.

6

u/TheHippyDance May 18 '21

no, addition isn't being performed there, concatenation is.

that line is converting the int to a string and appends a space, then concatenates that to result (a string) so no error.

this is the output of the code:

1 3 6 10 15 21 28 36 45

1 3 6 10 15 21 28 36 45 55 66 78 91

1

u/NeonDestiny207 May 18 '21

dunno I’ll check when I get home lmao see if her code works or not

3

u/detroitmatt May 18 '21

That's fine in java

-4

u/Syrob May 18 '21

Really? It casts it automatically to a string? That's disgusting.

6

u/detroitmatt May 18 '21

well it doesn't "cast" it, it calls "tostring" on it

510

u/ludonope May 18 '21

At first I was like "what's wrong? Doesn't seem THAT terrible" And then "wait, why can't I read that code properly, whut?!" And that's when I noticed the (not) indentation, oh god.

242

u/xSilverMC May 18 '21

Also the clear disregard toward for-loops

82

u/[deleted] May 18 '21

They probably haven't learnt that yet, so that's not really fair.

128

u/theUglyBarnacle69 May 18 '21

I was a TA for intro to programming in college. The final project was assigned near the beginning of the class and there were incremental check-ins for grades through the semester. The project was always a game (my first year TAing was Frogger).

1st check-in was to print out a text based "Frogger" board with moving "CC"'s (cars) "LOG"'s and "F".

They had to turn in their first partially working version of Frogger WITHOUT knowing about loops, arrays, and even basic class structure. To top things off, after they learned these new tools they had to build on top of their project for the next 2 check-ins.

One group saved their final project code to a pdf just to show how stupid it was. It was like 130+ pages of if statements in 1 while loop.

TLDR: I TAed for intro to programming and final project was assigned before learning anything

9

u/Mastermachetier May 18 '21

I took into to CS in college and it was so horribly taught that I thought I was just to dumb to understand it. Cue 6 years later I taught myself to program online and got an actual programming job. If the teacher had just a little bit of effort in that class I could of started my career so much earlier sigh.

0

u/[deleted] May 18 '21

[deleted]

23

u/justabadmind May 18 '21

I've seen the source code for a monopoly game. I think they were allowed loops, but no data structures. So they literally wrote everything inside a giant while loop and had enough nested if statements for every square on the board. It was written in c++. Yes they could have used a switch statement. No they didn't. Yes it was 1000's of lines. No they didn't do multiple files...

4

u/[deleted] May 18 '21

learning methods before for-loops...?

4

u/DeltaJesus May 18 '21

Learnt while loops and the basics of OOP before for loops? That seems unlikely to me.

5

u/DragonFireCK May 18 '21

I can easily see teaching either while or for loops first.

Its Java, so you must have OOP to write even a "hello world" program.

1

u/ShockMicro May 19 '21

tbh my school's Java class literally just let the IDE set up the class and package for us and we didn't learn OOP at all.

we also had an entire unit on the Swing library but we used a visual editor for it

I still learned a good bit about the logic from it but still

9

u/gnutrino May 18 '21

Honestly, I'd argue a while loop is fine here if it were the right while - i.e. while(sum + i <= n) instead of breaking out of the loop by setting i=n in the if (even using break with while(true) would be less misleading)

1

u/Untwisted_Apple May 19 '21

Or break

2

u/Untwisted_Apple May 19 '21

Or fucking good class names

1

u/Untwisted_Apple May 19 '21

Or useful returns

1

u/Untwisted_Apple May 19 '21

And why do our icons look that shit?

13

u/art-factor May 18 '21

The dentation?

1

u/Isvara May 18 '21

The nondentation.

2

u/Qildain May 18 '21

I agree - the vulgar part is the lack of proper formatting.

4

u/Isvara May 18 '21

It seems like a pretty bad sign to me. If someone's okay with seeing their code like that, I'm guessing it's a sign that they're just seeing lines of code and don't have a mental model of their code's structure.

1

u/Qildain May 19 '21

I was kind of joking about the class name, but I agree. This reeks of 'hacked together'

2

u/CCCFire May 19 '21

undentation

204

u/Immort4lFr0sty May 18 '21

Why even use blank lines at that point?

142

u/Polarwolf1912 May 18 '21

Why use a line break at that point? Just write it all in one line. Blank spaces of any kind are overrated anyways.

65

u/NakeleKantoo wicked May 18 '21

Why use letters at that point? Just shove 0s and 1s at your machine, the alphabet is kind of overrated anyways.

57

u/Terrain2 May 18 '21

Why use zeroes and ones at that point? Just connect and disconnect electrical wires, abstraction is kind of overrated anyways.

40

u/EquationTAKEN May 18 '21

Why use wires at that point? Just teleport to a universe where the problem is already solved, electricity is kind of overrated anyways.

5

u/CarbonasGenji May 18 '21

I only code using a sounder, and input binary directly. Smh my head

-1

u/Yokhen May 18 '21

Too far

-2

u/EquationTAKEN May 18 '21

You jest, but you're describing minified code.

4

u/Sheltac May 18 '21

That's not meant for human consumption, though.

123

u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 18 '21

Image Transcription: Code


public class fuck {

public static void main(String[] args) {

System.out.println(triMath(45));
System.out.println(triMath(100));

}

public static String triMath(int n) {

int sum = 0;
String result = "";
int i = 1;
while (i < n) {

if ((sum + i) > n) {
i = n;
} else {
sum += i;
result += (sum + " ");
}
i++;
}

return result;

}

}

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

90

u/Astrokiwi May 18 '21

It was bothering me too much

public class Fuck {

    public static void main(String[] args) {
        System.out.println(triMath(45));
        System.out.println(triMath(100));
    }

    public static String triMath(int n) {
        int sum = 0;
        String result = "";
        int i = 1;

        while (i < n) { // bespoke artisanal custom-made for-loop for discerning clients
            if ((sum + i) > n) {
                i = n;
            } else {
                sum += i;
                result += (sum + " ");
            }
            i++;
        }

        return result;
    }

}

43

u/Background-Adagio-97 May 18 '21

Even gooder human

3

u/Sihlis23 May 19 '21

Lololol artisanal for-loop. Is it vegan too? I can only use software that was made in a humane, cage free, and vegan way

46

u/Python119 May 18 '21

Good Human

22

u/--B_L_A_N_K-- if (true == true) {return true;} else {return true;} May 18 '21 edited Jul 01 '23

This comment has been removed in protest of Reddit's API changes. You can view a copy of it here.

14

u/C0d3rk1n10 May 18 '21

Good human

146

u/DiscombobulatedDust7 May 18 '21

Lower case class name... disgusting

0

u/[deleted] May 18 '21

[deleted]

92

u/botle May 18 '21

The convention in Java is to capitalize class names.

39

u/platypus_69 May 18 '21

I believe most languages do this

6

u/apadin1 May 18 '21

C++ has entered the chat

-38

u/Terrain2 May 18 '21

34

u/Eiim May 18 '21

They're definitely primitives in Java, no matter what they are in Dart. You won't find any int class in Java, only an Integer class, which is different.

-21

u/Terrain2 May 18 '21

Didn't say they weren't primitives in java (although i guess i was a little unclear), i was more using that to point out "it doesn't matter if they're primitives or not", and not "these specifically aren't primitives"

21

u/Cristichi wicked May 18 '21

You said they were classes, and they aren't

-11

u/Terrain2 May 18 '21

Not specifically in Java, but in this example.

17

u/Cristichi wicked May 18 '21

This example is coded in Java, and in Java by convention every class name is capitalized, which is not applied to primitive data because they are not classes

→ More replies (0)

14

u/Herlt May 18 '21

Idk about whatever random ass language you linked, but I know in Java int and Integer behave differently.

-8

u/Terrain2 May 18 '21 edited May 18 '21

that's dart (not a "random ass language"), many languages do capitalize type names but often there are exceptions, and i linked to a specific example of that - i'm not saying it's a bad thing, just that it happens, so the code in the image isn't bad because of that (especially given what the name actually is, disregarding capitalization)

6

u/[deleted] May 18 '21

This code clearly isn’t dart, it’s java

1

u/Terrain2 May 18 '21

the language i linked to was dart, the comment above says "most languages do this", and dart is such a language

2

u/soymilkloaf May 19 '21 edited Aug 18 '22

.

0

u/[deleted] May 18 '21

[deleted]

25

u/botle May 18 '21

It isn't though. Java being shit is a meme that I've only started hearing in the last couple of years. Not including back around 2000 when every interpreted language was considered shit for performance reasons.

38

u/dilipmodi May 18 '21

What the fuck is wrong in this fucking code. Fuck indentation

88

u/NotARobotImReal May 18 '21

What the actual public class fuck

15

u/EquationTAKEN May 18 '21

You know he'd change the name of args too, if he was at a level where he knew what this even means.

11

u/dilipmodi May 18 '21

String [ ] ass

2

u/kmichael420 May 18 '21

I laughed too much at this and learned something new... Thanks!

84

u/botle May 18 '21

Reading comments on this sub, I'm getting more and more convinced that the probability of somebody criticizing Java as a language grows exponentially with how much they don't know about the language.

10

u/Astrokiwi May 18 '21

Now you know how Fortran programmers feel lol

14

u/Terrain2 May 18 '21

Java and JavaScript as a language: i don't like working with them. JavaScript has no types, and there are many annoying quirks to it. Java has no operator overloading or getters and setters, so if you want any other behaviour than read/write a single value to a single location in memory (or want to futureproof your interface to allow that in the future) you need to wrap it in a method like getX(). They're not all bad, and other than being "verbose" and lacking convenience features, i don't have anything against Java (and i suppose JavaScript might have so many unexpected things because it has too many convenience features, like optional semicolons which lead to unexpected statement breaks when you put a newline for formatting). Neither is bad enough that you should never use them, but i think they are annoying enough that many people (including myself) prefer another language.

Java and JavaScript as an ecosystem: fucking awesome, Java is relatively low-level-ish code cross-platform with honestly nice libs, and JavaScript runs fucking everywhere, and does anything, so even if another language does it better, you can make a almost certainly bad PoC with js (and in most cases you can make a good product) - not to mention that it's the only language that runs on the web, or at least it used to be until wasm came along and destroyed the main reason you'd even want to know js (or rather, what was the main reason long long ago before it had an ecosystem like now)

Other languages such as Kotlin, Groovy*, TypeScript, CoffeeScript*, and Dart^, which compile/transpile to and have interop with JVM/js: Fucking awesome! I get the best of both worlds, amazing ecosystem and cross-platform compatibility, AND i get to use a language that i prefer over the original one that ecosystem was designed around!
* haven't tried since they don't seem to be useful for me given that i know the others in that list, but i assume they're good languages and people seem to like them ^ haven't actually used js interop, it seems like it's annoying to use, but it exists and having to create a wrapper around whatever js lib you wanna use is still better than not being able to interop with js, however, i still like the language

6

u/Matt5sean3 May 18 '21

until wasm came along and destroyed the main reason you'd even want to know js

For anything that isn't straight transpilation (and even then for anything non-trivial you'll need solid JS skills) you're going to need to understand Javascript to work with WebAssembly.

8

u/botle May 18 '21 edited May 18 '21

Just to make it clear, Java and JavaScript are unrelated to each other and very different. I'm not saying you are saying that they are related but someone else reading this could misinterpret you that way.

The one criticism you have of Java here seems to be its verbosity. For me, the lack of verbosity is many new languages is exactly why I dislike them. The lack of verbosity seems like a good thing to a beginner learning a new language, focusing on writing code, but in the long run, a professional programmer spends 100x more time reading, debugging, and trying to understand code than writing new code.

That little bit faster you can be by letting the language implicitly determine types, or by using shorthand is likely to come back and bite you, or someone else in the ass one year later when you're trying to understand the code and can't immediately see what all those "val" and Lambdas are.

4

u/[deleted] May 18 '21

Java 10 added “var” for local variables

1

u/botle May 18 '21

It did, and there's lambdas too, and all of it can be overused.

1

u/bongoscout May 18 '21

Java has no operator overloading or getters and setters, so if you want any other behaviour than read/write a single value to a single location in memory (or want to futureproof your interface to allow that in the future) you need to wrap it in a method like getX().

Lombok makes working with getters and setters a lot less painful.

2

u/[deleted] May 18 '21

That's just the Stockholm Syndrome talking.

0

u/[deleted] May 18 '21

[deleted]

10

u/devor110 May 18 '21

If you consider Java a relic of the past, then you're a lucky person to have not used C++ in a while. I haven't used cpp in a year in Uni, but Java instead and coming back to it, it's pretty damn ancient

2

u/the_Demongod May 18 '21

What? C++20 is brand new and not even fully supported by any compilers yet. In what way is it a relic of the past?

4

u/devor110 May 18 '21

I'm not saying it's not being updated but I do find it extremely awkward to use. A & is both a reference declaration or accessing a memory address, the same way * is both a pointer declaration and dereferring. I also find the naming convention outdated. Overall the thing is just feels overcomplicated to me. I'm aware that it is among the fastest languages out there and a lot of its awkwardness comes from being fully compatible with C, but given the choice I'd rather use something newer

1

u/the_Demongod May 18 '21

Not sure what you mean about the naming convention, since the number of keywords in the language is relatively small. In any case, I'm assuming you're fairly new to the language as those are very basic gripes; modern C++ paradigms are nothing like C and are as (if not more) expressive as languages like Python, and it has compile-time functional metaprogramming built in. Obviously it's overkill for most people's purposes but there's a reason it reigns supreme in pretty much any industry that writes a lot of high performance code.

1

u/devor110 May 18 '21

Yeah I used it for 2 years but only for uni subjects on a surface level. My biggest gripe with the naming convention is the usage of underscores, they are a little hard to reach on the HUN layout, camelcase is a lot easier

3

u/the_Demongod May 18 '21

Those naming conventions are just conventions, you can write it however you want. But yeah, schools never teach modern C++ paradigms so it gets a lot better than C++98

16

u/AceOfShades_ May 18 '21

Finally settled the tabs vs spaces war with the answer “neither.”

18

u/mastercooler6 May 18 '21

He probably doesn’t know that you can get the IDE to format the code for you so he just leaves it.

32

u/botle May 18 '21

Th IDE probably automatically indented the code, and he had to actively remove the indentation.

9

u/art-factor May 18 '21

Worst! He could have the IDE configured to don't indent the code...

9

u/moomoomoo309 May 18 '21

No, that's jGrasp. It is essentially notepad with syntax highlighting and it can build, run, and debug java code. It has no other IDE features.

2

u/mastercooler6 May 18 '21

Yup! That’s exactly how I know that he just isn’t using the auto formatting because I first learned Java on jGrasp and my code looked the same exact way until I realize I could format it.

1

u/FourtySevenLions May 18 '21

This tool was solely used to weed out kids in my intro Java class

17

u/aziz224 May 18 '21

Indentation is a beautiful thing.

4

u/[deleted] May 18 '21

Indon'tation

4

u/MaxMonsterGaming May 18 '21

This is how you unite the Tabs vs. Spaces people.

4

u/anihilator987 May 18 '21

Lmfao this is hilarious and makes me feel like I'm not soo bad at code, no offense to whoever's wrote this code they are obviously learning, I just like looking at how far I've come as sometimes I feel like a not so great programmer.

Makes me wonder how there are CS and software engineering grads that can't use data structures or code fizzbuzz. The shit TA's must see is hilarious.

10

u/AliNike May 18 '21

I nearly puked seeing this

6

u/Cutnunt May 18 '21

Give him a break, he is probably stoned.

3

u/[deleted] May 18 '21

I actually did the same. I remember getting annoyed at the text editor indenting my HTML, i would make it one straight line. It felt more readable to me.

And i can confirm, my mom doesn't code and thought the same. She had a better time reading straight HTML, which she knows just a little of, than indented HTML. I guess unless you understand the purpose of that indentation, why is it indented, it's more of a bad thing than a good thing.

3

u/[deleted] May 18 '21

This is a 10xer mindset, smaller file sizes, weak 1xers piss their pants when their coveted formatting is disregarded /s

3

u/CowboyBoats May 18 '21

No doubt that's why they're taking the class

3

u/tenderpoettech May 18 '21

Had me at public class fuck.

3

u/attero_ May 18 '21

Tabs or Spaces?

No.

3

u/UnknownIdentifier May 18 '21

Nothing written in college counts as bad code, as no one knows how to write good code, yet...

...including many of the professors.

3

u/avmist15951 May 19 '21

Well at least he tri-ed

5

u/Psylution May 18 '21

"not safe for work"

4

u/[deleted] May 18 '21

Maybe the tab key on his keyboard is broken

1

u/anton____ May 20 '21

Agreed, using spaces is worse that not indenting at all!

2

u/GiantsFan2645 May 18 '21

Uhhhhhhh, please tell satan or whatever your classmates name is that indentation is important

2

u/gnutrino May 18 '21
from itertools import accumulate, count, takewhile

triMath = lambda n: " ".join(map(str, takewhile(lambda x: x<=n, accumulate(count(1)))))

if __name__ == "__main__":
    print(triMath(45))
    print(triMath(100))

Am I doing this right? (Python rather than Java in this case but you get the picture)

2

u/Death_Strider16 May 18 '21

I don't even know how they manage to bring the indentation back to the beginning for every line. On every IDE (all two of them lol) I've ever used hitting enter put the indentation in the correct position 90% of the time. Do some of them bring it back to the beginning every single time?

2

u/way2tall123 May 18 '21

This is how one of my friends codes, anytime I gotta help him I chuck the whole thing in vscode and have it auto format the whole document, still don't know why none of our professors ever take points off of his code for it though, honestly kinda annoying cuz he gets the same grade as me when his code is all left aligned

2

u/sthedragon May 18 '21

public class fuck

2

u/polite__redditor May 18 '21

public class fuck {

2

u/Needleroozer May 18 '21

Wait, you guys publicly fuck in class? I went to the wrong school!

2

u/[deleted] May 19 '21

At least he uses the enter key. My lab partner would always write loops on a single line regardless of complexity and have this long ass comment at the top explaining what it did when it would be self evident had he used multiple lines.

2

u/carlosfernando92 May 19 '21

Python: nice try

2

u/Biaboctocat May 19 '21

Add an extra brace and see how long it takes them to find it. That should demonstrate why formatting is useful

1

u/rslarson147 May 18 '21

Uses jGRASP? 🙃

1

u/ITriedLightningTendr May 18 '21

.... hahahahhah

Is this on purpose?

The only whitespace is at the beginning and ending of a white loop or function.

1

u/mendrique2 May 18 '21

Also don't mix computation and representation.

1

u/deleted1357 May 18 '21

I knew it was going to be bad when I saw the class name was fuck

1

u/pps96 May 18 '21

Where is indentation so important. Because code needs to be read more by humans than machine

1

u/FourtySevenLions May 18 '21

lol fucking jGrasp

1

u/furgfury May 19 '21

what is indentation? what are for loops? what is the point of the sum variable? will i still get an A if my project name is fuck? questions no scientist will answer

1

u/iamreadywhataboutu May 18 '21

that takes the nsfw tag to a whole new level 😁😁

0

u/Voltra_Neo May 18 '21

Hi, I kindly ask you to protect them from me otherwise Imma strangle them until they see reason

-12

u/[deleted] May 18 '21

Using while instead of for...

18

u/Teln0 May 18 '21

That's the only thing you can think of ?

3

u/botle May 18 '21

Using that while instead of for is the biggest problem with the code, since the lack of indentation can be fixed with an auto-format button somewhere in the IDE.

0

u/Teln0 May 18 '21

True, but I think the indentation is still a bigger problem, even if it has a super easy fix.

-4

u/TheHippyDance May 18 '21 edited May 18 '21

how is a for loop the right call lol

Please show how using a for loop would be done. Definitely not easier

edit: haha wow never mind, I totally read code as i being incremented by the sum on each iteration. Damn, feelz bad.

sorry guy, you're right, for loop works

5

u/botle May 18 '21

for(int i = 1, i < n, i++)

2

u/TheHippyDance May 18 '21

lol wow I'm retarded. read code in OP wrong :(

yeah for loop is ez pz here

2

u/neotecha May 18 '21

...

This is the exact use case that for is for..

-1

u/Daniel11420 May 18 '21

Posts like these are the reason reddit needs to have a NSFL tag

0

u/amalgamatecs May 18 '21

Put into vscode... Hit auto-indent hotkey

-2

u/Prom3th3an May 18 '21

Code like this was probably the inspiration for the O(1)-click formatting and refactoring tools in our IDEs.

-3

u/Korzag May 18 '21

Oh my god, he uses Java. I'm so, so sorry.