Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
johnDoe32338yLet's be honest here. We've all written some nasty code Like this at least once when learning to program.
Looking back on some of my old code and wondering what the hell I was thinking. -
johnDoe32338y@asgs ¯\_(ツ)_/¯ I guess he gave up.
Could have done this (even more cancerous):
Bool go = true;
Bool wasEven = true;
Int i = 2;
Int ticker = 0;
While(go){
if(number == i){
Console.writeline("your number
Was even!");
wasEven = false;
go = false;
}
ticker++;
i++;
if(ticker >= 100000){
Console.WriteLine("I give up");
go = false;
}
}
If(wasEven){
Console.WriteLine("Your number
Was odd!);
} -
Arigion5038yThe sad thing is I found code similar to the above in production. Generated by a code generator the dev built because he thought he was smart to automate his work.
As a side note: The code generator generated class names with more that 255 chars which made them a tad difficult to delete on Windows machines. -
aquaman2138yA fond memory of mine learning code was during my CS250 class, I wrote this monstrosity of a program to make a board game for my final. All prior assignments were around 100 lines of code for a solution.
My solution for the final was 15k lines of code.
Of that, the AI was a single function that was 9k lines of code. It also looped over itself roughly 32 lines per move :') -
johnDoe32338y@aquaman the time before you learn how to properly implement class structures and effective method calling. Was your program terminal based? Or did you go with a UI?
-
aquaman2138y@nicnaknic it was a c++ console program, I was tasked with making Halatafl with a rudimentary AI. I did utilize classes (poorly) but my tragically bad design decision was storing the board as a single dimensional array. Since the board is shaped as a cross, I had the delusional thought it was a witty way to avoid nullchecking in the corners. Little did I know that the indexes would no longer align, and that I would have to hard code the AI for all 33 board positions.
-
johnDoe32338y@rallport most definitely. One Google search and you would have saved a hell lot of time
-
I remember one early lesson had me find out which of two numbers was smaller.
My solution was to subtract one from the other, and check if the result was positive or negative (or zero).
When I think of that, I wonder how I ever got not only a degree, but a job. -
@Grumpy Oh, really? That's really cool! I assume you just check the sign bit of the result?
-
Grumpy28878y@UrsidaeGames There are flags that are affected by the last arithmetic operation, so there's a negative flag and a zero flag.
It's a long time since I did any assembly stuff (because hardware is so much faster these days), but having done it gives you nice insights. -
@Grumpy Ah, okay. I've seen some Project Euler solutions written in x86 which have piqued my interest in learning assembler.
-
johnDoe32338y@antons
If(num % 2 == 0){
Console.WriteLine("your number was even!");
} Else{
Console.WriteLine("your number was odd!");
}
Easy enough -
johnDoe32338y@playmast3r that's the better way of doing it yes. How ever I'm making fun of bad programming techniques. Also I messed up, the i++; should be i+= 2; or the sloppy way. i = i + 2;
-
gaben2698y@nicnaknic inline with ternary:
Console.WriteLine("Your number was " + (num % 2 == 0 ? "even!" : "odd!")); -
moort43408yExplained to my gf what modulo (%) is en why this way was bad practice by using a manual example
Its like writing a manual for a microwave naming every animal that you cant put it in.
Instead of using one line saying,no animals -
c3ypt1c99058ydef even (number):
return 0 == number % 2;
bloody hell... This is a python one liner... -
@antons couldn't resist necroposting. JS:
console.log("The number is " + ["even", "odd"][userNumber&1] + "!"); -
@antons nope not really. A not so clever interpreter might actually do a division for that modulus operation, whereas the bitwise and operation is dirt cheap!
-
@antons to clarify a bit. Anding with 1 will give a number which will be either 0 or 1, coincidentally 0 when the number is even, and 1 when the number is odd. This then is taken as the index into the string array.
-
gaben2698y@siljamicke would make more sense to not use an array for this to avoid unnecessary memory allocation, no?
-
@gaben I'm not very proficient in JavaScript so maybe you're right in that there is a penalty for instantiating an array. But for amusement purposes I went for a oneliner without any if statements.
-
gaben2698y@siljamicke
return num&1 == 0 ? "Even" : "Odd";
if JS even has ternary operators. I don't use JS, so I'm not sure. -
@gaben ah, but that is a function call! Those aren't so cheap either, are they? But ternary exists, right? Surely they must!
-
gaben2698y@siljamicke fair enough. Guess it would largely come down to whether the language is GC or not.
-
@gaben given that my array will be referenced INTMAX times, I guess the cache hits will be okay, and the array allocation will pay off 😋
I'll put my money on the array over the function, GC or no GC. But then again, I'm a lousy gambler. -
@gaben actually I was clearly wrong. Of course it is not called more than once occasionally. My bad, and I will mysteriously remove my bet from the table.
Related Rants
!rant: Wondering how the hell you even graduated.
undefined
graduation