"Premature optimization is the root of all evil" - Donald Knuth
You're writing code to solve a real world problem. Getting to a solution faster is more important than a fast solution.
- Macro performance (Design level) - system wide decisions ex. sql vs nosql
- Micro performance (Fine tuning) - modules, functions...
- Have a real performance problem.
- Make 80% moves (what change leads to an 80% reduction? usually data structures).
- Use a profiler to fix hot spots.
- Get under the hood (meomry).
function calculate(bottom, top) {
// One
if (top > bottom) {
// Two
let sum = 0;
for (let number = bottom; number <= top; number++) {
// Three
if (number % 2 == 0) {
// Four
sum += number;
}
}
return sum;
} else {
return 0;
}
}
- Extraction - Move code into a separate function
- Inversion - Early termination of conditions
// Extraction i.e. Separate function
function filterNumber(number) {
// One
if (number % 2 == 0) {
// Two
return number;
}
return 0;
}
function calculate(bottom, top) {
// One
// Inversion i.e. Early termination
if (top < bottom) return 0;
let sum = 0;
for (let number = bottom; number <= top; number++) {
// Two
sum += filterNumber(number);
}
return sum;
}
function relScore(m1, m2) {}
function movieRelationScore(movie1, movie2) {}
# big-endian = Most significant word FIRST
profitMonthlyMax
# little-endian = Most significant word LAST
maxMonthlyProfit
let x = 1;
let someValue = 1;
bool bIsValid;
int32_t iSpeed;
uint32_t uNumUsers;
char szUserName;
function execute(delay) {}
function execute(delaySeconds) {}
// utils.js
function assignDefaults() {}
function relationScore() {}
function moviesOnPage() {}
function topMoviesByRating() {}
function moviesByDirector() {}
function parseCookie() {}
function assignCookie() {}
// movies.js
function assignDefaults() {}
function relationScore() {}
// pager.js
function moviesOnPage() {}
// movieCollection.js
function topMoviesByRating() {}
function moviesByDirector() {}
// cookie.js
function parseCookie() {}
function assignCookie() {}
code > comments
Comments get bugs like code. When code is updated, usually the comment is not.
Use comments only when there is some non-obvious code optimization, explaining why the code is weird, or if it references some specific math or algorithm.
Comments = How code works
Documentation = How code is used
// A status of 5 signals message sent
if (status == 5) {
message.markSent();
}
CONST MESSAGE_SENT = 5
if (status == MESSAGE_SENT) {
message.markSent();
}
/*
You can update a message IF the current user is the author of the message and the message was delivered less 5 minutes ago OR if the current user is an administrator. You can also edit the message if the message wasn't delivered yer.
*/
if ((message.user.id == currentUser.id && (!message.deliveredTime() || datetime.now() - message.deliveredTime() < 300)) || currentUser.type == "administrator") {
message.updateText(text);
}
const FIVE_MINUTES = 5 * 60;
let userIsAuthor = message.user.id == currentUser.id;
let isRecent = !message.deliveredTime() || datetime.now() - message.deliveredTime() < FIVE_MINUTES;
let userIsAdmin = currentUser.type == "administrator";
if ((userIsAuthor && isRecent) || userIsAdmin) {
message.updateText(text);
}
function canEditMessage(currentUser, message) {
const FIVE_MINUTES = 5 * 60;
let userIsAuthor = message.user.id == currentUser.id;
let isRecent = !message.deliveredTime() || datetime.now() - message.deliveredTime() < FIVE_MINUTES;
let userIsAdmin = currentUser.type == "administrator";
return (userIsAuthor && isRecent) || userIsAdmin;
}
if (canEditMessage(currentUser, message)) {
message.updateText(text);
}