Monday, 27 October 2014

How Not to Debug, Part I

In my past blog posts, I discussed tips and tricks on debugging better and different practices for debugging code. However, I never really get into how not to debug. So today, I'll be looking at ways not to debug, so that you may be a better debugger.

Ergonomics & mindset.

When it comes to working at a computer, ergonomics is always critical. Because debugging can be a hair-pulling task, it is best to remain comfortable with adequate lighting levels. The screen shouldn't be to bright, and the space in which you're working should provide adequate lighting. Programs like Flux can really help with ensuring your eyes do not get strained from lighting. A comfy chair that is adjustable, cushioned, and comfortable, along with good desk space will allow the programmer to not feel cramped and help reduce headaches and stress. So when debugging, if you're not following at least some basic ergonomic rules, you could be artificially stressing your body out.

Debugging is not just a task, it's a mindset. If you're not thinking logically about your problem and instead pulling your hair out, screaming internally about why the $#^% that particular part of your program won't work, you could be debugging wrong. Panicking is exactly what you should do if you want to not debug correctly. In fact, here is a list of things not to do from a mental standpoint

  • Panic (probably the worst thing you can short of going postal).
  • Don't take breaks, don't drink water, don't talk about the problem to anyone, don't do anything to give your mind a break!
  • Berate yourself verbally (calling yourself an idiot, for example. We all make dumb mistakes)
  • Physically harm yourself (head desk, hair pulling, hitting yourself on the head. Yes, I know someone who smacks their head when debugging and he gets a headache every time). 
  • Point fingers at co-workers, peers, etc. This will only make things worse.
  • Get angry at the problem
Sometimes a little stress is good as it can motivate without causing any actual harm. However, if you let stress rule you, you won't be a good debugger, just a stressed out programmer. 

Shotgunning and hacking in code

A term I first heard from one of my TA's, shotgunning code means when you just write code into your program and hope it works, without actually looking into the problem. Sometimes, this approach will work, especially for smaller programs, but you won't figure out why and you certainly won't learn anything. The problem with code shotgunning is that you can spend hours just spraying if statements here and there, tweaking little values, adding in new junk, and by the end of it even if you got it working you probably won't understand how you got it to work. 

Code hacking can be just as bad, if not worse, because it turns shotgunning code into a bad habit. Sometimes programmers will encounter a problem and discover a fix but not understand how it fixes it but use it anyways. That isn't learning how to debug, that is having a bag of trick to fix niche issues you don't truly understand. 

Comments, documentation, and code structure

If you want to be a bad debugger, do these:
  • Don't comment, or not often enough.
  • Have weird variable names.
  • Spell words incorrectly.
  • No documentation or poorly written documentation
  • Don't plan. At all.
  • Don't have any flow charts, diagrams, or any indication of how your program is structured. 
If you don't do these, you're not helping yourself or anyone who could be looking at your code. Commenting code is so critical to writing code that will help you fix problems it isn't funny. How many times have you written some code into the wee hours of the night, didn't comment, then came back and looked at it and you had to figure out what it did, just to jolt your memory. If that sounds familiar, then try commenting more with additional clarity. Comments are meant to help someone understand the who, what, where, why, when, and how of code so programmers can understand it easier.

Good advice, but you don't HAVE to follow this exact method
Weird variable names and proper spelling is quite obvious. If your variable names are spelt wrong, programmers will have difficulty finding it. Weird variable names simply means if you're supposed to have a bool that checks if the player goes first during a game of Tic Tac Toe, call that bool isPlayerGoingFirst, not playerMove, or isPayerGng1st, because that will down right confuse people.

I don't think I can stress how important documentation is to code, more so third party libraries or a tool you made for a group project. SFML is an example of good documentation. It has lots of comments and describes how things work in short, brief statements. Nothing bad about longer explanations, but this works in a crisp and concise way. If you are going to code a menu creation tool, or a sophisticated object loader, comment about it and describe how it functions. You'll be happier for those comments if it has a problem.

Next step, plan. Seriously, plan your code. Buy a white board, markers, and plan a flow chart for your code. Don't know how to make one? Google it. Having a logical flow chart of how your program should go from point A to B to C and so on is stupidly important that if you don't do it, then it becomes harder to understand how your program works in the bigger picture of things. 

This isn't hard to do.

Conclusion

It is possible to write an entire book on how not to debug, but this is just a taste of how not to debug. Next time, I'll be discussing about how and how not to test your code by using actual examples, more in depth into how not to code, and some good practices I've learned over the years of coding. 




Sunday, 19 October 2014

Star Citizen and Bug Smashers, Part II

Last week I discussed tools that were used by a Cloud Imperium Games Corporation to help debug code for their game. Cloud Imperium's YouTube series, Around the Verse, gives people an inside look into the company, and even a look at how the debug their code in a segment called Bug Smashers, hosted by Mark Abent, a gameplay programmer at Cloud Imperium. This week, I'd like to discuss debugging techniques used from two episodes and juxtapose them with techniques learned in class. I will be focusing more on the techniques and thought processes used to solve the bugs instead of the tools used.

