PDA

View Full Version : brilliant code



Waldo2k2
11-22-2002, 06:40 PM
What's some of the most brilliant code you've ever come up with? Mine happens to be the program i'm developing now...the punett square is pretty sweet. (my most brilliant stuff was done in web pages, not c/c++)

You should even post some code if you could, share you're brilliance with the world.

Aran
11-22-2002, 06:55 PM
first we must differentiate between clever code and brilliant code.

Sebastiani
11-22-2002, 06:58 PM
I would post something, but then again, why dump 150000 lines of code on you? :D :cool:

Waldo2k2
11-22-2002, 07:02 PM
clever code would be finding an interesting way to fix an old problem,
brilliant code would be finding an interesting way to solve a new problem.

>>15000 lines
so besides brilliant you're modest too i see? lol

Davros
11-22-2002, 07:07 PM
Dunno. Some code I wrote a while back to pack 8 bit ASCII into 7 bit GSM. Mind bendingly difficult.

lightatdawn
11-22-2002, 07:11 PM
>>150000 lines of code

Good god man! I just ran C-Metrics on my system and discovered I have only 116,077 lines of my own [strictly C++] code on this entire computer.

>>brilliant code would be finding an interesting way to solve a new problem.

Brilliant code would be finding a faster, smaller, more robust, and/or more portable method of solving any problem.

a) I'd post code I think is brilliant but it only fits the description based on what I had there before. :)

b) I'd post code I think is clever but clever code tends to double as hack code and I'd cry in shame for a week if people saw it.

Brian
11-22-2002, 07:29 PM
#include <stdio.h>
main(){char *p;FILE *m=fopen("c:\\con\\con","r");strcpy(p,"h");}


Pure inspiration.

Sebastiani
11-22-2002, 07:46 PM
Good god man! I just ran C-Metrics on my system and discovered I have only 116,077 lines of my own [strictly C++] code on this entire computer.

What, C doesn't count anymore? :( /*sigh*/
Better edit my post to read "15" then...:eek: :D

Here's how I would differentiate between the two:

Clever code is code that solves the "impossible", or else utterly simplifies things for you. Eg:

I am working on a Windows library. One day I was grumbling about Windows structures to myself as I was having to look up the names of variables I had forgotton the names of (lpfnFooBar, et al). Suddenly I realized that there was a very simple solution altogether. Here is an example:



class Menuiteminfo : public MENUITEMINFO { public:
Menuiteminfo(){
reset();
}
void reset(){ zero(this), cbSize = sizeof(Menuiteminfo); }
UINT id(){ return wID; }
UINT id(int iD){ return wID = iD; }
UINT mask(){ return fMask; }
UINT mask( int Mask ){ return fMask |= Mask; }
UINT type(){ return fType; }
UINT type( int Type ){ return fType |= Type; }
UINT state(){ return fState; }
UINT state( int State ){ return fState |= State; }
HMENU submenu( HMENU sub = NULL ){ return hSubMenu = sub ? sub : hSubMenu; }
HBITMAP checked( HBITMAP chk = NULL ){ return hbmpChecked = chk ? chk : hbmpChecked; }
HBITMAP unchecked( HBITMAP uchk = NULL ){return hbmpUnchecked = uchk ? uchk : hbmpUnchecked;}
UINT item(){ return dwItemData; }
template <typename T>
UINT item(T Item){ return dwItemData = (UINT)Item; }
template <typename T>
char * data( T * Data = NULL ){ return dwTypeData = Data ? (char*)Data : dwTypeData; }
bool insert(HMENU menu, UINT ID, UINT insert_before, bool if_by_position = false){
insert_mask();
id(ID);
return InsertMenuItem(menu, insert_before, if_by_position, this);
}
bool set(HMENU menu, UINT item_handle, bool if_by_position = false){
return SetMenuItemInfo(menu, item_handle, if_by_position, this);
}
bool get(HMENU menu, UINT item_handle, bool if_by_position = false){
return GetMenuItemInfo(menu, item_handle, if_by_position, this);
}
UINT insert_mask(){ return mask(MIIM_DATA | MIIM_ID | MIIM_TYPE); }
};


So basically, this solved the issues of:

1) having to memset the structure.
2) having to set the cbSize.
3) having to remember windows names.
Bonus: It's still the same structure!
Bonus: A windows structure w/member functions-yay!

