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

Case Analysis of web Development Code

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, I would like to share with you the relevant knowledge points of web development code example analysis, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

Usually the code written by experienced programmers is good at first, but as the requirements change, the maintenance staff changes, and the architecture begins to decay, the code begins to get messy.

And sometimes the readability of the code (non-functional requirements) is completely ignored just to complete the function.

Give me a small example of how to maintain a constant focus on code readability when programming.

There is a simple requirement to write a method to generate a string key value, pass in the full class name, and return the key value by calling the method name. the length of key is limited to 50 characters by external conditions.

First, take a look at the following implementation:

Private String generateKey (String service, String method) {String head = "DBO$"; String key = ""; int len = head.length () + service.length () + method.length (); if (len 50) {key = head + method If (key.length () > 50) {key = key.substring (0,48) + ". *";}} return key;}

The implementation of the method is not complex, short, and looks good.

Analyze the logic:

1. First, key consists of a fixed header (head) + service (full class name) + method (method). If it is less than 50 characters, it will be returned directly.

2. If it exceeds the 50-character limit, remove the package name, keep the class name, judge again, and return if it is less than 50 characters at this time.

3. If it still exceeds the 50-character limit (there may be a perverted guy with a long class name or method name), remove the class name together with the class name, and retain the header and method again. If it is less than 50 characters, it will be returned.

4. Finally, if there is an abnormal long method (46 + characters), there is no way but to violently truncate to 50 characters to return.

This implementation maximizes the retention of all useful information in the generated key, and discards cases that exceed the limit according to the importance of the information.

There is only one problem here. There are only four judgments for this business rule, and the implementation has nested if statements three times. Fortunately, this method is relatively short and readability is not a problem.

In reality, many business rules are much more complex than this. I have seen some implementations of if nesting up to 10 layers, and the methods are also extremely long. Of course, there were not so many layers nested at first, but later, with the evolution of time, the business rules changed and increased, and later programmers continued to nest in this way, slowly evolving to this point. By the time I saw it, there were 10 layers.

Programmers have a kind of programming inertia, especially when doing maintenance programming. If you take over a system made by others at first, it is impossible to understand and control the overall situation at once.

When adding new features, adding logic to the original code makes it easy to maintain the writing inertia of the original program (consider it safer to write this way).

So a 10-layer nested if business logic method implementation, the first programmer may only write 3 times of nesting, feel good without losing simplicity. Later, programmers who write floors 4, 5 and 6 are lazy and don't want to change, and programmers who write floors 8, 9 and 10 are probably afraid to move any more. The code finally turns into a big pile of *.

Consider the above simple example, how to change it. I wrote an implementation myself as follows:

Private String generateKey (String service, String method) {String head = "DBO$"; String key = head + service + "." + method; / / head + service (with package) + method if (key.length ()

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