메서드가 지켜야하는 것

개요

Here are some guidelines to consider when crafting methods:

  1. Single Responsibility Principle: Each method should have a single responsibility. It should perform only one task. This makes it easier to maintain and test.
  2. Descriptive Naming: The method's name should clearly express what the method does. This makes the code more readable and understandable. For example: calculateSum() is better than calc().
  3. Short and Sweet: If possible, keep your methods small. Methods that are shorter are easier to read and understand.
  4. Avoid Side Effects: Your method should not affect anything beyond the scope of its operations, keeping a predictable behavior.
  5. Use Parameters Wisely: Consider the number of parameters your method requires. Too many parameters can make a method difficult to understand and use. Consider using an object to wrap these parameters if necessary.
  6. Return Results When Possible: If possible, prefer methods that return results. This can make it easier to test the method and can sometimes result in cleaner code.
  7. Commenting and Documentation: Use comments to explain complex code or algorithms. They will help others understand why and how a particular piece of code works. Adding a brief description of the purpose, parameters, and returned results in a method's documentation can be very helpful.

좋은 메서드 가이드라인에서, 사이드 이펙트를 피하라고 하였는데