PDA

View Full Version : Why Quick and Dirty Code Doesn't Cut It


jake13jake
12-01-2006, 02:04 AM
In the world of computer programming, employers are trying to improve programmer time efficiency. This means the time spent on creating a program is less and, in turn, sacrifices program efficiency.

Three reasons why not to write quick code:
1. Lack Expandibility
2. Lack Reusability
3. Lack Readability

When you're writing a script, always break it down into independent tasks. I'll use movement for my example.

What do you need to move?
1. direction, speed.
2. possible detection of wall. (believe me, there are other ways than onwall)
3. distance to wall.
4. changing player coordinates.
You could make independent functions for all of these, and using variable and function names that make sense, it will be much easier to understand when someone else is looking at it, or you're reviewing it.

*Always keep in mind what you can reuse in other scripts, or what variables you can manipulate through other scripts.

You could make player.dir and movement direction independent. By doing this, you could make it so you can move a player so that the player is always facing a target, or that the player is walking sideways, backwards etc.

You could make wall detection detect by tiletypes, or even with exceptions to tiletypes of a certain tile. You could make it so that you can easily change the tiletypes or exceptions. When you detect the wall you can return the distance from the player to the wall.

You could make the player's base dynamic in wall detection. Making offsetx/y, width, and height variables.

You could reuse all of this with a clientside NPC, and if you have the energy, make a version of it that would work for serverside movement.


*Always plan for the future.
Add functionality that you're going to use in the future.

For instance, the reason to detect tiletypes in a movement script is so that you can define horses not to go over chairs, etc. Or if you're feeling like playing a prank on the server, you can make it so nobody can sit down on a chair, or so that you can only walk on wall tiles.

All of this work is NOT a waste of time. It will save you hours of slaving over your keyboard just to write many other scripts that could have been done with three lines of code had you not made things so sloppy.