Bug #1: Lens Flare methodology

In Around the Verse, episode 15, the focus of Bug Smashers was on a bug with the flare for engines not appearing. Before hoping in the code, he at first begins replicates the problem both in the game and the engine test scene. Stating the problem, lens flare archetype does not work in the game or engine, and understanding how it happens, he begins to do some fact gathering by understanding it only happens with the ship engines. Double checking the database tool Mark has available, he checks to see if the flare works as an archetype, and sees that it doesn't work. Next he checks it with a normal light entity and sees that it does in fact work.

He enumerates and narrows down the possibilities by looking at the entity initialization, and because Lua deals with the lighting logic, he was able to determine a likely spot where the problem may exist. After narrowing it down to a likely spot, he sets breakpoints to help determine the problem and discovered the properties for the newly created archetype does not set the property tables at all. Writing two lines of code to make sure it initialized correctly, he tested to see if that fixed the problem. The test was a success and the lens flare works in the engine test scene, but it failed to spawn in the game.

Fixing the lens flare in the game test mode was dealt with in a similar manner. Knowing the problem, Mark checked the database to see if there were any anomalies. Seeing right away that a material was not being set for the lens flare, he set the right path and gave it a test in game and found it worked. With the lens flare working both in the game and the engine, the bug was fixed. 

Bug #2: Texture error methodology

Video games cannot display high resolution textures all the time and will instead draw out low resolution textures when higher quality is not needed. In Around the Verse episode 5, a bug was causing the high and low resolution textures of one of their space ship models to constantly flip, causing frame rate drops and very noticeable texture flickering.

Initially, this bug was looked at by one of the graphics engineers who discovered that the level of detail (LOD) calculations were being done twice. The program would calculate the LOD once, then use the same values to calculate them again, causing the switching. The changes incurred by the calculations were causing a floating point error where the affected variables would change so minutely, between 1 and 0.

Trying to replicate the problem, Mark noticed that the bug was only present on static models. So if you have a flying ship, it would flip between high and low textures properly. A static ship statue however, would flip textures. Next, he enumerated the possibilities by looking at all of the related segments of the LOD code and then began to use breakpoints to see where discrepancy happened and eliminate sections where it could not have happened. After finding a segment where the LOD's rapidly change, he checks the call stack to see where the LOD is calculated. Working with the graphics engineer, they were both able to determine the location of the problem and ensure the LOD calculations were using the correct calculations. Once the LOD's were changed to calculate correctly, a test was done in the engine test scene and the game to determine if it was fixed. The test was successful.

Comparison to methods taught in class and key takeaways. 

In both bugs, Mark follows a clear method for uncovering and solving bugs that follows a similar methodology of the Sherlock Holmes method in the class textbook, Debugging By Thinking. Here is a list of methods that I picked up from the Bug Smashers portion of Around the Verse:
  • Define the problem clearly.
  • Gather facts about the bug.
  • State a hypothesis
  • Enumerate and eliminate possible bug locations.
  • Make a change somewhere.
  • Run and test all cases.
  • If failed, go back to step 2.
  • Remain calm during all of the above.
When debugging problems, it boils down to your tools and technique. It is important to remember that it is not what you think, but how you think. What I find awesome about the Bug Smashers series is the clear methodology used to identify and solve problems. Reminiscent of the Sherlock Holmes method of thinking, the way of thinking presented follows a clear logical path that can used to test any bug. Software defects are not solved by shotgunning code, guessing, or trying to hack in some cheap fix, but by a set methodology that makes it easier and less stressful. 

In class, we are taught about how different debugging tools are used to maximum effect. Bug Smashers also demonstrates the importance and effectiveness of tools when combined with a methodology for identifying and squashing bugs. Tools should be easy to navigate, visually simple, and provide ample information about what you are currently working on without being to invasive into your program. 

I took away three key concepts from Bug Smashers. One, you should have a logical methodology that helps you break down a problem, identify it, and not only solve it but understand why it was broken and why what you did fixed it. Two, your methodology should complement the tool you are using, which for me is the key take away from that portion of Bug Smashers. Three, don't panic. Not once did I see a sing of panic, stress, pull hair, yell, curse, or anything negative, during Bug Smashers. It is best to be in a calm and comfortable state of mind when debugging big problems.

Conclusion

Bug Smashers is a very fun an interesting segment of Cloud Imperium's YouTube series as it gives you an inside look of what it is like to debug for a game in development. There is a lot to learn from the series as it provides the viewer with some real case scenarios of what can go wrong during game development, how bugs can affect the game, and how to go about fixing them. Thank you for reading, I hope you enjoyed it! 


Saturday, 11 October 2014

Star Citizen and Bug Smashers, Part 1

After a long bout of writers blog and researching some new debugging techniques, I finally decided what to write my next blog on. Rather than discussing a debugging technique, or a particular tool, today I thought it would be more useful and interesting to dive right into how industry professionals go about debugging their games and how we can learn from it. So this blog will be split into segments, the first will be a tool analysis, the second a technique analysis, of the debugging methods used by Cloud Imperium Games Corporation, the developers of Star Citizen.

Star Citizen and Around the Verse

Star Citizen is an upcoming space trading and combat game developed using the CryEngine. The game is truly massive in scope, featuring first person space combat, first person shooting, trading, building of ships, on top of that a persistent multiplayer universe with a galactic economy, NPC's, and more. The game is huge and debugging is an even bigger task. Every week, the developers give players an inside look into the game with a YouTube series called 'Around the Verse'. An interesting segment of the 40 minute YouTube series is called 'Bug Smashers', where one of the gameplay programmers, Mark Abent, will answer fan questions or debug actual problems in the game. What makes the segment so awesome is the combination of tools, techniques, and experience, that are used to, well, smash bugs.

Bug Smashing in Bug Smashers

What makes Bug Smashers such a fun segment is how well it connects to not only in class lectures, but the class textbook, Debugging By Thinking. In episode 15 of Around the Verse, a bug with the engine flare not occurring during ship engine boost is the focus of this weeks Bug Smashers. Upon a quick test in the games debug mode, we get a visual representation of the problem. It's quite simple, no lens flair from an archetype entity. Note: an archetype entity is based off a regular entity that specifies individual parameter values for that Entity. Very quickly, we get a peak at some of the cool tools necessary to debug problems like this.

The tools

First thing to note was the ability to hop from a test mode to an editor that feels a lot like a Maya scene quickly. Very easily was he able to test flying the ship, to a debug test scene. Next was that we get a look at a menu that displays a database that allows the programmers to easily keep track of 'archtypes'. This data base keeps track of all kinds of data, whether it be light information, entities to use, etc. So if some particular light gets set up in a certain way, the database will hold onto that for easy use to grab later on.

Next, was that this database was used to grab a lens flare entity that should glow orange. It was as simple as a few clicks to grab it and place it in a test scene. What is important to note is that because this is a database, any other programmer focusing on this problem can simply open up the scene and grab that same lens flare entity and begin testing. While that is awesome, there is more to it.

Thirdly, was when he was attempting to solve the bug, the game uses Lua scripting for lights and Visual Studios for compiling code. Every time the code would compile, he would reset the editor, hop back in the level, and go right back to testing.

Exploring the bug

Over the course of the video, Mark discovers a series of problems which he is able to quickly and efficiently narrow down because of the tools at his disposal. After discovering the Archetype was not receiving the proper information, he double checked the code and made sure that the Archetype was getting the proper initialization that it needed, and reran the scene. After recompiling the code, he discovered that the lens flare was working in the editor! Next, after testing it in a debug test scene of the game, he discovered it didn't work. Using the database editor, he was able to identify that the correct material wasn't being set correctly, which was a simple as inputting the proper file path for that material.

Why the tools are so critical

After watching the video, it was abundantly clear why these tools were so useful to the development of the game. The database was easy to use and visually appealing, with ample features. Essentially a list of all kinds of information, programmers would be able to easily create new entities or archetypes that are saved in the database, view critical information, change values on the fly (for example, brightness, color, direction), spawn copies, see material paths, and more.

From a debugging point of view, when running into problems, it is easier, faster, and less stressful, to narrow down and test problems that would otherwise take forever to debug. This can be used to quickly replicate and analysis potential bugs without a steep learning curve of the tools. The ability to quickly change entities and test them right away in a test scene is a very powerful feature. It cuts down a lot of waiting time and gives programmers the power to debug their code on the spot. When waiting time and stress is reduced, everyone wins.

What is so important about these tools is that they are organized, visually appealing, easy to use, and informative. When a tool has these properties, they can help in so many different ways that it will really help the team solve problems much quicker. When tools are lacking in some way, they can be stress inducing, cost the company more time and money, and possibly even prevent some bugs from being exposed, which is never good.

What I take back from this

When developing my own tools for games in the future, a database is something I would love to create. For my Capstone project, I want to create a database to view and edit story instances of the game, so that it is easy to not only set data for them, but to connect them all together. While the test scene might be a bit to much beyond my scope and not fully necessary, a database of all my information in an easy to access and visual way would be exactly what I could use to speed up my production. While it may take a lot of time to make a database editor for the project, the amount of time, and stress, it would save over the long run would surely make up for it.

Next week I'll be discussing the thought process and methodology used to narrow down the bug, as well as its in class application, how it relates to portions of the textbook, and how the tools made all of that possible. Thank you for reading, hope you enjoyed it.