The dirtiest code you will ever see

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
Post Reply
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

The dirtiest code you will ever see

Post by nikki »

Today while reading the kernel sources for 'printk', I decided to write my own version of sprintf. About 20 minutes later I came out with this abomination:-
Click if you dare
User avatar
spacegaier
OGRE Team Member
OGRE Team Member
Posts: 4308
Joined: Mon Feb 04, 2008 2:02 pm
Location: Germany
x 137
Contact:

Re: The dirtiest code you will ever see

Post by spacegaier »

The "good" old goto :D
Ogre Admin [Admin, Dev, PR, Finance, Wiki, etc.] | BasicOgreFramework | AdvancedOgreFramework
Don't know what to do in your spare time? Help the Ogre wiki grow! Or squash a bug...
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: The dirtiest code you will ever see

Post by Klaim »

Urgh....

:mrgreen:

By the way, stackoverflow.com and thedailywtf.com are full of this kind of code that are in production softwares...
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: The dirtiest code you will ever see

Post by xavier »

"Production software" doesn't necessarily mean "clean and elegant"; you should see some of the code in the games you play... ;)
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
Fulmens
Gremlin
Posts: 151
Joined: Sun Jun 22, 2008 8:29 pm
Location: Nantes, France
x 1

Re: The dirtiest code you will ever see

Post by Fulmens »

xavier wrote:"Production software" doesn't necessarily mean "clean and elegant"; you should see some of the code in the games you play... ;)
http://www.gamasutra.com/view/feature/4 ... tricks.php

Well...after i started to really work, I never saw computing sciences the same way :roll:
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: The dirtiest code you will ever see

Post by Klaim »

I and my colleagues have done some too, sure :)

I remember that time when we were in the midle of the night trying to fix a bug that would add another bug in another level if you fix it on the first level and vice-versa. The boss was an elder programmer and came to my computer to see how he could help. Then he said : "this feature have different behavior in those two levels right?" - me:" yes" -"ok then make copy of this behaviour code targeted at the less commmon level and fix the bugs separately for each level".

It was the pork-est suggestion ever. But it did fix the problem :mrgreen:

I remember other tricks like the ones in the article too.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: The dirtiest code you will ever see

Post by nikki »

Actually, I found code from popular open source projects to be pretty neat (except a few GNU things, like GlibC :roll: ).
reptor
Ogre Magi
Posts: 1120
Joined: Wed Nov 15, 2006 7:41 pm
Location: Finland
x 5

Re: The dirtiest code you will ever see

Post by reptor »

A few years back I was a little shocked when a very experienced C programmer (retired, in fact) gave me his source code for a small program. It was a file conversion program.

The goto was in there in many places. But the code was hard to read in general, it was not just the goto, but all of it that was a problem. It was pretty much a mess. There were certainly no formatting rules in use for the source code when he wrote it - or perhaps he had a rule that formatting shall not matter! Certainly not good for the faint-hearted.

Hey, but the program worked... isn't that the whole purpose!? :)

I have to say I re-wrote the program in C++ and saw no noticeable performance decrease. If there was an actual decrease in the performance then it was very insignifant because it didn't really show up in my tests. Maybe with very big data there would have been some noticeable difference - or perhaps my code just was more effective (I find this hard to believe knowing I am not a pro in this business and knowing C should be the faster of the two languages).
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: The dirtiest code you will ever see

Post by Klaim »

Often, it's the compiler that writes the dirty code :mrgreen:


nikki> Yes and in fact I tend to write all very very clean in my home projects.... and as they are in constant refactoring it takes a lot longer to finish something...

Now I'm trying to learn how to get dirty sometimes. I learnt that if you isolate enough dirty code, it's easier to refactor, then you can focus on releasing something.
User avatar
mkultra333
Gold Sponsor
Gold Sponsor
Posts: 1894
Joined: Sun Mar 08, 2009 5:25 am
x 116

Re: The dirtiest code you will ever see

Post by mkultra333 »

I only code dirty. Way dirtier than that sprintf function... That looked pretty clean to me. (Edit: Ok, I pretty much never use Goto, although the poor statement is so maligned I feel a certain compassion for it.)

I'm not a trained programmer, and I often find the "clever" fanatically OO projects vastly more difficult to understand than the more C like ones. Maybe because I'm always coming in from the outside, looking at open source code that usually has no documentation. Things like the source to Quake 3 Arena, Q3Map2 and various versions of Radiant. They usually lack anything in the way of code documentation and are fairly sparsely commented.

The worst was GtkRadiant. Intensely OO and totally incomprehensible. At least when I start trying to work out how a C-like program works I can plant message box and prints about the code to trace execution. That works with Quake 3. With GtkRadiant I just found program execution vanishing into the aether beyond a certain point. Now I'm using DarkRadiant which is a sort of refactored, cleaned version of GtkRadiant. It's still largly incomprehensible to me, but I have managed to get it doing what I want with more ease than GtkRadiant.

