Avoid Pre-optimization
Pre-optimization occurs when you assume a piece of code is inefficient on resources, and thus focus on fixing it, without even checking if it's a problem.
You can waste quite a bit of time on this, and it could potentially harm your design decisions. If you have any worries, profile your application.
Avoid Mico-optimization
Mico-optimization is an attempt to save resources by trying trying to strip things down to their bare bones.
An example would be declaring local variables. If we had a collision detection method, it may look something like:
boolean isCollidingWith(Entity entity) {
return this.x < entity.x && this.x + this.width > entity.x || this.x > entity.x && entity.x + entity.width > this.x || ...;
}
A developer may want to clean this up by declaring some local boolean
variables:
boolean isCollidingWith(Entity entity) {
boolean collideRight = this.x < entity.x && this.x + this.width > entity.x;
boolean collideLeft = this.x > entity.x && entity.x + entity.width > this.x;
return collideRight || collideLeft || ...;
}
If one were to say "you don't need those variables, they just waste space", that would be considered mico-optimization. Although a little space may be saved, it's negligable, and attempting to save that space may harm readability.
Suit your program's needs/requirements
You are required to write a program that calculates PI. Whoever writes the most powerful PI calculator wins a prize.
In a scenario like this, you may prefer resource efficiency over readability, conserving as much as possible. You plan on deleting this program afterwards, so you won't need to read this code ever again.
Are you going to invest hours into making this project scalable? Probably not.
You own a popular social networking site.
The rules have changed. You have millions of users constantly requesting content, while also attempting to fix any bugs currently being exploited. On top of this, you have ideas of your own you want to implement.
Performance is important, but there are many other, potentially more important, factors at hand. Readability is a must, due to your need for a team and the need to revisit code.
Make sure you understand the needs of your situation.
Learn how to properly test
Testing can be a long and painful process if you don't know how to do it right. You could spend hours fighting with tests, just to realize you didn't test a unit properly.
Unit testing is a mainstream form of testing. It focuses on checking the integrity of a single unit of code. Integration testing would be the next step: testing as a whole.
Mocking allows you to recreate situations that don't usually occur on command, such as seeing if a robot wakes up when an alarm goes off (you could mock the alarm and give it a specific "current time", rather than waiting).
Without exposing yourself to these concepts, you'll have a hard time ensuring your software's integrity.