Jump to content

Bad, terrible or simply funny code examples

 

Python:

print ("Hello, world!")

Java:

public class HelloWorld{        public static void main (String[] args)        {                System.out.println("Hello, world!");        }}

by your defintion C++ and c# must be utterly crappy as well ...

#include <iostream> int main(){    std::cout << "Hello, world!" << std::endl;}
class MainClass{    public static void Main()    {        System.Console.WriteLine("Hello, world!");    }}

So I'd guess you would love BASIC ;)

 

____

 

 

I bet you know almost nothing about java, nor have you ever extensively used it ;)

 

edit: (Apple) SWIFT must be awesome as well ...

println("Hello, world")

Mini-Desktop: NCASE M1 Build Log
Mini-Server: M350 Build Log

Link to comment
Share on other sites

Link to post
Share on other sites

A couple of months ago I found a website full of this stuff: http://codecrap.com/content/best/

 

There really is some absolute gold on there...

int getRandomize(int randMax){  srand ( time(NULL) );  int randNum; = rand() % randMax + 1;  return 2;}
// Happy debugging, suckers# define true (rand() > 10)
try {    return true;} finally {    return false;}
int multiplyBy10(int number){    std::stringstream str;    str << number << '0';    str >> number;    return number;}
Link to comment
Share on other sites

Link to post
Share on other sites

#include <iostream> int main(){    std::cout << "Hello, world!" << std::endl;    return 0;}

 

Fixed. It will compile now. :P

 

EDIT: Here's another hilarious one from that site:

 

void get_tomorrow_date( struct timeval *date )  {    sleep( 86400 ); // 60 * 60 * 24    gettimeofday( date, 0 );  }

Well, that is one solution...

Link to comment
Share on other sites

Link to post
Share on other sites

Fixed. It will compile now. :P

 

EDIT: Here's another hilarious one from that site:

 

void get_tomorrow_date( struct timeval *date )  {    sleep( 86400 ); // 60 * 60 * 24    gettimeofday( date, 0 );  }

Well, that is one solution...

I refuse to believe someone can be that stupid.

Link to comment
Share on other sites

Link to post
Share on other sites

System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(64)))), ((int)(((byte)(64)))));

I think the developer was either drunk, sleep deprived or a combination of both perhaps :) Yes we are indeed casting an integer to a byte and then back to an integer again...

I'm pretty sure the point of that was to make sure he's passing a value between 0 and 255 and that's actually a clever way of doing it. That said, he's passing a number, not a variable. I guess that's what happens if you copy-paste a lot.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm pretty sure the point of that was to make sure he's passing a value between 0 and 255 and that's actually a clever way of doing it. That said, he's passing a number, not a variable. I guess that's what happens if you copy-paste a lot.

 

Just doing that kind of thing to ensure a byte value is very bad itself and not at all clever in any way.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Of course, if it wasn't for the fact that that it was hard coded. Moreover just doing that kind of thing to ensure a byte value is very bad itself and not at all clever in any way.

Well, how else would you do it then? I understand it shouldn't be necessary in most cases, but sometimes when you accept user input one needs some idiot-proofing... I noticed it was hard-coded right after I posted, that's why I added the copy-paste thing.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

Well, how else would you do it then? I understand it shouldn't be necessary in most cases, but sometimes when you accept user input one needs some idiot-proofing... I noticed it was hard-coded right after I posted, that's why I added the copy-paste thing.

Yeah I know sorry, I adjusted my post but just not in time, my fault for skim reading and trying to do too many things at once! There was defiantly a great deal of copy pasting/code duplication going on. These particular casts were found around hand adjusted standard controls (ick).

Stuff that's wrong with this kind of thing; introducing fragility, complexity, information loss and assumptions among other bad practices. There's many ways around it; being explicit in intent is probably the best, cultivating better object design is another. As for user input, without being specific in example to any particular language or methodology, again, I would say that the same arguments totally apply.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

Compared to Python, C, C sharp, C++, and others. Python is one of the most orderly logical languages, which is why it is the best for beginners. The code is very 'readable' if you know what I mean :)

 

I think Java has more order than C and C++...

Link to comment
Share on other sites

Link to post
Share on other sites

Anyone here learning Swift? I am, as my first Language. The OP is all nothingness in my eyes though.

Link to comment
Share on other sites

Link to post
Share on other sites

Anyone here learning Swift? I am, as my first Language. The OP is all nothingness in my eyes though.

 

I like your profile picture.

 