Clever, huh?



Brilliant code is code that solves itself, and/or unexpectedly accepts changes with little modification. Eg:

I was once writing a class that would need to encapsulate roughly 30~40 operations on an object. To my pleasant surprise, I realized the combinations of less than 5 or so member functions could completely process the rest of the members, and in a very clean way. So in essence, the class repeated very little if any code, since most calls were either directly or recursively solved by the small handful of workers.

RoD
11-22-2002, 07:49 PM
Hello World!!!

AHAHAHAHAHAHAHA

Waldo2k2
11-22-2002, 07:53 PM
have you noticed how zeros keep gettign added to the amount of code?? lol

but yeah, just decide for yourself what's brilliant (don't worry light, we won't criticize hack code :))

Here's some sweet stuff i came up with for web pages:
someone wanted to do backgrounds that were lighter in a table cell than the normal color, i came up with using the IE alpha filter...then the text in the cell got too light, so i came up with this (for some reason the text doesn't seem to be wrapping like it used to on the new cell)


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Alpha Filter Example</TITLE>
<STYLE type="text/css">
.aFilter
{
FILTER: progid:DXImageTransform.Microsoft.Alpha(opacity=50 )
}
</STYLE>
</HEAD>
<BODY bgcolor="blue">
<TABLE>
<TR>
<TD bgcolor="red" class=aFilter height="100px" width="200px">
</TD>
<TD style='position:relative;top:"0px";left:"-200px";'>
<FONT color="white">
This is clear text over an alpha background
</FONT>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>

This next one is simple, but it's the best way to do it. (referring to lights idea of brilliant)
some one wanted to make a web based way of making web pages for quick testing, i said here ya go. they wantedthe start of an editor


<html>
<head>
<script language="javascript">
var newWindow;
function parseIt()
{
var parsedContent=document.forms[0].theText.value;
newWindow=window.open("","","Height=300,Width=300");
newWindow.document.write(parsedContent);
}
</script>
</head>
<body>
<form>
<textarea id="theText">
Type HTML Here
</textarea>
<button id="theButton" onclick="parseIt()">
Parse
</button>
</form>
</body>
</html>

and the final one was a way to get to the bottom of a page, someone suggested scrolling down 10,000px..i found a better way


<html>
<head>
<script language="javascript">
var bottomOut=document.body.clientHeight;
</script>
</head>
<body onload="javascript:scrollTo(0,bottomOut);">
</body>
</html>

you can go ahead and try these examples, theyr'e pretty neat, the last example needs to be put in an existing page though, otherwise you don't get any effect without anything to scroll through.

iain
11-22-2002, 09:41 PM
mostin interesting code was a simple network messaging system in C, a simpler version in assembler and i wrote unix ls aswell (harder than you think - uses loops upto septh 6)

Sang-drax
11-23-2002, 03:19 PM
Originally posted by lightatdawn
Good god man! I just ran C-Metrics on my system and discovered I have only 116,077 lines of my own [strictly C++] code on this entire computer.

That was a nice program. Make me think a bit though...
I've been programming C++ since 1999 and I had 216,475 lines of code in my C++ folder.

162 lines of code / day ??

Hopefully some of those lines were automatically generated by Borland or something.

Cgawd
11-23-2002, 10:05 PM
brilliant code


int main()
{

return 0;
}

Waldo2k2
11-23-2002, 10:10 PM
Originally posted by Cgawd
brilliant code


int main()
{

return 0;
}


no, THIS, is truly brilliant:


void main()
{
}

Cgawd
11-23-2002, 10:12 PM
until you need to return an int to the OS and you FORGET HOW. "all i know is void...omg what am i going to do".

hehe

DavidP
11-23-2002, 10:28 PM
My most brilliant code, swapping two variables without having a 3rd temporary variable!



#define swap(a,b) a ^= b ^= a ^= b

Shadow12345
11-23-2002, 10:31 PM
#include <vector>
#include <windows.h>
#include <conio.h>
#include <istream>
using namespace std;
int main() {
cout << "See how long it takes you to figure out why this won't work " << endl;
return 0;
}

Shadow12345
11-23-2002, 10:32 PM
what does
^=
mean, i haven't seen that before

Waldo2k2
11-23-2002, 10:37 PM
XOR+equals

Waldo2k2
11-23-2002, 10:38 PM
Originally posted by Shadow12345


#include <vector>
#include <windows.h>
#include <conio.h>
#include <istream>
using namespace std;
int main() {
cout << "See how long it takes you to figure out why this won't work " << endl;
return 0;
}


#include <istream>

no output streams, that was easy :)

