当前位置:网站首页>Five bad code practices programmers should avoid

Five bad code practices programmers should avoid

2021-07-21 06:48:09 InfoQ

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" This article will show you five bad code practices , They are enough to disgust all programmers ."}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Turn variable naming into a puzzle game "}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/33\/33ebadeb49a841946bd24958c9003103.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Picture translation :parseDBMXML What does it mean :A、 analysis DBM XML .B、 analysis DB MXML.C、 analysis DB Mx Markup language .D、 analysis DB Mx machine learning ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" First , Start with the simplest ,"},{"type":"text","marks":[{"type":"strong"}],"text":" When you name a function or a variable, you add to it , Use unnecessary abbreviations "},{"type":"text","text":". Good code practice suggests that people name things as clearly as possible , such as "},{"type":"codeinline","content":[{"type":"text","text":"handleFormSubmit"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"getUserConfiguration"}]},{"type":"text","text":", Or is it "},{"type":"codeinline","content":[{"type":"text","text":"parseCustomerInformation"}]},{"type":"text","text":"."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The bad code practice is to use abbreviations and abbreviations as much as possible in naming , So the next developer who takes over your code has to guess what you want to do ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" image "},{"type":"codeinline","content":[{"type":"text","text":"handleBtnClick"}]},{"type":"text","text":"、"},{"type":"codeinline","content":[{"type":"text","text":"getConfig"}]},{"type":"text","text":", or "},{"type":"codeinline","content":[{"type":"text","text":"parseInfo"}]},{"type":"text","text":" This kind of naming is quite casual . It's hard to see the usefulness of functions from these names , But others can still accept them when they do code reviews . Unnecessary abbreviations are more confusing ,btn、func、config, perhaps cb, All of them are too difficult to understand ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" There are more hands and feet in variable naming ! There is a list of unconfirmed users ? Do not write "},{"type":"codeinline","content":[{"type":"text","text":"unconfirmedUsers"}]},{"type":"text","text":", Direct use "},{"type":"codeinline","content":[{"type":"text","text":"users"}]},{"type":"text","text":", In this way, the developer who takes over will have to read through all your code to figure out what this variable refers to . Don't use it against this poor developer discountedProducts, Direct use product The name is enough ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" I want to make another fire ? Sure , Case is your next toy , I promise you , The people who take over your code will definitely hate you . Don't use the example of good code "},{"type":"codeinline","content":[{"type":"text","text":"readXmlDocument"}]},{"type":"text","text":" It's called ( The case of abbreviations should be the same as that of other words ),readXMLDocument So that other developers can read your code more carefully , Read your variable names more carefully to figure out what you're trying to express ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Of course , All these puzzles will die in the code review stage , But you're not going to lie down, are you ? Maybe it's because I don't want to change , Maybe it's because of your rebellious personality , But anyway , You can always boast , Next time PR Make corrections in , And put your hands together and pray that your colleagues will forgive you for your sins ( Maybe they'll forgive you )."}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Complicated code "}]},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/23\/23a7fb14ebf2dc85eeef3af69b28202d.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":" Picture translation : I : Trying to write the entire function in one call "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" I don't know if you've ever proved yourself a software developer Rick Sanchez( One of Rick and Morty's strange, drunken, crazy scientists ) The opportunity of ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" All you have to do is do some unnecessary complicated work on your code ."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" For example, in a general way , Reuse three lines of code in two places ? All you have to do is write a small function that takes five variables . A little more shrewd , You can also abbreviate these three lines of code into a precise three-tier nested ternary operation ! There's no limit to imagination , My friend !"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Of course , These will make the app "},{"type":"text","marks":[{"type":"strong"}],"text":" It's harder to read and harder to maintain "},{"type":"text","text":", But these burdens will probably only fall on your colleagues , Right ? That's great !"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Let's have a try , Use code to prove that you are the real hacker . I would suggest you try chained functions 、 Nested conditional statements 、 Over inflated design patterns , As well as the use of programming language in the unknown tips to write a subtle single line of code . If we could use the more mysterious "},{"type":"codeinline","content":[{"type":"text","text":"+new Date()"}]},{"type":"text","text":", So why use the old-fashioned "},{"type":"codeinline","content":[{"type":"text","text":"Date.now()"}]},{"type":"text","text":" Well ? I believe that when you and your project colleagues are studying what your code is doing , I will thank you very much !"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" please remember , The more sophisticated and prematurely optimized code , Your colleagues will be in a worse situation when they handle them . For every one you use reduce Function plus 10 points , Add a hundred points to each recursive call . At the end of the project , You will be a real programmer , come on. !"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Confused import"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/bc\/bc567df33728f12a7dc29b89a4064433.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":" Picture translation : I : Trying to explain the dependency hierarchy of the project "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" As we mentioned before, these tricks are likely to die in the code review stage , But if you're using JS or PSP Of web developer , So at the beginning of all your files, there will probably be a series of import or use sentence . Developers don't care about these things when they do reviews , They only care about the more predictable parts ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" That's why dependency is a good place to mess with code ."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Try to imagine , you are here React There is a call. "},{"type":"codeinline","content":[{"type":"text","text":"UserSubscriptions"}]},{"type":"text","text":" Of view, And then create a new one called "},{"type":"codeinline","content":[{"type":"text","text":"calculateTimeToSubscriptionEnd"}]},{"type":"text","text":" Of helper function . that , Now comes the question , You're going to put this helper Where are the functions ? In a separate file that holds all domain logic related to subscription or payment ? How boring . It's better to put it directly on the new one you just built view side !"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Then plan ahead and finally create the management panel and the "},{"type":"codeinline","content":[{"type":"text","text":"Subscription"}]},{"type":"text","text":" view The situation of , You can go straight from user Of view in import What do you want? helper function , After all, no one cares import list . Two unrelated modules mixed together , It takes a lot of effort for others to modify or refactor your code . believe me , What developers hate most is poorly structured projects ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" What did you say? ?“ It's not chaotic enough ”? Novice programmers are likely not to move your code in a short time , They will struggle in the mess you create , To be on the safe side, it's better to keep it as it is . And every time a new developer tries to understand your project structure , Will bring a new round of torture , When you update or delete something in development, you are import The things mentioned in , It's going to redouble the torture . A chaotic situation that is harmful to all but not beneficial to all , Only the brave can save the situation , Maybe you can try to be this warrior ?"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" The same function that runs differently "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/c3\/c3bdcdbe74434902f876480361c346d0.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Picture translation : male : I added a new user related node in the management interface , Will return the new data "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Woman : It will be like other user related nodes , Receiving user ID Right? ?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" male :( What you think? )"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Woman :…… It will be like other user related nodes , Receiving user ID Of , Right ?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Maybe you think you're creative , And very artistic people . Let me introduce to you , This new form of torture ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The world is changing all the time , No one says that everything has to be the same . No one has ever said that consistency and predictability are the key to good development experience and successful projects . Maybe someone really said , But we don't have to listen to them either ?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Our task is to make a function in the code base have a little bit of variability , To ruin someone else's Day ."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" for instance , A verification function , Data validation will return True, If the verification fails, a failure message will be returned . that , If the data is correct, the function returns False Or undefined , Your colleagues will probably have to write a few more cases to deal with your return , Isn't that beautiful ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Of course , You can also accept parameters of different shapes in all functions . Let's take the above diagram as an example , Let some functions accept the user ID, Others can only use users ID Accept the entire user object in the case of . Maybe you can also find some ways to accept users' e-mail addresses ? The guy who takes over your code will face hell ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" You can even add a little more element of surprise , Make everything unpredictable . Whatever the consistency , Long live randomness ! Of course , Don't worry about the whole code base anymore , How nice it is to change one file after another . What's the point of being an engineer , It's better to be an artist, happy and comfortable ! I'm sure your developers will hate you from the bottom of their hearts . But what's the use of that , You've got a long lead ."}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" Copy and paste the code everywhere "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https:\/\/static001.geekbang.org\/infoq\/0c\/0cf951d524cdf98d32e5b45ee03afb30.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":"center","origin":null},"content":[{"type":"text","text":" Picture translation : My code base : Copy and paste the same code from other files into different files "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" When you do that , I'm sure no one will want to work with you again ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Don't spread the same logic over different functions 、 class 、 Go to the module . Just copy and paste the few lines of code you need ."}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" After all , Your code is perfect and meaningful , It's enough for everyone to see different parts of the project over and over again . Let your code glow !"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" But you know , That's not why you copy the code crazily . In some updates , The system will require developers to change a large number of files at the same time . If the test scope is not enough , Some people may forget to delete repetitive logic once or twice , And had to start a second or even a third update . What's more interesting than searching for duplicate code in thousands of files ? Your colleagues will enjoy it ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Remember I said good abbreviations are hard and time-consuming ? So why don't we just copy the code where we need it ? otherwise , You'll probably have to spend more time explaining to others why you keep copying your previous code . I believe you , You can do it !"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":" summary "}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Obviously , This article is just for fun . Please don't try the operations I described in this article , It's not something that can “ Bet 50 cents ” A little joke ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Please do the opposite in practice :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Keep the naming clear in the code "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" The code should be easy to understand "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Keep a clean project structure "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Remember to use constants and predictable interfaces "}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Split the same logic while keeping the code clear "}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" And most importantly , Be nice to your colleagues !"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" There is a saying circulating in the development community , When you write code , Always be like a serial killer taking over your code . You don't want to be targeted by a serial killer , Isn't it ?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" And I think , You should be writing code , Imagine if the next person to take over the code is yourself , What do you think ."}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" When you're programming , Please be sure to ask yourself ,“ If I didn't remember what these programs were for a long time , Would I love to see the code ?”"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":" Link to the original text :"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https:\/\/tsh.io\/blog\/bad-coding-practices\/"}]}]}

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/07/20210719150921679H.html

随机推荐