I think OO is oversold... too much is a bad thing, and I'm not really convinced that it's the only way to go on small or medium sized projects. I can see how it has its place when you have to keep a hundred programmers in line on a hundred million dollar project though. And I admit I should use it a little more myself.
"In theory there is no difference between practice and theory. In practice, there is." - Psychology Textbook.
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: The dirtiest code you will ever see

Post by nikki »

Well, obviously 'OO just because OO' will be bad. If it's for the purpose of code reuse, abstraction or such, then it isn't bad. For example, having an abstract 'GameObject' class that's the parent of all logical objects in my game has helped a lot.
User avatar
_tommo_
Gnoll
Posts: 677
Joined: Tue Sep 19, 2006 6:09 pm
x 5
Contact:

Re: The dirtiest code you will ever see

Post by _tommo_ »

Fulmens wrote: http://www.gamasutra.com/view/feature/4 ... tricks.php

Well...after i started to really work, I never saw computing sciences the same way :roll:
Good link :lol:

Anyway these times i tend to think that "dirty code" can even need strong programming skills! In fact, often i find myself speculating about an over-engineered solution to a simple problem... where a collection of specific solutions would be more solid and fast to develop.

One perfect example would be Lua support in my game: in the end the whole system was so much big and complicated, that changes were in fact HARDEST:
you had to change different lua files; the wrapper, or even the C++ code.
In the end creating an header-only class for each single item type, along with a Factory Pattern, was by many times the cleanest solution :roll:
This way i know exactly where and when i encounter one-time-use code... making it safer and more powerful that useless generic scripts.

In fact i thought that "YAGNI" was a thing for superficial developers... i didn't know that sometimes it is easier and more bug-proof to add "hacks" on "hacks" rather than putting everything in from the start and try to make code that does "anything" :D
OverMindGames Blog
IndieVault.it: Il nuovo portale italiano su Game Dev & Indie Games
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: The dirtiest code you will ever see

Post by nikki »

Some more C fun! This time, it's tries.
User avatar
xavier
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 9481
Joined: Fri Feb 18, 2005 2:03 am
Location: Dublin, CA, US
x 22

Re: The dirtiest code you will ever see

Post by xavier »

_tommo_ wrote:In fact i thought that "YAGNI" was a thing for superficial developers... i didn't know that sometimes it is easier and more bug-proof to add "hacks" on "hacks" rather than putting everything in from the start and try to make code that does "anything" :D
I live and breathe YAGNI -- it's saved me so much time and headache and "analysis paralysis". Assumptions made at the beginning of a "framework" development rarely end up being how practice turns out to be...and then you get to go back and re-architect the entire app (or slap heinous hacks in to work around the now-artificial framework constraints). So I'm 100% with you on this one. ;)
Do you need help? What have you tried?

Image

Angels can fly because they take themselves lightly.
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: The dirtiest code you will ever see

Post by Klaim »

Same here. Yesterday at night I refactored (totally rewritten in fact) an old event system in witch at the time (some years ago) I added some event cancelling functions, assuming it would be useful to cancel an event or events of the same message... I removed all that as it was never used and I got totally YAGNI style since last time I wrote this old code. That was a big relief, making the internal code really simple and explicit.

YAGNI, DRY, SoC are like Epice in my coffee :D
User avatar
syedhs
Silver Sponsor
Silver Sponsor
Posts: 2703
Joined: Mon Aug 29, 2005 3:24 pm
Location: Kuala Lumpur, Malaysia
x 51

Re: The dirtiest code you will ever see

Post by syedhs »

IMO, YAGNI design approach can only truly be appreciated by people of experience - well for people like me :mrgreen: Without any single dispute, it has been proven that designing software which fulfil today requirement is simply that simple & satisfying for both you - the developers and of course the clients. Designing for future is just BS, and for that development iteration is probably what it is for.
A willow deeply scarred, somebody's broken heart
And a washed-out dream
They follow the pattern of the wind, ya' see
Cause they got no place to be
That's why I'm starting with me
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56
Contact:

Re: The dirtiest code you will ever see

Post by Klaim »

IMO, YAGNI design approach can only truly be appreciated by people of experience
So true : this is not natural for beginners and it shouldn't be told until they hit problems relativee to it. That way, they understand the need. (but that takes time...)
User avatar
nikki
Old One
Posts: 2730
Joined: Sat Sep 17, 2005 10:08 am
Location: San Francisco
x 13
Contact:

Re: The dirtiest code you will ever see

Post by nikki »

Ok, now I made a little stack based scripting language after being inspired to do so reading the CPython source code. You can see it here. In case it isn't clear, $<name> basically creates a new variable with name <name>, popping a value off the stack and assigning it to it. There's no way to mutate (modify) variables yet. :P It's easy though, just that I have to sleep now.

There's a basic arithmetic function ('add') and output ('print'). It's easy to add new functions (check out ns_print and ns_add for the C versions of these, and ns_funcmap to see the script name -> function mapping). You can pass functions around, set a block of code to a variable, pass a block around etc. Kinda fun. :P
Post Reply