|
1 | 1 | # Code Organization |
2 | 2 |
|
3 | | -Where everything lives and why |
| 3 | +_Where everything lives and why_ |
| 4 | + |
| 5 | +## Table of Contents |
| 6 | + |
| 7 | +- [Overview](#overview) |
| 8 | +- [Folder Structure Strategies](#folder-structure-strategies) |
| 9 | + - [Flat vs Deep Structures](#flat-vs-deep-structures) |
| 10 | + - [Recommended: Feature-Based Organization](#recommended-feature-based-organization) |
| 11 | +- [File Naming Strategies](#file-naming-strategies) |
| 12 | +- [Naming Conventions](#naming-conventions) |
| 13 | + - [Variables and Functions](#variables-and-functions) |
| 14 | + - [Files and Folders](#files-and-folders) |
| 15 | + - [Component/Class Names](#componentclass-names) |
| 16 | + - [Principle: Say What It Does](#principle-say-what-it-does) |
| 17 | +- [A Practical Approach](#a-practical-approach) |
| 18 | + - [Class Names Reflect Their Location](#class-names-reflect-their-location) |
| 19 | +- [Is There a Standard Organization?](#is-there-a-standard-organization) |
| 20 | + |
| 21 | +--- |
4 | 22 |
|
5 | 23 | ## Overview |
6 | 24 |
|
7 | | -*Coming soon...* |
| 25 | +Code organization is crucial yet tricky. |
| 26 | + |
| 27 | +We can make code powerful, research patterns and algorithms, but what happens after? |
| 28 | + |
| 29 | +- **The Knowledge Loss:** Today's brilliant pattern becomes tomorrow's forgotten memory without clear structure. Proper organization bridges the gap between understanding and recall. |
| 30 | + |
| 31 | +- **The Memory Problem:** You might remember your code structure for about a week. Beyond that, you'll definitely spend considerable time re-learning it if the code isn't organized well. |
| 32 | + |
| 33 | +- **The Team Collaboration Problem:** Imagine working with team members who each have different coding styles. Without proper organization, this becomes exhausting and unproductive. |
| 34 | + |
| 35 | +- **The Productivity Impact:** Well-organized code saves hours. Poorly organized code wastes them. The difference is in the initial investment of structure and documentation. |
| 36 | + |
| 37 | +### Quick Takeaways |
| 38 | + |
| 39 | +Before diving into details, here are the key principles: |
| 40 | + |
| 41 | +1. **Feature-based organization** - Group related code by business feature (users, products, orders) |
| 42 | +2. **Consistent naming** - Use the same pattern throughout your project (kebab-case, PascalCase, etc.) |
| 43 | +3. **Descriptive names** - Classes, functions, and files should explain what they do |
| 44 | +4. **Path + name = context** - Folder location + class name tells the complete story |
| 45 | +5. **No universal standard** - Choose what works for your team and stick with it |
| 46 | + |
| 47 | +## Folder Structure Strategies |
| 48 | + |
| 49 | +A well-planned folder structure can make code much easier to maintain. |
| 50 | + |
| 51 | +### Flat vs Deep Structures |
| 52 | + |
| 53 | +**Flat Structure (Few folders):** |
| 54 | + |
| 55 | +``` |
| 56 | +project/ |
| 57 | +├── src/ |
| 58 | +│ ├── users.ts |
| 59 | +│ ├── products.ts |
| 60 | +│ ├── orders.ts |
| 61 | +│ └── utils.ts |
| 62 | +``` |
| 63 | + |
| 64 | +- ✅ Simple, easy to find files |
| 65 | +- ✅ Good for small projects |
| 66 | +- ❌ Gets messy with scale |
| 67 | +- ❌ All files mixed together |
| 68 | + |
| 69 | +**Deep Structure (Many folders):** |
| 70 | + |
| 71 | +``` |
| 72 | +project/ |
| 73 | +├── src/ |
| 74 | +│ ├── features/ |
| 75 | +│ │ ├── users/ |
| 76 | +│ │ │ ├── controllers/ |
| 77 | +│ │ │ ├── models/ |
| 78 | +│ │ │ ├── routes/ |
| 79 | +│ │ │ └── utils/ |
| 80 | +│ │ ├── products/ |
| 81 | +│ │ └── orders/ |
| 82 | +│ ├── core/ |
| 83 | +│ │ ├── database/ |
| 84 | +│ │ ├── auth/ |
| 85 | +│ │ └── middleware/ |
| 86 | +│ └── shared/ |
| 87 | +│ ├── types/ |
| 88 | +│ └── constants/ |
| 89 | +``` |
| 90 | + |
| 91 | +- ✅ Scales well |
| 92 | +- ✅ Clear separation of concerns |
| 93 | +- ✅ Easy to locate related code |
| 94 | +- ❌ Can become too deep |
| 95 | +- ❌ More navigation required |
| 96 | + |
| 97 | +### Recommended: Feature-Based Organization |
| 98 | + |
| 99 | +Organize by features/modules first, then by technical layers: |
| 100 | + |
| 101 | +``` |
| 102 | +project/ |
| 103 | +├── src/ |
| 104 | +│ ├── features/ |
| 105 | +│ │ ├── users/ # User-related everything |
| 106 | +│ │ │ ├── index.ts |
| 107 | +│ │ │ ├── service.ts |
| 108 | +│ │ │ ├── types.ts |
| 109 | +│ │ │ └── routes.ts |
| 110 | +│ │ ├── products/ |
| 111 | +│ │ └── orders/ |
| 112 | +│ ├── core/ # Shared infrastructure |
| 113 | +│ │ ├── database.ts |
| 114 | +│ │ ├── auth.ts |
| 115 | +│ │ └── config.ts |
| 116 | +│ └── shared/ # Common utilities |
| 117 | +│ ├── utils/ |
| 118 | +│ ├── types/ |
| 119 | +│ └── constants/ |
| 120 | +``` |
| 121 | + |
| 122 | +**Why this helps:** |
| 123 | + |
| 124 | +- Related code stays together |
| 125 | +- New developers understand quickly |
| 126 | +- Easy to refactor entire features |
| 127 | +- Clear ownership per module |
| 128 | + |
| 129 | +## File Naming Strategies |
| 130 | + |
| 131 | +**Consistency is key:** |
| 132 | + |
| 133 | +- ✅ Descriptive names |
| 134 | +- ✅ Same pattern across the project |
| 135 | +- ✅ Match project conventions |
| 136 | + |
| 137 | +**Common patterns:** |
| 138 | + |
| 139 | +``` |
| 140 | +kebab-case -> user-service.ts |
| 141 | +camelCase -> userService.ts |
| 142 | +PascalCase -> UserService.ts |
| 143 | +snake_case -> user_service.ts |
| 144 | +``` |
| 145 | + |
| 146 | +**Choose one convention and use it consistently.** |
| 147 | + |
| 148 | +## Naming Conventions |
| 149 | + |
| 150 | +Names should reveal intent and purpose. |
| 151 | + |
| 152 | +### Variables and Functions |
| 153 | + |
| 154 | +**✅ Good:** |
| 155 | + |
| 156 | +```typescript |
| 157 | +const userAge = 25 |
| 158 | +const isAuthenticated = true |
| 159 | +const fetchUserData = async () => {} |
| 160 | +const calculateTotalPrice = () => {} |
| 161 | +``` |
| 162 | + |
| 163 | +**❌ Bad:** |
| 164 | + |
| 165 | +```typescript |
| 166 | +const age = 25 // What age? |
| 167 | +const flag = true // What flag? |
| 168 | +const fetchData = () => {} // What data? |
| 169 | +const calc = () => {} // What does it calculate? |
| 170 | +``` |
| 171 | + |
| 172 | +### Files and Folders |
| 173 | + |
| 174 | +**✅ Good:** |
| 175 | + |
| 176 | +``` |
| 177 | +/src/features/user/UserService.ts |
| 178 | +/src/core/database/DatabaseConnection.ts |
| 179 | +/src/utils/formatDate.ts |
| 180 | +``` |
| 181 | + |
| 182 | +**❌ Bad:** |
| 183 | + |
| 184 | +``` |
| 185 | +/src/f1/s1.ts |
| 186 | +/src/core/db.ts |
| 187 | +/src/utilities/f.ts |
| 188 | +``` |
| 189 | + |
| 190 | +### Component/Class Names |
| 191 | + |
| 192 | +**✅ Good:** |
| 193 | + |
| 194 | +```typescript |
| 195 | +class UserRepository |
| 196 | +class PaymentProcessor |
| 197 | +class EmailValidator |
| 198 | +``` |
| 199 | + |
| 200 | +**❌ Bad:** |
| 201 | + |
| 202 | +```typescript |
| 203 | +class UserClass // Too generic! |
| 204 | +class PaymentManager // Too vague! |
| 205 | +class EmailHelper // Not a class, sounds like a utility |
| 206 | +``` |
| 207 | + |
| 208 | +### Principle: Say What It Does |
| 209 | + |
| 210 | +**Good names:** |
| 211 | + |
| 212 | +- Answer "What does it do?" |
| 213 | +- Answer "What is it?" |
| 214 | +- Are self-documenting |
| 215 | + |
| 216 | +**Examples:** |
| 217 | + |
| 218 | +``` |
| 219 | +✅ getUserById() → Gets user by id |
| 220 | +✅ isEmailValid() → Checks if email is valid |
| 221 | +✅ formatCurrency() → Formats currency |
| 222 | +✅ sendEmailNotification() → Sends email notification |
| 223 | + |
| 224 | +❌ getUser() → Gets user how? |
| 225 | +❌ validate() → Validates what? |
| 226 | +❌ format() → Formats what? |
| 227 | +❌ notify() → Notifies about what? |
| 228 | +``` |
| 229 | + |
| 230 | +## A Practical Approach |
| 231 | + |
| 232 | +Here's an approach that has proven helpful: applying abstraction in both naming conventions and folder structure. This pattern helps make code easier to understand and navigate. |
| 233 | + |
| 234 | +**The Abstraction Principle:** |
| 235 | + |
| 236 | +- Folder structure reveals context |
| 237 | +- Naming indicates specificity |
| 238 | +- Path itself tells a story |
| 239 | + |
| 240 | +**Example File Structures:** |
| 241 | + |
| 242 | +``` |
| 243 | +/src/features/user/Generator.ts |
| 244 | +→ Read as: User-specific generator feature |
| 245 | + |
| 246 | +/src/core/database/Connection.ts |
| 247 | +→ Read as: Database connection (core infrastructure) |
| 248 | + |
| 249 | +/src/utils/Formatter.ts |
| 250 | +→ Read as: General data formatter utility |
| 251 | +``` |
| 252 | + |
| 253 | +**Benefits of This Approach:** |
| 254 | + |
| 255 | +- Each path conveys meaning |
| 256 | +- Context is clear from location |
| 257 | +- Easy to understand without opening files |
| 258 | +- Self-documenting structure |
| 259 | + |
| 260 | +### Class Names Reflect Their Location |
| 261 | + |
| 262 | +Here's a helpful pattern: combine the folder context with the file purpose in the class name. |
| 263 | + |
| 264 | +Here's how it reads: |
| 265 | + |
| 266 | +```typescript |
| 267 | +// File: /src/features/user/Generator.ts |
| 268 | +class UserGenerator { |
| 269 | + // Breakdown: |
| 270 | + // - "User" = context from folder (/features/user/) |
| 271 | + // - "Generator" = purpose from file name (Generator.ts) |
| 272 | + // Result: A generator for user-related functionality |
| 273 | +} |
| 274 | +``` |
| 275 | + |
| 276 | +```typescript |
| 277 | +// File: /src/core/database/Connection.ts |
| 278 | +class DatabaseConnection { |
| 279 | + // Breakdown: |
| 280 | + // - "Database" = context from folder (/core/database/) |
| 281 | + // - "Connection" = purpose from file name (Connection.ts) |
| 282 | + // Result: A connection for database operations |
| 283 | +} |
| 284 | +``` |
| 285 | + |
| 286 | +```typescript |
| 287 | +// File: /src/utils/Formatter.ts |
| 288 | +class FormatterUtils { |
| 289 | + // Breakdown: |
| 290 | + // - "Formatter" = purpose from file name (Formatter.ts) |
| 291 | + // - "Utils" = indicates utility context from (/utils/) |
| 292 | + // Result: A general formatting utility |
| 293 | +} |
| 294 | +``` |
| 295 | + |
| 296 | +**The Pattern:** |
| 297 | + |
| 298 | +1. **Identify the context**: Look at the folder path (e.g., `/features/user/` → "User") |
| 299 | +2. **Identify the purpose**: Look at the file name (e.g., `Generator.ts` → "Generator") |
| 300 | +3. **Combine them**: Class name = Context + Purpose |
| 301 | +4. **Result**: Self-documenting code that tells a complete story |
| 302 | + |
| 303 | +This approach creates a mental map: location + class name gives you full understanding without opening the file |
| 304 | + |
| 305 | +## Is There a Standard Organization? |
| 306 | + |
| 307 | +**Short answer: No universal standard exists.** |
| 308 | + |
| 309 | +Code organization depends on each developer's style, team preferences, and project needs. What works for a startup might not work for an enterprise, and what's perfect for a solo project might not scale for a team of 50. |
| 310 | + |
| 311 | +**The real standard:** |
| 312 | + |
| 313 | +- Be consistent |
| 314 | +- Document your choices |
| 315 | +- Choose an approach that works for **your team** |
| 316 | +- Refactor when it stops serving you |
| 317 | + |
| 318 | +**Remember:** The best organization is one that your team can understand and maintain. There's no "one size fits all" solution—just principles and patterns you adapt to your situation. |
0 commit comments