Jump to content

  • Log In with Google      Sign In   
  • Create Account


"Oh God, Who Wrote This?", "You Did", "Wait? I wrote This?"

  • You cannot reply to this topic
13 replies to this topic

#1 dejaime   Crossbones+   -  Reputation: 3480

Like
8Likes
Like

Posted 23 January 2014 - 12:12 AM

for (totalMatches = 1; // From the last input entry
         (totalMatches <= (pIt)->first->getSize() &&
         ((pIt)->first->compare( (pIt)->first->getSize()-totalMatches, eventLog[eventLog.size()-totalMatches]->getType(),
         translator->translate(eventLog[eventLog.size()-totalMatches]->getKeyCode()), getTimeDiff( (eventLog.size()-totalMatches)))))
         ;++totalMatches) {}

I guess that is the ugliest piece of code I ever wrote.
Notice the {} in the end. All the computing actually happens in the middle section of the for loop... where I use some megamoth functions...

God, this is ugly. I'll have to rewrite the whole function to get rid of this monster I created.

What this does is basically comparing two different vectors to see if they match.
The problem is that one of the vectors need to be translated and interpreted in order to compare with the other one...
There is also some math to calculate a time diff...

and it does it backwards...


Yeah, that's simply horrible, I know it, but I am thinking of it as a placeholder... lol
I do this all the time with sprites!
That's the first time I do that with code though! haha

 

But, of course, I wrote the whole thing while I should be sleeping and it didn't work at first. It took me almost 20 minutes to figure out I was advancing on only one of the vectors and always comparing the second's back. There were also some other less stupid mistakes, I probably wrote this loop in under a minute, but spent over 30 trying to fix it... so ugly... I really need some programming advice! lol

 

@edit:

And yet another bug on this loop. Hey kids, if you are reading this, do not, I repeat: do not, create monsters! They Bite!


Edited by dejaime, 23 January 2014 - 02:42 PM.


Sponsor:

#2 swiftcoder   Senior Moderators   -  Reputation: 9506

Like
9Likes
Like

Posted 24 January 2014 - 08:15 AM

I used to write a lot of "clever" code like this, when I was still giddy with C++'s power.

 

If experience has taught me anything, it's that the KISS principle should be branded onto every programmer's forehead at birth.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#3 dejaime   Crossbones+   -  Reputation: 3480

Like
0Likes
Like

Posted 24 January 2014 - 04:06 PM

Yeah, that's really common. But in my college they taught me pretty well this kind of best practices.

Of course, by then I only thought that was unnecessary and overrated, and only got to understand what I was taught long after that...

 

This one I knew was going to be a problem when I typed the r in for... "I'll make it like this anyway, who's going to read this anyway?", and ta-da! I probably spent 40 minutes debugging this loop...

Guess I'll start to do what needs to be done, it is not like I did this out of inexperience, neglect would be the word!

 

And I even have a KISS T-shirt... (I really do)



#4 boogyman19946   Members   -  Reputation: 1021

Like
0Likes
Like

Posted 24 January 2014 - 04:52 PM

Yeah, that's really common. But in my college they taught me pretty well this kind of best practices.

Of course, by then I only thought that was unnecessary and overrated, and only got to understand what I was taught long after that...

 

This one I knew was going to be a problem when I typed the r in for... "I'll make it like this anyway, who's going to read this anyway?", and ta-da! I probably spent 40 minutes debugging this loop...

Guess I'll start to do what needs to be done, it is not like I did this out of inexperience, neglect would be the word!

 

And I even have a KISS T-shirt... (I really do)

 

My university has horrible coding classes. I took three of them: Intro to C, and Comp. Sci. 1 & 2. 

 

In my last class, I had a teacher who used inconsistent namings, strange variable names, passed vectors by value like a baus, didn't bother deleting allocated memory, and wrote a crap ton of comments in the style "int add(int, int); // Adds two integers," some of which I found to be lying about their return values.

 

Currently, and this is about 4 years now since I started programming, I am quite anal about consistently structuring my code and focus quite about on making sure it's as straightforward as I can make it. Of course, everyone says I'm over reacting, but I've seen the effects of not caring, and I know how big of a benefit clarity and consistency carry.


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#5 swiftcoder   Senior Moderators   -  Reputation: 9506

Like
3Likes
Like

Posted 24 January 2014 - 08:10 PM


Of course, everyone says I'm over reacting, but I've seen the effects of not caring, and I know how big of a benefit clarity and consistency carry.

There is definitely a balance to be struck there. Ideally you are able to write both sorts of code: the throwaway prototype you needed yesterday, and the carefully structured (and tested) code that is headed for production.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#6 boogyman19946   Members   -  Reputation: 1021

Like
2Likes
Like

Posted 24 January 2014 - 09:56 PM

 


Of course, everyone says I'm over reacting, but I've seen the effects of not caring, and I know how big of a benefit clarity and consistency carry.

There is definitely a balance to be struck there. Ideally you are able to write both sorts of code: the throwaway prototype you needed yesterday, and the carefully structured (and tested) code that is headed for production.

 

 

