Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not use the If-Else statement

2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Why not use If-Else sentence". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Why not use If-Else statements"!

We are all too familiar with the If-Else statement. You may have seen countless tutorials and read a lot of programming books to improve the way to use If-Else as a branch of de facto. Maybe even your default mode is to use If-Else.

We can end this. The use of If-Else adds complexity under any new conditions. Replace If-Else with a state object. To apply state mode, you only need to use a dedicated state object instead of using If-Else statements to change the behavior of the object.

The era of code like this is gone forever:

Trigger PTSD warning

You must have written more complex branches, and I have done the same. The branching logic above is not even very complex, but if you try to add new conditions, things are bound to get worse.

In addition, if you think it sounds annoying to create a new class instead of simply using branch statements, be sure to wait until you actually use it, you will find this method simple and elegant. In addition to the "D" section, it will also make your code base more solid.

So how do you avoid messy branch code?

We will look at how to replace the If-Else branch in production-ready code. This is a fictional example, but in the same way as I used in the code base of large clients.

Create a very simple Booking class that contains some state. It also has two public methods: Accept () and Cancel (). I did my best to draw a chart showing the different states the reservation might be in.

There are three steps in the process of refactoring branch logic in your code:

Create an abstract base class

Implement each state as a separate class that inherits from the base state

Let the Booking` class have a private or internal method that takes the state base class as a parameter

It's demonstration time!

First, you need a base class that will inherit all states.

Note that the base class also has two methods, Accept and Cancel--, although they are marked as internal here. In addition, the ground state has a "special" EnterState (Booking booking) method. This method is called whenever a new state is assigned to a subscription object.

Second, create a separate class for each state you want to represent.

Notice how each class represents a state, as shown in the figure above. In addition, ExpiredState and CancelledState do not transition bookings to a new state. These two categories are very similar to Null Object Pattern in nature.

Finally, the reservation type itself.

See how the subscription type simply delegates the implementation of Accept and Cancel to its state object? This allows us to remove a lot of conditional logic and make each state focus only on what is important to us, and the current state also has the opportunity to transition the reservation to a new state.

Quan A

1. How to deal with the new conditional function?

If you usually need to use some conditional checking to implement new functionality, now you just need to create a new state class, and you will no longer have to deal with if-else statements.

two。 How do I keep the state object in the database?

No, you don't.

When saving an object to a database such as SQL or NoSQL, the state object is not important; it is only important to understand the state of the object and how it should be mapped to the column. You can map states to friendly type names, enumerations, or integers. If you like, there is always a way to convert the saved value back to the state object.

3. But are you still using If's?

They are essential, especially when used as protective clauses. But the If-Else combination is the root cause of the maintainability problem.

Complexity does not come from the number of classes you have, but from the functionality that those classes undertake. Having many specialized classes will make your code base more readable and maintainable, and generally easier to use.

At this point, I believe you have a deeper understanding of "Why not use If-Else statements". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report