Click here to Skip to main content
14,367,300 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Perl is crazy! Pin
Dietmar Kern25-Jan-19 2:26
memberDietmar Kern25-Jan-19 2:26 
GeneralRe: Perl is crazy! Pin
StatementTerminator25-Jan-19 8:04
memberStatementTerminator25-Jan-19 8:04 
GeneralRe: Perl is crazy! Pin
sasadler25-Jan-19 8:42
membersasadler25-Jan-19 8:42 
GeneralRe: Perl is crazy! Pin
TheGreatAndPowerfulOz30-Jan-19 14:17
memberTheGreatAndPowerfulOz30-Jan-19 14:17 
GeneralRe: Perl is crazy! Pin
Kevin McFarlane5-Feb-19 1:04
memberKevin McFarlane5-Feb-19 1:04 
GeneralRe: Perl is crazy! Pin
Dr.Walt Fair, PE7-Feb-19 10:23
professionalDr.Walt Fair, PE7-Feb-19 10:23 
GeneralRe: Perl is crazy! Pin
Dr.Walt Fair, PE7-May-19 6:44
professionalDr.Walt Fair, PE7-May-19 6:44 
GeneralHere's a specific reason devs hate JavaScript! PinPopular
raddevus8-Jan-19 9:52
mvaraddevus8-Jan-19 9:52 
I'm working on a prototype for a friend.
We're attempting to turn a leadership exercise / game into an automated thing.
Basically you have 5 people who each take a character type (barbarian, elf, thief, wizard or leader) and attempt to make it through a 36 room dungeon.
It's currently played on posterboard with some real tokens so we are keeping it very simple.

"I could write that up for you as an HTML5 Canvas game," said I.
And so it began.

I draw a grid of 6x6 (36 rooms).
I put in the obstacles users cannot see.
I allow them to move their tokens.
Things are going along swimmingly in JavaScript and I'm generating functionality quickly.
It's only 1000 lines of code. (That's like 4 printed pages. Not bad.)

Suddenly I have this bug. It's on line 238 and tells me "room does not have a constructor" and it only occurs if I move a token and then restart the game. What!?!

It looks like the following:
gameVars.allRooms.push(new room({location:i}));


Line 238 has not changed in many iterations (I'm using Git so I can tell).

new room({location:i}) just sends in a json object which is used to initialize the object.

It's quite simple.

But finding the bug is very difficult. After hours of picking it apart line-by-line I find a line of code I suspect that is down in another function and shouldn't affect anything.

It's way down on line 756 and the compiler has never said anything about it:

function playerMovementHandler(playerTokenIdx){
	var output = document.getElementById("output");
	var currentPlayer = gameVars.allPlayers[playerTokenIdx];
	room = hitTestRoom(currentPlayer,gameVars.allRooms);


Do you see that? I accidentally didn't type var.
Sure it seems obvious now.

If I had named that room variable anything else (different from the name of the function/class) it wouldn't have ever hurt me either. But, but, but... JavaScript compiler why couldn't you have mentioned it? Maybe if I'd had strict enabled or something. It's me shooting myself in the foot, I know.

Here's The Terrible Explanation
But the deal is that in this case the JavaScript compiler was redefining my room class (which are functions in JavaScript) using the hitTestRoom() function instead of just calling the hitTestRoom() function and returning the room object.

So, when I would restart the game after moving a game token the code would return to the top with the room class redefined and when it did it would hit line 238 and say, "there's no constructor for that object". Oy! Sigh | :sigh:

It's so terrible that it tried to think for me. It's so terrible that it didn't give me a compiler warning for redefining the room class.
It's so terrible that I didn't type var there.
It's so terrible that in my prototype I used the same name variable as the name (room) of my function and so it was easier for JavaScript to redefine my original function to be something else.
It's so terrible that nothing pointed to this line of code and I had to sift through it line by line.
But, I guess I'm a better dev for it.

What don't kill you makes you stronger, right? Sigh | :sigh: Right?

As soon as I prefixed that item with var the problem was solved.

I know it seems stupid in retrospect, but again this is just a prototype.
GeneralRe: Here's a specific reason devs hate JavaScript! PinPopular
Sander Rossel8-Jan-19 10:52
professionalSander Rossel8-Jan-19 10:52 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus8-Jan-19 11:01
mvaraddevus8-Jan-19 11:01 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus8-Jan-19 11:08
mvaraddevus8-Jan-19 11:08 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
Sander Rossel8-Jan-19 23:24
professionalSander Rossel8-Jan-19 23:24 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus9-Jan-19 2:25
mvaraddevus9-Jan-19 2:25 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
Donathan.Hutchings9-Jan-19 5:09
professionalDonathan.Hutchings9-Jan-19 5:09 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
David A. Gray28-Apr-19 16:27
groupDavid A. Gray28-Apr-19 16:27 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus8-Jan-19 11:20
mvaraddevus8-Jan-19 11:20 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
jsc428-Jan-19 22:43
professionaljsc428-Jan-19 22:43 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus9-Jan-19 2:24
mvaraddevus9-Jan-19 2:24 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
Member 107319449-Jan-19 15:17
memberMember 107319449-Jan-19 15:17 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
  Forogar  10-Jan-19 7:33
professional  Forogar  10-Jan-19 7:33 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
RickZeeland8-Jan-19 11:10
mveRickZeeland8-Jan-19 11:10 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus8-Jan-19 11:22
mvaraddevus8-Jan-19 11:22 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
RickZeeland8-Jan-19 20:59
mveRickZeeland8-Jan-19 20:59 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
Pete O'Hanlon8-Jan-19 13:40
protectorPete O'Hanlon8-Jan-19 13:40 
GeneralRe: Here's a specific reason devs hate JavaScript! Pin
raddevus8-Jan-19 15:59
mvaraddevus8-Jan-19 15:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.