Sobes.tech
Middle

How can you determine that the code organization is poor?

sobes.tech AI

Answer from AI

  • Lack of consistency: Different developers use different formatting styles, variable and function naming conventions.
  • High Coupling: Changes in one module require modifications in many others.
  • Low Cohesion: A module or function performs too many unrelated tasks.
  • Code duplication: Repeating code fragments instead of using functions or shared structures.
  • Violation of SOLID principles: Not following the principles of single responsibility, open/closed, Liskov substitution, interface segregation, and dependency inversion.
  • Long functions and files: Functions that perform too many steps or files with thousands of lines of code.
  • High complexity: Code that is hard to read and understand due to convoluted logic, excessive use of conditional statements, or nested structures.
  • Lack or poor quality of tests: Absence of unit and integration tests, or tests that do not cover critical functionality.
  • Lack of comments or outdated comments: Missing explanations for complex parts of the code or comments that do not match the current implementation.

Example of high coupling:

// Bad: PaymentProcessor function is heavily dependent on a specific EmailSender type
struct EmailSender {}

func (s *EmailSender) SendPaymentConfirmation(email, details string) {
	// Email sending logic
}

struct PaymentProcessor {
	EmailSender EmailSender // Direct dependency
}

func (p *PaymentProcessor) ProcessPayment(amount float64, email string) {
	// Payment processing logic
	p.EmailSender.SendPaymentConfirmation(email, "Payment successful!")
}

Example of low cohesion:

// Bad: UserUtility function performs too many different tasks
struct UserUtility {}

func (u *UserUtility) CreateUser(name, email string) error {
	// Create user in DB
	return nil
}

func (u *UserUtility) GenerateReport(userID int) string {
	// Generate user report
	return "Report data"
}

func (u *UserUtility) SendWelcomeEmail(email string) error {
	// Send welcome email
	return nil
}