Pesticide pit, only into the know. Every rank match, always be beaten back to the original form.

preface

Projects go from one project to another, code gets rolled over and over again, old stuff gets copied back and forth, and it doesn’t seem interesting.

Want to hold a large knife to cut at will kill, but found that the foundation has a problem, not tied heart.

Instead of blindly adjusting the foundation, it is better to accumulate from the click, the river flows into the sea, and eventually merges into the vast ocean!

First of all, let’s briefly explain the problems encountered in the current project:

  • More repetitive function;
  • Demand is not clear caused by multiple rework, a lot of time consumption in the brainless code fine tuning;
  • .

Maybe it’s the specificity of the project. There may also be something wrong with my overall conception. In order to catch up with the so-called schedule, the project code is filled with many repetitive things.

The cause of the incident was that I didn’t bother to explain the reason, so I directly took part of the screenshots and sent them to the group, only to find all kinds of discomfort later.

Let’s take a look at the previous “encapsulation” :

The following case around this step by step analysis, derivation.

Open to

The reason for the original encapsulation was that the background suddenly returned a type, well, it was quite sudden, suddenly did not pay attention to the type, obviously there was a problem with the App. The most original scheme is in every place without brain Copy, the so-called simple high (make) efficiency (laugh) cost, is nothing to add redundant code, and directly lead to follow-up maintenance is very troublesome.

To be honest, I looked at zha or say, the actual hair group, suddenly feel this code to write really low. Decisive consultation my chicken eldest brother has what good optimization plan.

Chicken boss said two very meaningful words, here to share:

  • You can do abstractions instead of abstractions;
  • The abstractions are not business, but are done according to business morphology.

Long Long ago, I used to think that I have reached a comfortable understanding of object-oriented, chicken boss’s words, let me have to look at myself again, face their own loopholes. Unfortunately, the current existing ability just stays in the extraction method 😅, here is more than explained, one day may wake up.

Chicken boss super like to ask, after listening to my ideas, directly prompt you this writing method still has a lot of room for improvement, repeated code too much, how to change.

In a “careful” look at the encapsulated method, instantiate an Intent in each type and pass the corresponding ID to facilitate subsequent viewing of details based on the ID.

Now that I know where to cut, I’m going to do a little surgery on this method:

  • Simply use an Intent and instantiate the corresponding jump instances of the Intent with different types.

Transformation completed in minutes:

Happy to find the chicken boss to brag ~

Chicken eldest brother leisurely leisurely came sentence: still have a lot of room for improvement, you think again.

After the previous step, the Intent was successfully instantiated down to Only One. If you look closely, each Type corresponds to something like the same operation:

  • Instantiate the Intent to jump;
  • Pass the corresponding ID value

So why don’t I just make a private method that deals exclusively with intents? Different types just need to give me the corresponding parameter values. The transformation is as follows:

Is that okay? Well, I think it’s fine. Look for the chicken boss to brag about ~There is still a lot of room for improvement. Send it to me and I will do it.

I don’t think we’re gonna get the chicken to do it himself. 😅😅😅 I got it. I got it.

The chicken added a few more points:

  • Remove the comment, do not understand what you write, difficult to understand;
  • What if I want to call one of the methods in the When case alone? How to do?
  • Do not handle errors privately. Throw out requirements that may cause problems.
  • A method only does one thing, and again only uses the ID. Why pass the bean? There are new requirements to extend the rewrite method;
  • The first thing you want to do is make sure people read your code and know what it means at a glance. Secondly, do more business oriented to code, readability is the first guarantee, followed by elegance. And you don’t design for performance.

Finally, chicken boss gives a rule (don’t spray if you don’t like it, my boss’s words I regard as classic, thank you!) :

Security > Availability > Maintainability > code simplicity > Performance

Further optimization is proposed for chicken boss:

  • Case provides corresponding processing method separately, can be used independently, method name identifies the meaning of the name;
  • Check that the existing business logic of the code only needs an ID to work with, replacing the parameter Bean with a concrete parameter;
  • Providing methods no longer handle error cases, but direct throws, handled by the actual caller;

After the above adjustment, the code is as follows:

End – Think 🤔

From the beginning thought the best, to think the best repeatedly to the chicken boss comments, old face.

I had to get down on my knees, just one snippet of code, and I could hit the pain straight, and even I, who wrote the code, ignored the jump to multiple scene calls, multiple frequent copies, and even the subsequent smug encapsulation of a small method.

Writing code, not just writing code, if you just write code for the sake of writing code, writing code for the sake of completing a task, writing code, really hard to see, here is to myself.

Redundant code versus lean code; Relying on comments to read through code and simply reading through code to know what it means; Disorganized code and well-designed code; .

Ask yourself, still want to fool yourself?

There is a long way to go!

One day

Quick question:

  • Do you ever feel bad about your code?
  • How did you handle it?