And, I'm getting really, really annoyed with HTML5+CSS3... you would at least think that it would let you resize text within a div without bringing yet another language into the equation. Ugh. Javascript.

INTEL i5 4670 (non-K) | MSI H81I | XFX Black Edition R9 280 | SAMSUNG 840 EVO 120GB


Seasonic M12II EVO 620W | SEAGATE Barracuda 2TB


CRUCIAL Ballistix Sport 8GB | CM Elite 130

Link to comment
Share on other sites

Link to post
Share on other sites

I like your profile picture.

 

And, I'm getting really, really annoyed with HTML5+CSS3... you would at least think that it would let you resize text within a div without bringing yet another language into the equation. Ugh. Javascript.

It does. Whats the problem? Javascript isn't needed for simple display. If you want to increase or decrease the size dynamically however, you need JS.

 

 

Anyone here learning Swift? I am, as my first Language. The OP is all nothingness in my eyes though.

I wouldn't advise learning a language, that's limited to one manufacturer.

You should probably start with something more global.

Link to comment
Share on other sites

Link to post
Share on other sites

I like your profile picture.

 

And, I'm getting really, really annoyed with HTML5+CSS3... you would at least think that it would let you resize text within a div without bringing yet another language into the equation. Ugh. Javascript.

/* Language = CSS */p{	font-size: 1em;	font-size: 12pt;	font-size: 12px;	font-size: 112%;}

-Turkey with a vengeance 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm pretty sure the point of that was to make sure he's passing a value between 0 and 255 and that's actually a clever way of doing it. That said, he's passing a number, not a variable. I guess that's what happens if you copy-paste a lot.

 

It's not really all that clever. If I want to be sure I am passing no more than a byte I can do this...

System.Drawing.Color.FromArgb( (r & 0xff), (g & 0xff), (b & 0xff));

Casting values is both less readable and a whole lot slower. (not actually 100% about slower, but I presume as much since a bitwise AND is just a single ALU op).

Link to comment
Share on other sites

Link to post
Share on other sites

It does. Whats the problem? Javascript isn't needed for simple display. If you want to increase or decrease the size dynamically however, you need JS.

 

 

-snip-

 

What?

 

/* Language = CSS */p{	font-size: 1em;	font-size: 12pt;	font-size: 12px;	font-size: 112%;}

 

Oh.

Right. I'll try that...

 

1fltZd6.png

 

You, sir, are complete gold.

INTEL i5 4670 (non-K) | MSI H81I | XFX Black Edition R9 280 | SAMSUNG 840 EVO 120GB


Seasonic M12II EVO 620W | SEAGATE Barracuda 2TB


CRUCIAL Ballistix Sport 8GB | CM Elite 130

Link to comment
Share on other sites

Link to post
Share on other sites

It's not really all that clever. If I want to be sure I am passing no more than a byte I can do this...

System.Drawing.Color.FromArgb( (r & 0xff), (g & 0xff), (b & 0xff));

Casting values is both less readable and a whole lot slower. (not actually 100% about slower, but I presume as much since a bitwise AND is just a single ALU op).

Thanks, mate. I was wondering if there was a better way.

I cannot be held responsible for any bad advice given.

I've no idea why the world is afraid of 3D-printed guns when clearly 3D-printed crossbows would be more practical for now.

My rig: The StealthRay. Plans for a newer, better version of its mufflers are already being made.

Link to comment
Share on other sites

Link to post
Share on other sites

i remembered of this code i saw on the forum, i spent like 10 minutes trying to understand what i was missing

 

i'll tag @RocK_Lee2 to be fair

this isn't meant to make fun of you, it's just that seeing a (good) implementation of "the XOR method" without the use of a single XOR operator seemed very funny to me :)

Link to comment
Share on other sites

Link to post
Share on other sites

It's not really all that clever. If I want to be sure I am passing no more than a byte I can do this...

System.Drawing.Color.FromArgb( (r & 0xff), (g & 0xff), (b & 0xff));

Casting values is both less readable and a whole lot slower. (not actually 100% about slower, but I presume as much since a bitwise AND is just a single ALU op).

But in the end what's the point of calling this at all if you get totally wrong color in case of values outside 0-255?

Link to comment
Share on other sites

Link to post
Share on other sites

But in the end what's the point of calling this at all if you get totally wrong color in case of values outside 0-255?

 

I agree, but if you really want to ensure a value is between 0 and 255, that's the way to do it....

