1. Another Creative Way to Simplify Expressions
void AddStats(const Stats& add_from, Stats* add_to) {
add_to->set_total_memory(add_from.total_memory() + add_to->total_memory());
add_to->set_free_memory(add_from.free_memory() + add_to->free_memory());
add_to->set_swap_memory(add_from.swap_memory() + add_to->swap_memory());
add_to->set_status_string(add_from.status_string() + add_to->status_string());
add_to->set_num_processes(add_from.num_processes() + add_to->num_processes());
...
}
After ten seconds of careful scrutiny, you might realize that each line is doing the same thing,
just to a different field each time:
add_to->set_XXX(add_from.XXX() + add_to->XXX())
In C++, we can define a macro to implement this:
void AddStats(const Stats& add_from, Stats* add_to) {
#define ADD_FIELD(field) add_to->set_##field(add_from.field() + add_to->field())
ADD_FIELD(total_memory);
ADD_FIELD(free_memory);
ADD_FIELD(swap_memory);
ADD_FIELD(status_string);
ADD_FIELD(num_processes);
...
#undef ADD_FIELD
}
2. Breaking Down Giant Statements
var update_highlight = function (message_num) {
if ($("#vote_value" + message_num).html() === "Up") {
$("#thumbs_up" + message_num).addClass("highlighted");
$("#thumbs_down" + message_num).removeClass("highlighted");
} else if ($("#vote_value" + message_num).html() === "Down") {
$("#thumbs_up" + message_num).removeClass("highlighted");
$("#thumbs_down" + message_num).addClass("highlighted");
} else {
$("#thumbs_up" + message_num).removeClass("highighted");
$("#thumbs_down" + message_num).removeClass("highlighted");
}
};
//his is also an instance of the DRY—Don’t Repeat Yourself—principle):
var update_highlight = function (message_num) {
var thumbs_up = $("#thumbs_up" + message_num);
var thumbs_down = $("#thumbs_down" + message_num);
var vote_value = $("#vote_value" + message_num).html();
var hi = "highlighted";
if (vote_value === "Up") {
thumbs_up.addClass(hi);
thumbs_down.removeClass(hi);
} else if (vote_value === "Down") {
thumbs_up.removeClass(hi);
thumbs_down.addClass(hi);
} else {
thumbs_up.removeClass(hi);
thumbs_down.removeClass(hi);
}
};
3. Explaining Variables
Here is an example:
if line.split(':')[0].strip() == "root":
...
Here is the same code, now with an explaining variable:
username = line.split(':')[0].strip()
if username == "root":
4. Summary Variables
var CleanReply = function (request, reply)
{
//For example, consider the expressions in this code:
if (request.user.id == document.owner_id) {
// user can edit this document...
}
if (request.user.id != document.owner_id) {
// document is read-only...
}
//stated more clearly by adding a summary variable:
var user_owns_document = (request.user.id == document.owner_id);
if (user_owns_document) {
// user can edit this document...
}
if (!user_owns_document) {
// document is read-only...
}
}
5.Using De Morgan’s Laws (positive is easy to readable)
var Contains = function (request, reply)
{
/* 1) not (a or b or c) ⇔ (not a) and (not b) and (not c)
2) not (a and b and c) ⇔ (not a) or (not b) or (not c)*/
//You can sometimes use these laws to make a boolean expression more readable. For instance,
//if your code is:
if (!(file_exists && !is_protected)) Error("Sorry, could not read file.");
//It can be rewritten to:
if (!file_exists || is_protected) Error("Sorry, could not read file.");
}