How to write easy-to-understand code [Summary 3]

About methods / functions

One job per function Method classification

Basically, one function is used for each method.

Be careful of things that cannot be given a simple name. As a result, single-function ones are easier to reuse than multifunctional ones.

×

void playWholeSongAndStop(Music song)
{play}

void play(Music song)
{}
void stop(Music song)
{}

It is easier to customize according to the application if it is divided into small pieces as in the above example. In the example of ×, playback is performed at double speed. Play halfway. It seems that additional functions such as are implemented.

Method classification

It is roughly classified into two types.

  1. Those that perform calculations and algorithms.
  2. A method that does a big job by combining multiple methods. It's difficult to implement large methods correctly ... It's easy to implement small methods correctly. It is said that code maintainability is improved by dividing a complex method that does a big job into a method that does a small job.

Code to be methodized

Basic-> Overlapping parts are made into methods ・ Methodization of conditional expressions ・ Methodization of calculation formulas -Methodization in the conditional branch block ・ Loop method -Method in loop block ・ Methodization of data conversion ・ Methodization of data confirmation -Method of array access ・ Methodization of comment part Please refer to the following source books for details of each.

How to write and divide functions, https://prettysoft.hatenablog.com/entry/20101118/1439990878

Eigen Otsuka, 2015, Coding Technology for Game Programmers, Gijutsu-Hyoronsha

This article cites the above links and books. The terms and some code have been changed to those used in Java.

Recommended Posts

How to write easy-to-understand code [Summary 3]
[RSpec] How to write test code
Summary of how to write annotation arguments
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
How to write code that thinks object-oriented Ruby
How to write test code with Basic authentication
Bit Tetris (how to write)
How to write java comments
[Refactoring] How to write routing
Great poor (how to write)
[Note] How to write Dockerfile/docker-compose.yml
How to write Rails validation
How to write Rails seed
[Ruby] How to write blocks
How to write Rails routing
Studying Java # 6 (How to write blocks)
[Rails] How to write in Japanese
Baseball ball count (how to write)
How to write a ternary operator
Rails on Tiles (how to write)
[Rails] How to write exception handling?
How to write Java variable declaration
Y-shaped road tour (how to write)
How to write ruby if in one line Summary by beginner
How to make a Java calendar Summary
[Basic] How to write a Dockerfile Self-learning ②
[java] Summary of how to handle char
[Easy-to-understand explanation! ] How to use Java instance
[Introduction to Java] How to write a Java program
How to call Swift 5.3 code from Objective-C
[Java] How to output and write files!
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
Write code that is difficult to test
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to write Spring AOP pointcut specifier
How to write an RSpec controller test
[SpringBoot] How to write a controller test
How to write and explain Dockerfile, docker-compose
[Easy-to-understand explanation! ] How to use Java overload
[Easy-to-understand explanation! ] How to use Java encapsulation
How to count UTF-8 code points fast
AtCoder is called TLE and talks about how to write beautiful code
Summary of how to select elements in Selenium
JDBC promises and examples of how to write
Rails: How to write a rake task nicely
[java] Summary of how to handle character strings
[JavaFX] How to write Eclipse permissions in build.gradle
[Rails] How to write when making a subquery
Write code that is easy to maintain (Part 1)
How to color code console output in Eclipse
[Java] Summary of how to abbreviate lambda expressions
How to write an if statement to improve readability-java
JUnit 5: How to write test cases in enum
[Easy-to-understand explanation! ] How to use Java inheritance [Override explanation]
Offline real-time how to write F06 implementation example
Write code that is easy to maintain (Part 4)