search
HomeJavajavaTutorialHow did our company completely eliminate 2,000 if-else in the project?


How did our company completely eliminate 2,000 if-else in the project?

#5 Ways to Design Better Software and Replace If-Else. Beginner to Advanced Example

Let me just say this: If-Else is usually a poor choice.

It leads to complex designs, poor code readability, and may lead to difficulty in refactoring.

But it does make sense that If-Else has become the de facto code branching solution. This is the first thing that is taught to all aspiring developers. Unfortunately, many developers never move forward to a more appropriate branching strategy.

Some people’s mantra is: If-Else is a hammer, everything is a nail.

The inability to distinguish when to use a more appropriate method is one of the things that differentiates juniors from juniors.

I'm going to show you some tips and patterns that will put an end to this horrible practice.

The difficulty increases with each example.

1. Completely unnecessary Else block

This is perhaps one of the biggest sins that junior developers are guilty of. The example below is a great example of what happens when you are told that If-Else is great.

How did our company completely eliminate 2,000 if-else in the project?

Simply remove the else` block to simplify this process.

How did our company completely eliminate 2,000 if-else in the project?

Looks more professional, right?

You will often find that the other blocks are not actually needed at all. Like in this case, you want to perform some action and return immediately if a specific condition is met.

2. Value Assignment

If you are assigning a new value to a variable based on some input provided, stop the If-Else nonsense - a more readable sexual method.

How did our company completely eliminate 2,000 if-else in the project?

As simple as it is, it's terrible. First, If-Else can easily be replaced by a switch here. However, we can simplify this code further by removing else completely.

How did our company completely eliminate 2,000 if-else in the project?

If else is not used, we are left with clean readable code. Note that I also changed the style to a fast return rather than a single return statement - there is no point in continuing to test for a value if the correct value has already been found.


3. Precondition Check

Usually, I find that if a method provides an invalid value, there is no way to continue execution. meaningful.

Suppose we have the DefineGender method from before, which requires that the input value provided must always be 0 or 1.

How did our company completely eliminate 2,000 if-else in the project?

It makes no sense to execute this method without value validation. Therefore, we need to check some prerequisites before allowing the method to continue execution.

Applying the guard clause defensive coding technique, you will check the input values ​​of the method and then continue executing the method.

How did our company completely eliminate 2,000 if-else in the project?

#At this point, we ensure that the main logic is only executed when the value falls within the expected range.

Now, IF has also been replaced by ternary, because there is no longer a need to return "unknown" by default at the end.

4. Convert If-Else to Dictionary - Avoid If-Else completely

Suppose you need to perform some operations that will select based on some conditions, We knew we would have to add more operations in the future.

How did our company completely eliminate 2,000 if-else in the project?

Maybe someone prefers to use the tried and tested If-Else. If you add a new action, simply add something else. Very simple. However, this approach is not a good design in terms of maintenance.

Knowing that we need to add new operations in the future, we can refactor If-Else into a dictionary.

How did our company completely eliminate 2,000 if-else in the project?

#Readability has been greatly improved and the code can be more easily inferred.

Note that the dictionary is placed inside the method for illustration purposes only. You may want to provide it from somewhere else.

5. Extend the application—avoid using If-Else entirely

This is a slightly more advanced example.

Know when to even eliminate Ifs entirely by replacing them with objects.

Often you will find yourself having to extend certain parts of your application. As a junior developer, you may be tempted to do this by adding additional If-Else (i.e. else-if) statements.

Give this illustrative example. Here we need to display the Order instance as a string. First, we only have two string representations: JSON and plain text. Using If-Else is not a big deal at this stage, if we can easily replace the others, just as mentioned earlier.

How did our company completely eliminate 2,000 if-else in the project?

#Knowing that we need to extend this part of the application, this approach is absolutely unacceptable.

The above code not only violates the "open/close" principle, but also does not read well and can cause maintainability troubles.

The correct approach is one that follows the SOLID principles - we do this by implementing a dynamic type discovery process (in this case the Strategy pattern).

The process of refactoring this messy process is as follows:

· Extract each branch into a separate strategy class using a common interface

· Dynamically find all classes that implement the common interface

·Determine which strategy to execute based on the input

The code that replaces the above example is as follows. Yes, this is way more code. It requires you to understand how type discovery works. But dynamically scaling applications is an advanced topic.

I only show the exact part that will replace the If-Else example. If you want to see all the objects involved, check out this gist.

How did our company completely eliminate 2,000 if-else in the project?

Let’s take a quick look at the code.

The method signature remains unchanged because the caller does not need to know about our refactoring.

First, get all types in the assembly that implement the general interface IOrderOutputStrategy. Then, we create a dictionary, the name of the displayName of the formatter is key, and the type is value.

Then select the formatter type from the dictionary and try to instantiate the policy object.

Finally, call ConvertOrderToString of the strategy object.

The above is the detailed content of How did our company completely eliminate 2,000 if-else in the project?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:Java学习指南. If there is any infringement, please contact admin@php.cn delete
How does the class loader subsystem in the JVM contribute to platform independence?How does the class loader subsystem in the JVM contribute to platform independence?Apr 23, 2025 am 12:14 AM

The class loader ensures the consistency and compatibility of Java programs on different platforms through unified class file format, dynamic loading, parent delegation model and platform-independent bytecode, and achieves platform independence.

Does the Java compiler produce platform-specific code? Explain.Does the Java compiler produce platform-specific code? Explain.Apr 23, 2025 am 12:09 AM

The code generated by the Java compiler is platform-independent, but the code that is ultimately executed is platform-specific. 1. Java source code is compiled into platform-independent bytecode. 2. The JVM converts bytecode into machine code for a specific platform, ensuring cross-platform operation but performance may be different.

How does the JVM handle multithreading on different operating systems?How does the JVM handle multithreading on different operating systems?Apr 23, 2025 am 12:07 AM

Multithreading is important in modern programming because it can improve program responsiveness and resource utilization and handle complex concurrent tasks. JVM ensures the consistency and efficiency of multithreads on different operating systems through thread mapping, scheduling mechanism and synchronization lock mechanism.

What does 'platform independence' mean in the context of Java?What does 'platform independence' mean in the context of Java?Apr 23, 2025 am 12:05 AM

Java's platform independence means that the code written can run on any platform with JVM installed without modification. 1) Java source code is compiled into bytecode, 2) Bytecode is interpreted and executed by the JVM, 3) The JVM provides memory management and garbage collection functions to ensure that the program runs on different operating systems.

Can Java applications still encounter platform-specific bugs or issues?Can Java applications still encounter platform-specific bugs or issues?Apr 23, 2025 am 12:03 AM

Javaapplicationscanindeedencounterplatform-specificissuesdespitetheJVM'sabstraction.Reasonsinclude:1)Nativecodeandlibraries,2)Operatingsystemdifferences,3)JVMimplementationvariations,and4)Hardwaredependencies.Tomitigatethese,developersshould:1)Conduc

How does cloud computing impact the importance of Java's platform independence?How does cloud computing impact the importance of Java's platform independence?Apr 22, 2025 pm 07:05 PM

Cloud computing significantly improves Java's platform independence. 1) Java code is compiled into bytecode and executed by the JVM on different operating systems to ensure cross-platform operation. 2) Use Docker and Kubernetes to deploy Java applications to improve portability and scalability.

What role has Java's platform independence played in its widespread adoption?What role has Java's platform independence played in its widespread adoption?Apr 22, 2025 pm 06:53 PM

Java'splatformindependenceallowsdeveloperstowritecodeonceandrunitonanydeviceorOSwithaJVM.Thisisachievedthroughcompilingtobytecode,whichtheJVMinterpretsorcompilesatruntime.ThisfeaturehassignificantlyboostedJava'sadoptionduetocross-platformdeployment,s

How do containerization technologies (like Docker) affect the importance of Java's platform independence?How do containerization technologies (like Docker) affect the importance of Java's platform independence?Apr 22, 2025 pm 06:49 PM

Containerization technologies such as Docker enhance rather than replace Java's platform independence. 1) Ensure consistency across environments, 2) Manage dependencies, including specific JVM versions, 3) Simplify the deployment process to make Java applications more adaptable and manageable.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools