Search This Blog

Friday, March 17, 2017

Coding Tips & Tricks

Coding Tips & Tricks? Yes. Hopefully, these will help you keep things organized, and find bugs.


  • Comments. Comments are your best friend. They can help you refine or edit your code, as well as help others edit or refine your code. It is also helpful to someone who is reviewing your code. As you can see in this example, I have placed comments on nearly every line. The lines marked as "Wall 1" or "Wall 2" may seem unclear, but I understand what they mean, and if you have run this yourself you will know that it builds the walls in that order with a short time delay between, so all you have to do is count the walls as it builds them, to see which line builds which wall.
 
this example is a library program I wrote to keep track of all my books, and as you can see there are very few comments. I have put headings, but not comments on each line, and I can see many, many, many places where I should have put comments when I wrote this program.


  • Another thing you can do to help keep your code organized is leave blank spaces between parts of your code.

As you can see from this image, I have drawn lines around "blocks" of code. These are totally subjective. This is just the way I like to lay out my code, but you should change that to fit your needs.

  • The rubber duck method. Yes, this is a real thing. The rubber duck method involves using a rubber duck, toy, stuffed animal, or sibling/parent, to explain your code to. You just explain the code, line by line, to your rubber duck, and this will hopefully help you catch bugs. Now using a rubber duck is ideal, because when you find a bug and say "HOW COULD I NOT SEE THAT?!?!?!" And get mad, and want to throw something (no you are not alone in this feeling) you can throw the rubber duck, while your sibling/parent might object to being thrown. Please pardon the following pun.
Feel free to ask questions, suggest a post, or suggest something else to add to this post!




No comments:

Post a Comment