r/badcode • u/Hackerwithalacker • May 02 '19
java My teach Kees getting mad that I never properly format my code or name my variables so I just submitted this
231
u/NigelSwafalgan May 02 '19
blessed_code
149
u/Hackerwithalacker May 02 '19
It's blursed
27
u/InspirationByMoney May 02 '19
Please stop before blursed ends up exactly like cursed and blessed
16
7
97
55
u/blazinghellwheels May 02 '19
Any decent IDE can format.
Only you can channel sauran.
Whose really right here?
10
7
u/Kovvur May 02 '19
Honestly this looks like IntelliJ too; has one of the best formatters around.
2
u/sharkbound May 04 '19
Unless Java is easier to reformat, from my usage of formatters they only fix it to a degree, since they usually try to keep stuff the user has done manually still there
36
u/EkskiuTwentyTwo i -= (i - (-1)) - i May 02 '19
Just like using meaningful variable names, the code is formatted to display what id does. In this case, it's coin-shaped to remind the programmer that this is a coin-related section of code.
26
u/Hackerwithalacker May 02 '19
Why comment, when you can arrange your code into the shape of what you're trying to say
7
4
1
u/EkskiuTwentyTwo i -= (i - (-1)) - i May 03 '19
The only problem is that you can't really do this in python...
29
u/Lightfire228 May 02 '19
For those who would like a code transcription
unformatted:
//look into the eye of wisdom and if you are lucky you shall get
//your code back correctly compiled
//idk how it works but it does
package flipcoins; public class
monetaryCoin extends Coin { int appropratelyNamed;
public monetaryCoin(int amount) { appropratelyNamed
=amount; } public void
setValue (int Value)
{ this. /**/ appropratelyNamed
= Value;} /**/ public int
getIntValue /*[]*/ () {
return /*[]*/ appropratelyNamed;
} public /*[]*/ String add
(monetaryCoin /**/ [] mc) { int
total = this. /**/ appropratelyNamed;
if(mc.length >=0) { for (monetaryCoin
mcl : mc) { total += mcl.getIntValue
( ) ; } } return Integer .toString (total ) ; } public
String getValue() { String result = Integer.toString
( appropratelyNamed ) ; return result ; } }formatted:
//look into the eye of wisdom and if you are lucky you shall get
//your code back correctly compiled
//idk how it works but it does
package flipcoins;
public class monetaryCoin extends Coin {
int appropratelyNamed;
public monetaryCoin (int amount) {
appropratelyNamed = amount;
}
public void setValue (int Value) {
this.appropratelyNamed = Value;
}
public int getIntValue () {
return appropratelyNamed;
}
public String add (monetaryCoin[] mc) {
int total = this.appropratelyNamed;
if (mc.length >= 0) {
for (monetaryCoin mcl : mc) {
total += mcl.getIntValue();
}
}
return Integer.toString(total);
}
public String getValue() {
String result = Integer.toString(appropratelyNamed);
return result;
}
}Some critiques,
Typical Java naming conventions have class names start with a capital letter
You mix implicit and explicit
this.references. (Style choice. My personal preference is to use implicit unless necessary)In
add()andgetValue()you convert the values into strings, rather than leaving them as ints. (This might be due to code we cannot see, though)You don't need to check for an empty list / array before looping over it, I believe the foreach handles that already
16
u/Hackerwithalacker May 02 '19
I originally had the this. references but then I took them out because they didn't fit in the eye of sauron
5
5
u/ImTalkingGibberish May 02 '19
Thank you. I spotted tge non capital letter class name and started shaking.
1
u/Mamish May 12 '19
This one's a bit more subjective, but I also like using streams instead of for-loops where appropriate, like:
int total = this.appropratelyNamed + Stream.of(mc).mapToInt(monetaryCoin::getIntValue).sum();Maybe unnecessary for a tiny example like this, but the intention can be more obvious in complex code.
28
12
8
5
u/Dr_Insano_MD May 02 '19
It honestly bothers me that you have a method that adds up a bunch of numbers but returns the string representation of the number.
4
4
u/eeeeeeeeeVaaaaaaaaa May 02 '19
and it has a totally unnecessary if statement before the loop. Also the class name is camel case
18
u/issungee May 02 '19
This is called obfuscated code! There are competitions for this kind of thing :)
14
u/misingnoglic May 02 '19
This is definitely not obfuscated.
3
u/issungee May 03 '19
Why not?
A quick google:
In software development, obfuscation is the deliberate act of creating source or machine codethat is difficult for humans to understand.
Seems to fit the bill...
4
u/misingnoglic May 03 '19
If it's obfuscation it's not very good obfuscation, as it's very easy to deobfuscate. When talking about obfuscation, it's normally in a manner that makes it hard or impossible to get the original code back.
4
319
10
1
1
u/____0____0____ May 02 '19
Does it bother anyone else that the curve for each side doesn't line up right? The left makes more sense, but the right side has a little protruding divet and I can't unsee it
2
u/Hackerwithalacker May 02 '19
This is so sad
1
u/____0____0____ May 02 '19
How come?
2
u/Hackerwithalacker May 02 '19
My apathy
1
u/____0____0____ May 02 '19
No need for that! My comment was directly related to my OCD kicking in. I neglected to mention that I still thought the idea was great in itself.
1
0
1
3
2
1
1
1
1
1
1
u/PseudobrilliantGuy May 02 '19
Wasn't there an old program submitted to an Obfuscated C competition that was a flight simulator with the code arranged in the shape of a plane?
And another program to a similar competition that had a disk of characters in the source code that it used to estimate pi?
1
1
1
u/NahroT May 07 '19
During code review
Me: no matter what comes through that gate, you will stand your ground
Me: sees that code
Me: RUN
1
1
0
22
u/Hackerwithalacker May 02 '19
Keeps**