JavaScript 코딩 표준
JavaScript 표준
섹션 제목: “JavaScript 표준”XOOPS는 가독성과 유지 관리성에 중점을 두고 최신 JavaScript 표준(ES6+)을 따릅니다.
XOOPS JavaScript 표준은 다음을 기반으로 합니다.
- ECMAScript 2015+(ES6 및 최신 기능)
- 에어비앤비 JavaScript 스타일 가이드 (개정판)
- 일관성을 위한 XOOPS 규칙
- 접근성 표준(WCAG)
파일 구조
섹션 제목: “파일 구조”파일 정리
섹션 제목: “파일 정리”// 1. File header comment/** * XOOPS Module - Feature Name * @file Handles user interactions on the dashboard * @author Your Name <email@example.com> * @copyright 2026 XOOPS Project * @license GPL-2.0-or-later */
// 2. Importsimport { Helper } from './helpers.js';import { API } from './api.js';
// 3. Constantsconst DEFAULT_TIMEOUT = 5000;const API_ENDPOINT = '/api/v1';
// 4. Module setupconst Dashboard = {};
// 5. Private functionsfunction initializeUI() { // ...}
// 6. Public methodsDashboard.init = function () { // ...};
// 7. Exportsexport default Dashboard;파일 이름 지정
섹션 제목: “파일 이름 지정”// Use lowercase with hyphensdashboard.jsuser-profile.jsform-validator.jsapi-client.js
// React components (PascalCase)UserProfile.jsxFormValidator.jsxDashboard.jsx변수 및 상수
섹션 제목: “변수 및 상수”변수 선언
섹션 제목: “변수 선언”// Use const by defaultconst maxRetries = 3;const userName = 'John';
// Use let for variables that changelet currentIndex = 0;
// Avoid var (legacy)// ❌ var oldStyle = true;
// Const objects and arrays can have contents modifiedconst user = { name: 'John' };user.name = 'Jane'; // ✅ OKuser = {}; // ❌ Error
const numbers = [1, 2, 3];numbers.push(4); // ✅ OKnumbers = []; // ❌ Error변수 이름 지정
섹션 제목: “변수 이름 지정”// Use descriptive namesconst userName = 'John'; // ✅const un = 'John'; // ❌
// Boolean variables should indicate stateconst isActive = true; // ✅const hasPermission = false; // ✅const canEdit = true; // ✅const active = true; // ❌ Unclear
// Arrays should use plural namesconst users = ['John', 'Jane'];const userList = ['John', 'Jane'];const items = [];// UPPER_SNAKE_CASE for module-level constantsconst API_TIMEOUT = 5000;const MAX_RETRIES = 3;const DEFAULT_PAGE_SIZE = 10;
// camelCase for object properties (even constants)const config = { apiTimeout: 5000, maxRetries: 3, defaultPageSize: 10,};함수 선언
섹션 제목: “함수 선언”// Named functions (preferred for reusability)function validateEmail(email) { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);}
// Arrow functions (preferred for callbacks)const validateEmail = (email) => { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);};
// Short arrow functionsconst isPositive = (num) => num > 0;const double = (x) => x * 2;
// Avoid anonymous function expressions// ❌ const fn = function() {};함수 이름 지정
섹션 제목: “함수 이름 지정”// Use descriptive verb-based namesfunction getUserById(id) { } // ✅ Describes what it getsfunction validateUserInput(data) { } // ✅ Describes actionfunction formatDate(date) { } // ✅ Describes transformation
// Avoid single letters except in obvious cases (loops)function f(x) { } // ❌function fetch() { } // ❌ Conflicts with global함수 매개변수
섹션 제목: “함수 매개변수”// Use clear parameter namesfunction addUser(name, email, role = 'user') { // ...}
// Use destructuring for objectsfunction createPost({ title, content, author, published = false }) { // ...}
// Document complex functions/** * Fetch user data from the API * @param {number} userId - The user ID to fetch * @param {Object} options - Optional settings * @param {boolean} options.includeProfile - Include profile data * @returns {Promise<Object>} User data object */async function fetchUser(userId, options = {}) { const { includeProfile = false } = options; // ...}클래스와 객체
섹션 제목: “클래스와 객체”클래스 정의
섹션 제목: “클래스 정의”/** * Represents a user in the system */class User { constructor(name, email) { this.name = name; this.email = email; this.id = null; }
/** * Get user's display name * @returns {string} */ getDisplayName() { return this.name.trim(); }
/** * Validate user email * @returns {boolean} */ isValidEmail() { return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(this.email); }}
// Usageconst user = new User('John Doe', 'john@example.com');console.log(user.getDisplayName());객체 리터럴
섹션 제목: “객체 리터럴”// Use object shorthandconst name = 'John';const age = 30;
// Shorthand properties (ES6)const person = { name, age, getInfo() { return `${this.name} is ${this.age} years old`; },};
// Without shorthand (avoid)// const person = {// name: name,// age: age,// getInfo: function() { }// };서식 지정
섹션 제목: “서식 지정”간격 및 들여쓰기
섹션 제목: “간격 및 들여쓰기”// Use 2 spaces for indentation (or 4, be consistent)function example() { if (true) { console.log('Indented'); }}
// Spaces around operatorsconst x = 5 + 3; // ✅const y = 5+3; // ❌const z = isDone ? 'yes' : 'no'; // ✅
// No space inside parenthesesif (condition) { } // ✅if ( condition ) { } // ❌
// Space before function bracesfunction test() { } // ✅function test(){ } // ❌줄 길이
섹션 제목: “줄 길이”// Maximum 100 characters per line (or 120)// Break long lines logically
// Long stringsconst message = 'This is a very long message that ' + 'continues on the next line';
// Long function callsconst result = myFunction( parameter1, parameter2, parameter3);
// Long conditionalsif (condition1 && condition2 && condition3) { // ...}세미콜론
섹션 제목: “세미콜론”// Use semicolonsconst x = 5; // ✅const y = 10;
// Not using semicolons (ASI - Automatic Semicolon Insertion)const x = 5 // ❌ Avoid relying on this문자열
섹션 제목: “문자열”문자열 리터럴
섹션 제목: “문자열 리터럴”// Use single quotes for consistencyconst name = 'John'; // ✅
// Or double quotes - just be consistentconst name = "John";
// Use backticks for template literals (interpolation)const greeting = `Hello, ${name}!`; // ✅
// Avoid concatenationconst message = 'Hello ' + name; // ❌const message = `Hello ${name}`; // ✅
// Multi-line stringsconst html = ` <div> <h1>${title}</h1> <p>${content}</p> </div>`;배열 방법
섹션 제목: “배열 방법”// Prefer modern array methodsconst numbers = [1, 2, 3, 4, 5];
// Mapconst doubled = numbers.map(n => n * 2); // ✅// for (let i = 0; i < numbers.length; i++) { } // ❌
// Filterconst evens = numbers.filter(n => n % 2 === 0); // ✅
// Reduceconst sum = numbers.reduce((acc, n) => acc + n, 0); // ✅
// Findconst first = numbers.find(n => n > 3); // ✅
// Some/Everyconst hasEven = numbers.some(n => n % 2 === 0); // ✅const allPositive = numbers.every(n => n > 0); // ✅배열 파괴
섹션 제목: “배열 파괴”// Extract array elementsconst [first, second, ...rest] = [1, 2, 3, 4, 5];// first = 1, second = 2, rest = [3, 4, 5]
// Skip elementsconst [,, third] = [1, 2, 3];// third = 3
// Use in function parametersfunction processItems([first, second]) { console.log(first, second);}객체 파괴
섹션 제목: “객체 파괴”// Extract object propertiesconst user = { name: 'John', email: 'john@example.com' };const { name, email } = user;
// Rename propertiesconst { name: userName, email: userEmail } = user;
// Default valuesconst { role = 'user' } = user;
// Nested destructuringconst { user: { name, email } } = response;
// Function parametersfunction displayUser({ name, email, role = 'user' }) { console.log(`${name} (${role})`);}스프레드 연산자
섹션 제목: “스프레드 연산자”// Copy arraysconst original = [1, 2, 3];const copy = [...original];
// Merge arraysconst merged = [...arr1, ...arr2];
// Copy objectsconst user = { name: 'John', email: 'john@example.com' };const userCopy = { ...user };
// Merge objectsconst merged = { ...defaults, ...options };
// Update propertiesconst updated = { ...user, email: 'newemail@example.com' };비동기 프로그래밍
섹션 제목: “비동기 프로그래밍”// Basic promiseconst promise = new Promise((resolve, reject) => { if (success) { resolve(result); } else { reject(error); }});
// Promise methodsPromise.all([p1, p2, p3]) .then(results => console.log(results)) .catch(error => console.error(error));
Promise.race([p1, p2]) .then(result => console.log(result));비동기/대기
섹션 제목: “비동기/대기”// Preferred for readabilityasync function fetchUser(userId) { try { const response = await fetch(`/api/users/${userId}`); if (!response.ok) throw new Error('User not found'); const data = await response.json(); return data; } catch (error) { console.error('Failed to fetch user:', error); throw error; }}
// Usageconst user = await fetchUser(123);
// Multiple operationsasync function loadDashboard() { const users = await fetchUsers(); const posts = await fetchPosts(); const comments = await fetchComments();
return { users, posts, comments };}주석 및 문서
섹션 제목: “주석 및 문서”인라인 댓글
섹션 제목: “인라인 댓글”// Explain WHY, not WHATconst result = calculateTotal(items, taxRate); // ✅ Why
// ❌ Don't explain obvious codeconst x = 5; // Set x to 5const sum = a + b; // Add a and bJSDoc 댓글
섹션 제목: “JSDoc 댓글”/** * Calculate the total price of items including tax * * @param {Array<Object>} items - Array of items with price property * @param {number} taxRate - Tax rate as decimal (0.1 = 10%) * @returns {number} Total price including tax * @throws {Error} If items is not an array * @example * const total = calculateTotal( * [{ price: 100 }, { price: 50 }], * 0.1 * ); * console.log(total); // 165 */function calculateTotal(items, taxRate = 0) { if (!Array.isArray(items)) { throw new Error('Items must be an array'); }
const subtotal = items.reduce((sum, item) => sum + item.price, 0); return subtotal * (1 + taxRate);}오류 처리
섹션 제목: “오류 처리”시도/캐치
섹션 제목: “시도/캐치”// Always handle errorstry { const result = riskyOperation();} catch (error) { console.error('Operation failed:', error);} finally { cleanup();}
// Be specific with errorstry { const data = JSON.parse(jsonString);} catch (error) { if (error instanceof SyntaxError) { console.error('Invalid JSON'); } else { console.error('Unknown error'); }}사용자 정의 오류
섹션 제목: “사용자 정의 오류”class ValidationError extends Error { constructor(message) { super(message); this.name = 'ValidationError'; }}
// Usageif (!isValidEmail(email)) { throw new ValidationError(`Invalid email: ${email}`);}DOM 조작
섹션 제목: “DOM 조작”요소 선택
섹션 제목: “요소 선택”// Modern methods (preferred)const element = document.querySelector('#my-id');const elements = document.querySelectorAll('.my-class');
// Avoid older methods// const el = document.getElementById('my-id'); // ❌// const els = document.getElementsByClassName('my-class'); // ❌
// Cache elementsconst button = document.querySelector('button');button.addEventListener('click', handler);이벤트 처리
섹션 제목: “이벤트 처리”// Use addEventListenerelement.addEventListener('click', (event) => { event.preventDefault(); handleClick();});
// Remove listenerselement.removeEventListener('click', handler);
// Event delegationcontainer.addEventListener('click', (event) => { if (event.target.matches('.item')) { handleItemClick(event.target); }});DOM 업데이트
섹션 제목: “DOM 업데이트”// Use textContent (safer than innerHTML)element.textContent = 'Safe text'; // ✅
// Use innerHTML only for trusted contentelement.innerHTML = `<b>${escapeHtml(text)}</b>`;
// Class manipulationelement.classList.add('active');element.classList.remove('inactive');element.classList.toggle('disabled');
// Attribute manipulationelement.setAttribute('data-id', userId);const id = element.getAttribute('data-id');element.removeAttribute('disabled');모듈 패턴
섹션 제목: “모듈 패턴”ES6 모듈
섹션 제목: “ES6 모듈”// Exportexport const helper = () => { };export default Dashboard;
// Importimport Dashboard from './dashboard.js';import { helper } from './helper.js';import * as utils from './utils.js';모범 사례 요약
섹션 제목: “모범 사례 요약”하세요
섹션 제목: “하세요”- 기본적으로 const를 사용
- 설명이 포함된 이름을 사용하세요.
- 콜백에 화살표 기능 사용
- Promise에 async/await를 사용하세요.
- 문서 복합 기능
- DOM 요소 캐시
- 이벤트 위임을 활용하세요
- 순수 함수 작성
- 기능에 집중하세요
하지 마세요
섹션 제목: “하지 마세요”- var(레거시) 사용
- 전역 변수를 사용하세요
- 긴 함수 생성(50줄 이상)
- 깊게 중첩된 코드
- eval()을 사용하세요.
- 인라인 이벤트 핸들러를 사용하세요.
- 프로덕션에 console.log()를 그대로 둡니다.
- 메모리 누수 생성
- 함수 매개변수 변경
도구 및 린팅
섹션 제목: “도구 및 린팅”ESLint 구성
섹션 제목: “ESLint 구성”{ "env": { "browser": true, "es2021": true, "node": true }, "extends": ["eslint:recommended"], "rules": { "indent": ["error", 2], "quotes": ["error", "single"], "semi": ["error", "always"], "no-var": "error", "prefer-const": "error" }}더 예쁜 구성
섹션 제목: “더 예쁜 구성”{ "semi": true, "singleQuote": true, "trailingComma": "es5", "printWidth": 100, "tabWidth": 2}관련 문서
섹션 제목: “관련 문서”- CSS 지침
- 행동강령
- 기여 워크플로우
- PHP 표준
#xoops #javascript #es6 #코딩 표준 #모범 사례