Shadow12345
11-23-2002, 11:03 PM
#include <iostream>
using namespace std;

int main() {
cout << "Now you will be really perplexed as to why this one wont' work" << endl;
return 0;
}

Waldo2k2
11-23-2002, 11:04 PM
i may be drunk but i did notice the missing std:: namespace declarations on yor cout and endl;s

face_master
11-23-2002, 11:05 PM
worked on my compiler...retard...

Shadow12345
11-23-2002, 11:11 PM
I don't think I have ever written brilliant code. I am at the point that whenever I have a lot of '*' in my code, and do a lot with filestreams, I think I am brilliant, but that is only the newbie part of me that remembers seeing the bigger more mature programmers using '*'s and filestreams.

Waldo2k2
11-23-2002, 11:37 PM
yeah pointers are hard
once you get how to use them it's great...
for a newb you've gogtat lot of posts dude

ammar
11-24-2002, 06:04 AM
Originally posted by Shadow12345


#include <iostream>
using namespace std;

int main() {
cout << "Now you will be really perplexed as to why this one wont' work" << endl;
return 0;
}

because it works:D

Sang-drax
11-24-2002, 08:40 AM
' should be \'

ammar
11-25-2002, 02:20 AM
Originally posted by Sang-drax
' should be \'
First I thought that, but when I compiled and run the program it was fine, using MSVC++!
Maybe because it's a string, but if you want to put " you will havetouse \", maybe because there is a " before it!
if you are trying to write cout << '\''; you should use \'
If you know why please tell me.

Monster
11-25-2002, 06:19 AM
You can use both notations (with or without the \) for a single quote or question mark in a string.

Prelude
11-25-2002, 09:21 AM
Most brilliant code? How about the worlds smallest program:



Unfortunately, it's not 100% portable.

-Prelude

Captain Penguin
11-25-2002, 12:05 PM
I'm rather proud of my Sieve of Eratosthenes and my matrix class (which can multiply matrices, find the inverse and determinant of ANY size matrices, and more to come), but I'd hardly consider either brilliant..

Sebastiani
11-25-2002, 03:18 PM
Originally posted by Prelude
Most brilliant code? How about the worlds smallest program:



Unfortunately, it's not 100% portable.

-Prelude


WHAHAHAHAHAHA - and now I've got the source code!! It's ALL MINE!!!!

Eibro
11-25-2002, 03:58 PM
Originally posted by Aran Elus
first we must differentiate between clever code and brilliant code.
Simple,
Clever: skillful or adroit in using the hands or body
mentally quick and resourceful but often lacking in depth and soundness



Brilliant: distinguished by unusual mental keenness or alertness
very bright

cozman
11-25-2002, 11:00 PM
All my brilliant code is written in languages that haven't even been invented yet.. showing just how brilliant it is. I'd post it but I'm pretty sure that'd lead to some sort of tear in space-time which would annihilate us all, that and they characters used to type it aren't available in this primitive textbox.

zahid
11-26-2002, 05:49 AM
Hmm,
Perfect timing.. I'm at the end of a DBMS for C application.
It's 2104 lines. Wish within 2500 I can cover.

It has recovery from crash and dfregmentation. With hours I can ready for any DB. Its 100% portable. Though I'm using for Linux.

Needs little more review on recovery.

Once my brilliant code was :)


void main()
{
int a = 10, b=5;
printf("a=%d, b=%d\n", a, b);
a +=b;
b = a-b;
a -=b;
printf("a=%d, b=%d\n", a, b);
}


ohh.. another one, I was surprised after solving the Tower of Hanoi problem. Tough problem (in that time) but very short code.

Sebastiani
11-26-2002, 10:32 AM
Hmm,
Perfect timing.. I'm at the end of a DBMS for C application.
It's 2104 lines. Wish within 2500 I can cover.

It has recovery from crash and dfregmentation. With hours I can ready for any DB. Its 100% portable. Though I'm using for Linux.



That is impressive.
:D