Well, depending upon what you're doing. You might want to clamp it instead.

Link to comment
Share on other sites

Link to post
Share on other sites

It's not really all that clever. If I want to be sure I am passing no more than a byte I can do this...

System.Drawing.Color.FromArgb( (r & 0xff), (g & 0xff), (b & 0xff));

Casting values is both less readable and a whole lot slower. (not actually 100% about slower, but I presume as much since a bitwise AND is just a single ALU op).

 

Any decent compiler will optimize that sort of stuff away.

 

Let's look at what GCC does using -O3 optimizations:

 

#include <stdio.h>#include <stdlib.h>void foo(int x, int y){    printf("%d %d", x, y);}int main(void){    int i = 0xFEFE;    int j;    unsigned int marker = 0xDEADC0DE;    j = rand();    printf("%d", marker);    foo(j & 0xFF, (unsigned char)i);    return 0;} 

 

Turns into:

 

  		call	jmp_msvcrt.dll!rand  		mov	dword ptr [esp+04h],DEADC0DEh  		mov	ebx,eax  		mov	dword ptr [esp],L0040302A  		call	jmp_msvcrt.dll!printf  		movzx	ebx,bl  		mov	dword ptr [esp+08h],000000FEh  		mov	[esp+04h],ebx  		mov	dword ptr [esp],SSZ00403024__d__d  		call	jmp_msvcrt.dll!printf  		xor	eax,eax  		mov	ebx,[ebp-04h]  		leave  		retn

 

The important lines are 6, 7, and 8.

 

GCC recognizes & 0xFF, and the result of the rand() is truncated on line 6 using the movzx (mov and zero-extend) instruction taking the byte in bl and zero extending it back into ebx. Changing it to (unsigned char)j produces identical assembly.

 

It also recognizes i is used nowhere else, and doesn't even bother storing 0xFEFE instead putting the cast-truncated value 0xFE on the stack (line 7).

 

Then finally on line 8 it puts the full ebx register onto the stack since foo takes an integer. (The foo call is actually optimized away as well)

 

No ANDs in sight  :)

 

Notwithstanding the merits of simply truncating a color value.

main(i){for(;i<101;i++)printf("Fizz\n\0Fizzz\bBuzz\n\0%d\n"+(!(i%5)^!!(i%3)*3)*6,i);}

Link to comment
Share on other sites

Link to post
Share on other sites


for (var i = 1; i >= 1; i++){

console.log(i);

}

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

Any decent compiler will optimize that sort of stuff away.

 

Let's look at what GCC does using -O3 optimizations:

 

#include <stdio.h>#include <stdlib.h>void foo(int x, int y){    printf("%d %d", x, y);}int main(void){    int i = 0xFEFE;    int j;    unsigned int marker = 0xDEADC0DE;    j = rand();    printf("%d", marker);    foo(j & 0xFF, (unsigned char)i);    return 0;} 

 

Turns into:

 

  		call	jmp_msvcrt.dll!rand  		mov	dword ptr [esp+04h],DEADC0DEh  		mov	ebx,eax  		mov	dword ptr [esp],L0040302A  		call	jmp_msvcrt.dll!printf  		movzx	ebx,bl  		mov	dword ptr [esp+08h],000000FEh  		mov	[esp+04h],ebx  		mov	dword ptr [esp],SSZ00403024__d__d  		call	jmp_msvcrt.dll!printf  		xor	eax,eax  		mov	ebx,[ebp-04h]  		leave  		retn

 

The important lines are 6, 7, and 8.

 

GCC recognizes & 0xFF, and the result of the rand() is truncated on line 6 using the movzx (mov and zero-extend) instruction taking the byte in bl and zero extending it back into ebx. Changing it to (unsigned char)j produces identical assembly.

 

It also recognizes i is used nowhere else, and doesn't even bother storing 0xFEFE instead putting the cast-truncated value 0xFE on the stack (line 7).

 

Then finally on line 8 it puts the full ebx register onto the stack since foo takes an integer. (The foo call is actually optimized away as well)

 

No ANDs in sight  :)

 

Notwithstanding the merits of simply truncating a color value.

 

Thanks for the explanation! I am not very au fait with Compilers and x86 ASM (I did some 68k ASM at Uni but it wasn't a huge part of my course, we used C mainly, and I think there may have been an option for me to learn about compilers but I never took it).

 

Anyway, I would still recommend (x & 0xff) over ((int)((byte)x)) for readabilities sake. The latter looks horrible, imo.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×