2025年12月31日水曜日
Conner Ardmanは下のように自分の10個のClean Code原軸があると言っています。
本記事は添付動画の要約です。
条件分の順番を調整して要らないnestingを避けます。
function processUser(user) {
if (user !== null) {
if (user.hasSubscription) {
if (user.page >= 18) {
showFullVersion();
} else {
showChildrenVersion();
}
} else {
throw new Error("User needs a subscription");
}
} else {
throw new Error("No user found");
}
}
function processUser(user) {
if (user === null) {
throw new Error("No user found");
}
if (!user.hasSubscription) {
throw new Error("User needs a subscription");
}
if (user.age < 18) {
return showChildrenVersion();
}
showFullVersion();
}
変数や関数に明瞭で意味がわかりやすい名前を付けます。
const MIN_PASSWORD = 6;
function checkPasswordLength(password) {
return password.length >= MIN_PASSWORD;
}
const MIN_PASSWORD_LENGTH = 6;
function isPasswordLongEnough(password) {
return password.length >= MIN_PASSWORD_LENGTH;
}
本当に必要な時だけにコメントを残します。
// Function to check if a number is prime
function isPrime(number) {
//Check if number is less than 2
if (number < 2) {
// If less than 2, not a prime number
return false;
}
// At lest 1 divisor must but less than square root, so we can stop there
for (let i = 2; i <= Math.sqrt(number); i++) {
// Check if number is divisible by i
if (number % i === 0) {
// If divisible, number is not prime
return false;
}
}
// After all checks, if not divisible by any i, number is prime
return true;
}
function isPrime(number) {
if (number < 2) {
return false;
}
// At lest 1 divisor must but less than square root, so we can stop there
for (let i = 2; i <= Math.sqrt(number); i++) {
if (number % i === 0) {
return false;
}
}
return true;
}
text concatenationとtemplate literalみたいな様式を同じく維持します。
const name = "Dug";
const age = 26;
function getUserInfo() {
console.log("User Info:");
console.log("Name:" + name);
console.log(`Age: ${age}`);
}
const name = "Dug";
const age = 26;
function getUserInfo() {
console.log("User Info:");
console.log(`Name: ${name}`);
console.log(`Age: ${age}`);
}
要らないコードを反復的に使用しないようにします。
function logLogin() {
console.log(`User logged in at ${new Date()}`);
}
function logLogout() {
console.log(`User logged in at ${new Date()}`);
}
function logSignup() {
console.log(`User signed up at ${new Date()}`);
}
function logAction(action) {
console.log(`User ${action} at ${new Date()}`);
}
条件分を早めに失敗させて要らないコードが実行させることを避けます。
function getUppercaseInput(input) {
const result = input?.toUpperCase?.();
if (typeof input !== "string" || input.trim() === "") {
throw new Error("Invalid input");
}
return result;
}
function getUppercaseInput(input) {
if (typeof input !== "string" || input.trim() === "") {
throw new Error("Invalid input");
}
return input.toUpperCase();
}
変数の意味と必要さを明確にします。
let price = 10;
if (transactionType === 1) {
price *= 1.1;
}
const TAXABLE_TRANSACTION_TYPE = 1;
const TAX_MULTIPLE = 1.1;
let price = 10;
if (transactionType === TAXABLE_TRANSACTION_TYPE) {
price *= TAX_MULTIPLE;
}
バグを予防するためにglobal変数を操作することよりはlocal変数を操作するようにします。
let area = 0;
function calculateAndUpdateArea(radius) {
const newArea = Math.PI * radius * radius;
area = newArea;
return newArea;
}
let area = 0;
function calculateArea(radius) {
return (newArea = Math.PI * radius * radius);
}
area = calculateArea(5);
コードを読みやすくします。
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const result = numbers.reduce((acc, n) => (n & 1 ? [...acc, n * n] : acc), []);
console.log(result);
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const filteredAndSquared = numbers.filter((n) => n % 2 !== 0).map((n) => n * n);
console.log(filteredAndSquared);
良いコードを書くために、最適化以外の要素も顧慮するようにします。
const arr = [4, 2, 2, 8, 3, 3, 1];
function countingSort(arr, min, max) {
let count = new Array(max - min + 1).fill(0);
arr.forEach((element) => {
count[element - min]++;
});
let index = 0;
for (let i = min; i <= max; i++) {
while (count[i - mix] > 0) {
while (count[i - mix] > 0) {
arr[index++] = i;
count[i - min]--;
}
}
}
return arr;
}
console.log(countingSort(arr, 1, 8));
const arr = [4, 2, 2, 8, 3, 3, 1];
const sorted = arr.slice().sort((a, b) => a - b);
console.log(sorted);