What is the harm of overly pursuing a short and elegant code?

Source: Internet
Author: User
Tags greatest common divisor
I am currently a acmer, the direction of web development, while Python,php,golang,js and other languages have been covered.

Because of the early contact with Python. Deeply influenced by the python take doctrine. Many small projects are written, but almost every project references many open source projects, and the code length is extremely short.

such as a recent management system. The entire foreground has only one static page, the data are all transmitted in JSON format, and the interface is rendered using ANGULARJS. Main angularjs+bootstrap+ jquery then has several bootstrap and jquery widgets. The server is Php+mysql+memcache, At the same time design the service side even want to upgrade NoSQL database and use Golang replace PHP. Such a lineup is bigger than some large-scale management systems. I feel a bit of a fuss.
And then complete the entire front desk additions and deletions, statistics of their own code to find the amount of code is less than 1000 lines (minus the referenced library code). Estimated total completion of 1500 lines of death.

Someone else a page template code on 1000 lines. Often hear such a senior one weeks to develop a website, and then tens of thousands of lines of code. That's ashamed.

And then write yourself some data to crawl the script or something. The code is too small to kill 200 lines. But it cites a whole bunch of unnamed open source projects. And the code like this is everywhere.

UserList = [I.split (":") for I in Open ("User.txt"). Read (). Split ("\ n") If Len (i) >0]

Reply content:

In fact, in Microsoft, if there is a person with a good algorithm, or what artifice, anyway very tall on a thing to get out, but others do not understand, this time we will be asked to open lectures and write tutorials, to church colleagues, otherwise the program will probably be rejected.

So as long as the author of this code can always do so, the harm is probably not very much.

Say "degree" in everything, you said "excessive".
The biggest disadvantage is the poor readability.

PS: The last example I think is OK, not too much, not to affect the understanding of others.

The question of "excessive" in many places, the questioner may probably have their own answers in mind.

The pursuit of short code, can be through different methods, as long as the choice can improve the readability of the method, in fact, not. The code in the title, for example, is not yet short and elegant, so it can be shorter and easier to read (this line is less than 80 characters), which is usually a more recommended practice.
userList=[line.split(":")forlineinopen("user.txt")ifline]
Wasting time "optimizing" your behavior in a system of rules that others make
While
(i >= x | | J >= x) {x = (i% x = = 0 && J% x = = 0)? (y = x) + 1:x + 1; } once wrote a play of a greatest common divisor method, and now even I can not understand, the visible code legibility is how important!
--The micro-blog of mean text | Sina
Weibo
If product development requires rapid iteration, using open source libraries to meet time and performance requirements, and ensuring code simplification, I think this is understandable.

From my experience, there may be several points to note:

1, whether the company's legal department allows the use of these open source libraries

Open source Agreement so many, perhaps a library of the agreement will cause the company product legal dispute, this kind of thing should try to avoid

2, happy to use a variety of third-party libraries, please also pay attention to the optimization problem

In addition to performance optimizations, there are some strange problems that need to be optimized.

Two days ago look at a video of pea pods "pea pods: Growing Pains , which mentions that as the code expands, it encounters a bottleneck of Dex's upper 65536 function, and has to merge functions repeatedly.
When it comes to choosing a third-party library, be careful not to introduce unnecessary baggage in order to use a function.

I am not familiar with Python, I am not sure if there will be similar pits, for reference only.

3, the last point, but the most important point, The more streamlined the code, the more fully commented


A row of melted miscellaneous library functions of NB code, at that time written out may be cool, the hearts of the dark scold oneself really TMD is genius.
After two days to see the discovery completely do not understand why the original did so.

I've done this kind of thing more than once.

For example, a long regular expression, a comment that contains an example of a matching target, is absolutely true.
When you need to change the platform, you will probably need to rewrite the regular expression of the wording, the reference example is there, write unit test.
But one time I forgot ...

So, full annotation is necessary, not only for others, but also for themselves. For Python
Suggest a look at pep 8--Style guide for Python Code

When I was a beginner, I also liked to use various "trick" to make the code look "bunker."
But in fact, legibility is too poor, other people will look at the side of the scold ... A bull man said, " The code was first seen, and then the machine was executed.”。
One of the Ancients said: There is no beauty or evil in things, extremes meet”。

So the pursuit is a good thing, but too much pursuit, inevitably brought to the extreme.
Life is too short to be clear about what to pursue. :) The maintainability and scalability of large project development is important. Short may sometimes be inconsistent with extensibility. Over-referencing, especially for unknown open source projects, may be inconsistent with maintainability ... Elegant ... How to define Elegance ...?

P.S.: Go + Angularjs is a normal match ... There is no special feeling that this is a overkill .... The short code mainly looks at the question: whether the subsequent programmer's understanding increases the difficulty.

If it's easy to understand, it's no problem.

If it is not easy to understand, then try to write a little more code, easy to understand the main.

In actual development engineering, code is often passed through the hands of multiple programmers, easy to understand is important, excessive pursuit of code short is not advocated.

This degree of certainty depends on experience, depending on the different teams and different projects.

The code above has a small problem: You use the variable i, if using line may be better understood.
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.