I agree that perhaps it's unnecessary to structure code with utmost care when prototyping, but even if prototyping I feel like there's still things one could do to write cleaner, albeit throwaway, code.

 

The poster in this thread is a prime example: http://www.gamedev.net/topic/652538-pathfinding-behaviour/

 

I wouldn't put code like this in production, but I would certainly not write it as throwaway either. I managed to fix an error in his adding function merely by giving the variables proper names, which, if he had done in the first place, he would have noticed the glaring logical error. 


"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#7 DekuTree64   Members   -  Reputation: 953

Like
0Likes
Like

Posted 02 February 2014 - 08:22 PM

laugh.png That is indeed horrible. Way too many parenthesis to keep track of what's inside what (and some are entirely unnecessary). Try separating the function arguments onto separate lines so you can see them, and separate out some of the repeating ugly things into variables.

for (totalMatches = 1; totalMatches <= (pIt)->first->getSize(); ++totalMatches)
{
    int eventIndex = eventLog.size()-totalMatches;
    const Event *event = eventLog[eventIndex];
    if (!(pIt)->first->compare(
         (pIt)->first->getSize()-totalMatches,
         event->getType(),
         translator->translate(event->getKeyCode()),
         getTimeDiff(eventIndex)))
         break;
}

Or better yet, put the whole thing in a function that returns true at the end, and have the if statement return false instead of breaking.



#8 capn_midnight   Crossbones+   -  Reputation: 1375

Like
0Likes
Like

Posted 07 February 2014 - 05:17 AM

DRY, YAGNI, KISS, GTFO, ROFLMAO


[See some of My Projects] - [Find me on twitter tumblr G+ Github]

#9 wodinoneeye   Members   -  Reputation: 664

Like
1Likes
Like

Posted 16 February 2014 - 11:21 AM

laugh.png That is indeed horrible. Way too many parenthesis to keep track of what's inside what (and some are entirely unnecessary). Try separating the function arguments onto separate lines so you can see them, and separate out some of the repeating ugly things into variables.

for (totalMatches = 1; totalMatches <= (pIt)->first->getSize(); ++totalMatches)
{
    int eventIndex = eventLog.size()-totalMatches;
    const Event *event = eventLog[eventIndex];
    if (!(pIt)->first->compare(
         (pIt)->first->getSize()-totalMatches,
         event->getType(),
         translator->translate(event->getKeyCode()),
         getTimeDiff(eventIndex)))
         break;
}

Or better yet, put the whole thing in a function that returns true at the end, and have the if statement return false instead of breaking.

 

variables  for   (pIt)->first  and  (pIt)->first->getSize()   outside  loop  to simplify even more

 

Im trying to remember when I just started using that kind of simplification and pre-optimization automatically


--------------------------------------------Ratings are Opinion, not Fact

#10 aregee   Members   -  Reputation: 676

Like
3Likes
Like

Posted 19 February 2014 - 02:26 PM

Way too many parenthesis to keep track of what's inside what (and some are entirely unnecessary).

 

Parenthesis are never unnecessary as long as they increase clarity, in my opinion.



#11 DekuTree64   Members   -  Reputation: 953

Like
0Likes
Like

Posted 21 February 2014 - 06:47 PM

 

Way too many parenthesis to keep track of what's inside what (and some are entirely unnecessary).

 

Parenthesis are never unnecessary as long as they increase clarity, in my opinion.

 

True. Especially with math, sometimes it's better to be explicit even when order of operations already does what you want. But I was mostly talking about this, which really doesn't have any redeeming quality:

getTimeDiff( (eventLog.size()-totalMatches))


Edited by DekuTree64, 21 February 2014 - 06:49 PM.


#12 swiftcoder   Senior Moderators   -  Reputation: 9506

Like
0Likes
Like

Posted 21 February 2014 - 07:01 PM

And then there was LISP (well, Scheme, but anyway...):

(define (factorial n) (let recurse ((n n) (acc 1)) (if (zero? n) acc (recurse (sub1 n) (* n acc)))))

Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#13 slicer4ever   Crossbones+   -  Reputation: 3076

Like
0Likes
Like

Posted 21 February 2014 - 07:23 PM

 

And then there was LISP (well, Scheme, but anyway...):

(define (factorial n) (let recurse ((n n) (acc 1)) (if (zero? n) acc (recurse (sub1 n) (* n acc)))))

every time i see lisp:

 

20120914120348!Exploding-head.gif


Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#14 wodinoneeye   Members   -  Reputation: 664

Like
0Likes
Like

Posted 23 February 2014 - 02:09 AM

 

 

And then there was LISP (well, Scheme, but anyway...):

(define (factorial n) (let recurse ((n n) (acc 1)) (if (zero? n) acc (recurse (sub1 n) (* n acc)))))

 

Of course then Lisp was a contemporary of Fortran when teh programmers were just happy NOT to have to program in machine code any more....

 

I believe also that the indent also had been invented by then  (as in - its always possible to make code look crappy(er) by running it all together into a contiguous block)


--------------------------------------------Ratings are Opinion, not Fact





PARTNERS