- Building Microservices with Spring
- Dinesh Rajput Rajesh R V
- 237字
- 2021-07-02 14:54:12
Code tangling
It is a coupling of concerns in the application. Code tangling occurs when there is a mixing of cross-cutting concerns with the application's business logic. It promotes tight coupling between the cross-cutting and business modules. Let's see the following code to understand more about code tangling:
public class TransferServiceImpl implements TransferService { public void transfer(Account a, Account b, Double amount) { //Security concern start here if (!hasPermission(SecurityContext.getPrincipal()) { throw new AccessDeniedException(); } //Security concern end here //Business logic start here Account aAct = accountRepository.findByAccountId(a); Account bAct = accountRepository.findByAccountId(b); accountRepository.transferAmount(aAct, bAct, amount); ... } }
As you can see in the preceding code, security concern code (highlighted) is mixing with application's business logic code. This situation is an example of code tangling. Here we have only included security concern, but in the enterprise application you have to implement multiple cross-cutting concerns such as logging, transaction management and so on. In such cases, it will be even more complicated to manage the code and make any change to the code, which may cause critical bugs in the code as follows in the figure:
In the preceding figure, you can see there are three cross-cutting concerns which are distributed across the TransferService business class and cross-cutting concerns logic mixing with AccountService's business logic. This coupling between the concerns and application's logic is called code tangling. Let's see another main problem if we are using aspects for cross-cutting concern.