TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How to Write Unmaintainable Code (2017)

196 pointsby migueldemouraalmost 7 years ago

37 comments

Yokohiiialmost 7 years ago
I actually like using short variable names. Using &quot;i&quot; as an index variable has been widely accepted. It saves a lot of typing, the use case is clear and it is usually tied the a block which is visually identifiable.<p>I carry over the same principles to functions&#x2F;methods. Functions are usually less than a screen which makes it rather easy to pick up the context and understand the program flow and the use of variables. I use &quot;list&quot; if there is there is a single list. The context usually defines what is in there. Maybe the function name, the type declarations or the fact that we are in a part of code handling lists of customers. How hard is it to guess what countCustomers(list) or incrementCustomersScore(list, n) is doing? Writing incrementCustomersScore(goodCustomerList, customerScoreDelta) is just redundant. Variable names just need to be descriptive enough to define their role within it&#x27;s context. If the function is very small, the variable names can be very short.<p>Local variables are the smallest named things in our systems, whith a very small scope, there shouldn&#x27;t be an excessive naming cult around it. Give advice how to properly name projects, packages, namespaces and classes and I start listening. We are really bad at this.
评论 #17782966 未加载
评论 #17782654 未加载
评论 #17782604 未加载
评论 #17783748 未加载
评论 #17782798 未加载
评论 #17785578 未加载
评论 #17786830 未加载
评论 #17784051 未加载
评论 #17784333 未加载
ppeetteerralmost 7 years ago
Any time I read C&#x2F;C++ code, I am under the impression it was written to be illegible.<p>I realize that with time, functions like `malloc` become second nature, but why not call it `allocate_memory`? (bash suffers from this too) It&#x27;s even worse when reading the source code of old video games due to the abstract nature of the operations being performed on virtual representations of visual elements.<p>The argument of time constraints when authoring doesn&#x27;t hold much value. Java is verbose AF but it&#x27;s also one of the most used languages. Second, most code is read, not written and should be written to be read by others, not just the author.<p>There may be a reason to be terse due to time constraints and lack of IDEs&#x2F;autocomplete or memory constraints or w&#x2F;e, but the need to shorten variables names and functions today only serves the whims of the author, not the 10+ future readers. <i>tinyrant</i> :D
评论 #17782830 未加载
评论 #17782463 未加载
评论 #17782491 未加载
评论 #17783759 未加载
wolfgang42almost 7 years ago
<i>&gt; The design doc should break the algorithm down into a hierarchy of structured steps, described in a hierarchy of auto-numbered individual paragraphs in the document.</i><p><i>&gt; then... (and this is the kicker) when you write the code, for each of these paragraphs you write a corresponding global function named:</i><p><i>&gt; Act1_2_4_6_3_13()</i><p><i>&gt; Do not document these functions. After all, that&#x27;s what the design document is for!</i><p>Ideally, your company will also have several file shares into which old and unidentifiable documents are filed, in nested layers of folders named things like &quot;Old documents&quot;, &quot;John&#x27;s Code&quot;, &quot;Please Clean Up&quot;, and &quot;Maybe Delete&quot;. However, for maximum effectiveness do not file your design documents here! Instead, put various old drafts (not identified as such, of course) in a folder on the server where your code <i>used</i> to run, somewhere like C:\backups\aurdbcl1\C\Documents and Settings\users\jdoe\Desktop\ (where aurdbcl1 is the name of the server where your program ran 20 years ago). This will ensure that they will spend as long of a time as possible hunting for something that could be useful. If possible, also ensure that there are some multi-Gigabyte zip files scattered about which have to be unpacked just in case there&#x27;s something useful inside.<p>A related technique for compiled languages is to arrange for various copies of the source code to be copied into similarly diverse locations. Do not use version control under any circumstances; if you must, check in at most once a month with the comment &quot;changed code&quot;. This works best for a program which is maintained very rarely. Anyone attempting to fix it will first have to identify which copy of the source code is the right one. Make sure you also have some copies that never made it to production, where you started changing things and never finished. If possible, the production version should also be subtly different from <i>all</i> copies of the source code that can be found.<p>(The above is, unfortunately, from experience. I have many other rants which similarly belong in this document, particularly around exception handling and copy-pating, but I feel that this comment is becoming too long already.)
评论 #17783133 未加载
maerF0x0almost 7 years ago
Disclaimer: This comment is mostly in jest.<p>For a second there I thought i was reading about golang[1].<p>* Single letter var names[2] &#x2F; [3]<p>* Underscores [4] &#x2F; [5]<p>* extended ascii code [6] &#x2F; [7]<p>[1]<a href="https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;effective_go.html" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;effective_go.html</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#single-letter-variable-names" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#single-lette...</a> [3] <a href="https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;wiki&#x2F;CodeReviewComments#variable-names" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golang&#x2F;go&#x2F;wiki&#x2F;CodeReviewComments#variabl...</a><p>[4] <a href="https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#underscore-a-friend-indeed" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#underscore-a...</a><p>[5] <a href="https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;effective_go.html#blank" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;doc&#x2F;effective_go.html#blank</a><p>[6] <a href="https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#extended-ascii" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#extended-asc...</a><p>[7] <a href="https:&#x2F;&#x2F;golang.org&#x2F;ref&#x2F;spec#Source_code_representation" rel="nofollow">https:&#x2F;&#x2F;golang.org&#x2F;ref&#x2F;spec#Source_code_representation</a>
评论 #17784948 未加载
评论 #17784670 未加载
newsbinatoralmost 7 years ago
&gt; Consider this real world example: a_crszkvc30LastNameCol.<p>&gt; It took a team of maintenance engineers nearly 3 days to figure out that this whopper variable name described a const, reference, function argument that was holding information from a database column of type Varchar[30] named &quot;LastName&quot; which was part of the table&#x27;s primary key.
评论 #17782451 未加载
评论 #17782250 未加载
ssethalmost 7 years ago
I had a colleague who used to use variable names from his local language in India (we were working in Europe at the time). So hwndParent would become hwndBaap. This combines both the hungarian and the local language strategies for job security.<p>Fortunately, he stopped programming and became very successful in agriculture. Good outcome for both fields.
评论 #17783015 未加载
nathellalmost 7 years ago
This is much older than 2017; the version at <a href="http:&#x2F;&#x2F;mindprod.com&#x2F;jgloss&#x2F;unmain.html" rel="nofollow">http:&#x2F;&#x2F;mindprod.com&#x2F;jgloss&#x2F;unmain.html</a> dates back to 2000.
jaclazalmost 7 years ago
As a side note, the &quot;opening&quot; quote attributed to Napoleon, seemingly isn&#x27;t:<p><a href="https:&#x2F;&#x2F;quoteinvestigator.com&#x2F;2016&#x2F;12&#x2F;30&#x2F;not-malice&#x2F;" rel="nofollow">https:&#x2F;&#x2F;quoteinvestigator.com&#x2F;2016&#x2F;12&#x2F;30&#x2F;not-malice&#x2F;</a><p>I can offer as corollary to the article a couple of unmaintainable simple batches:<p><a href="http:&#x2F;&#x2F;reboot.pro&#x2F;topic&#x2F;2986-yacbfc-dec2hexcmd-and-hex2deccmd&#x2F;?p=204425" rel="nofollow">http:&#x2F;&#x2F;reboot.pro&#x2F;topic&#x2F;2986-yacbfc-dec2hexcmd-and-hex2deccm...</a>
评论 #17782215 未加载
评论 #17782111 未加载
bausshfalmost 7 years ago
I have an addition to writing unmaintainable code.<p>Only have one struct with a member of every type. That one struct will be the type of every variable you have.<p>If the language you use support implicit operator overload abuse them to make sure all types can be implicitly converted to each other.<p>Ex.<p>struct Variable { byte a; short c; int d; long e;<p><pre><code> ...</code></pre> }<p>This will make it impossible to know what type a variable is until you find its assignment, which of course isn&#x27;t on the same line as the declaration and even better reuse the same variable for multiple values.
评论 #17782841 未加载
评论 #17783045 未加载
joeblaualmost 7 years ago
I had a funny now&#x2F;frustrating then story where I worked with his guy who named five variables in a class a, aa, aaa, aaaa, aaaaa. I was trying to refactor his code and I had no idea what anything did. The company is out of business now, but it was definitely unmaintainable.
alexeizalmost 7 years ago
Recently I came across some code that checks quite a few boxes in this guide. Check it out: <a href="https:&#x2F;&#x2F;github.com&#x2F;KxSystems&#x2F;kdb&#x2F;blob&#x2F;master&#x2F;c&#x2F;c&#x2F;k.h" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;KxSystems&#x2F;kdb&#x2F;blob&#x2F;master&#x2F;c&#x2F;c&#x2F;k.h</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;KxSystems&#x2F;kdb&#x2F;blob&#x2F;master&#x2F;c&#x2F;c&#x2F;odbc.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;KxSystems&#x2F;kdb&#x2F;blob&#x2F;master&#x2F;c&#x2F;c&#x2F;odbc.c</a>
评论 #17783614 未加载
ajssalmost 7 years ago
Along a similar line, I love Stuart Halloway&#x27;s talk on Narcissistic Design - <a href="https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LEZv-kQUSi4" rel="nofollow">https:&#x2F;&#x2F;www.youtube.com&#x2F;watch?v=LEZv-kQUSi4</a><p>Prefer APIs over data.<p>Start with DSLs.<p>Always connect (and never enqueue).<p>Create abstractions for information.<p>Use static typing across subsystem boundaries.<p>Put language semantics on the wire.<p>Write lots of unit tests.<p>Leverage context.<p>Update information in place.
paulddraperalmost 7 years ago
&gt; a naming convention from the world of C++ is the use of m_ in front of members. This is supposed to help you tell them apart from methods, so long as you forget that &quot;method&quot; also starts with the letter &quot;m&quot;.<p>C++ has member variables (which this refers to) and member functions.<p>&quot;Method&quot; is not part of the vernacular.
phakdingalmost 7 years ago
What incenses me is this recent notion that because programmers fail to change comment with the code change, they shouldn&#x27;t even write comments. They should instead write the self-documenting code.<p>This doesn&#x27;t make sense on so many levels. Only a recluse sitting in some dungeon who never dealt with people can come up with something so inane.<p>Edit:<p>I understand that that every developer should aspire to become someone who writes code that reads like a children&#x27;s book, and they should. However, actively discouraging writing comments is so wrong on so many levels.<p>At the last place I worked, we used to have a small at-home coding test before interview and none of the young developers who successfully completed the test did any sort of code documentation or commenting. When I asked why, they would tell me that their code is self-documenting, which was utter nonsense.
评论 #17782913 未加载
评论 #17826920 未加载
评论 #17782877 未加载
pure-awesomealmost 7 years ago
&gt; Since global variables are &quot;evil&quot;, define a structure to hold all the things you&#x27;d put in globals. Call it something clever like EverythingYoullEverNeed. Make all functions take a pointer to this structure (call it handle to confuse things more). This gives the impression that you&#x27;re not using global variables, you&#x27;re accessing everything through a &quot;handle&quot;. Then declare one statically so that all the code is using the same copy anyway.<p>I see this in practice WAY too often... Heck, I&#x27;ve been somewhat guilty of it myself before.<p>(Bonus points if it&#x27;s in a functional language :P )
评论 #17783551 未加载
zbentleyalmost 7 years ago
&gt; In C, functions are supposed to be idempotent, (without side effects).<p>What is meant by that statement? Is &quot;idempotent&quot; a mistype&#x2F;mistakenly selected word? If the author meant &quot;pure&quot; instead, should I file a PR, or consider this some sort of meta-meta-obfuscation? If the author meant &quot;pure&quot;, is that actually true of C specifically, or just generally good programming advice (localize side effects to known locations, the fewer the better, don&#x27;t sprinkle them all over and document where they occur)?
评论 #17782467 未加载
评论 #17784609 未加载
评论 #17790079 未加载
评论 #17785196 未加载
retSavaalmost 7 years ago
Well, since the repo more or less took the contents verbatim from the original author (unless they are the same), and here presents it _more_ clearly than the original, I&#x27;d say the repo owner failed.<p>At the very least it should be presented in a more unmaintainable way.<p>Edit, apparently repo owner already checked with author: <a href="https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code&#x2F;issues&#x2F;3" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code&#x2F;issues&#x2F;3</a> :+1:
评论 #17782106 未加载
coolaliasbroalmost 7 years ago
As a creative process, and in the interest of compassion, I tend to name everything as humanly readable as possible, and provide voluminous documentation in production systems. Even quick-and-dirty things tend to get moved to production if they work, so I increasingly apply the same approach to them, as the &quot;this will be tossed after x&quot; sentiment has bitten me on the ass more than once.
评论 #17782850 未加载
schindlabuaalmost 7 years ago
Ok so sometimes I have no concept of what american and british variants of a word are because I&#x27;m from mainland europe and so I will just use whatever. Sorry maintainers!<p>Grey&#x2F;Gray gets me every time.
评论 #17784356 未加载
评论 #17784398 未加载
Sohcahtoa82almost 7 years ago
I&#x27;d add something about breaking common conventions like moving the verb to the end of the function name. For example, I&#x27;d change GetPageFromServer() to PageFromServerGet()<p>Also, use synonyms for common function verbs. Like Grab instead of Get. GrabPageFromServer()<p>In a code base I dealt with, a developer did exactly those. Most &quot;get&quot; functions had &quot;get&quot; at the end of the function name. Some functions had it at the beginning, but replaced with &quot;grab&quot;.<p>Also, he misspelled a class name and just went with it.
bausshfalmost 7 years ago
&quot;Hungarian Notation is the tactical nuclear weapon of source code obfuscation techniques; use it!&quot; - I&#x27;m laughing so hard. This whole article is genius.
评论 #17782350 未加载
rajadigopulaalmost 7 years ago
A much novel way - use functional programming and create higher order functions and purely mathematical combinatoric functions and compose them in a complex way. A normal day developer must go to college again to even getting closer to understand it. This way the code is highly functional and there won&#x27;t be a compromise in the quality of the output!
评论 #17783322 未加载
ape4almost 7 years ago
I inherited some code with m_ prefixes on parameters to a function. eg theFunction(int m_param1) That should be a lint error.
nerdponxalmost 7 years ago
How is this any &quot;easier to share&quot;?
评论 #17782530 未加载
jjuhlalmost 7 years ago
There are 2 hard problems in computer science: cache invalidation, naming things, and off-by-1 errors.
gerdesjalmost 7 years ago
Don&#x27;t bother with all that documentation, get straight into the Stackoverflow example:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#a-real-life-example" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Droogans&#x2F;unmaintainable-code#a-real-life-...</a>
roland35almost 7 years ago
For embedded C work I do like to mark global variables with a Hungarian g_, all other prefixes are generally handled pretty well by the IDE&#x27;s type hinting. Other than that I agree Hungarian can go!
a_imhoalmost 7 years ago
Surprisingly light on multithreading and version control best practices.
emodendroketalmost 7 years ago
The comment section is particularly trenchant because I recently worked on a project that had hundreds of instances of the comment &quot;&#x2F;&#x2F; do this here&quot;
S-E-Palmost 7 years ago
My personal trick: Write a basic (not turing complete) brainf<i></i>k compiler into your application and feed your business logic through that.
评论 #17826952 未加载
mirekrusinalmost 7 years ago
One of the longest jokes I&#x27;ve seen.
tegansnyderalmost 7 years ago
Now whenever someone criticizes my old code I can point them to this and say it could be worse.
jghalmost 7 years ago
hahah so glad Hungarian notation was included.
Mc_Big_Galmost 7 years ago
Just use perl.
评论 #17787847 未加载
cessoralmost 7 years ago
Many people are pointing out that short identifier names are ok, and that i is perfectly fine as an identifier name. It likely is, and I believe the rule to be true that short identifier names are ok for short scope &#x2F; context. However, there are several issues:<p>i for an index is ok, but this is a red herring: You only ever need an i for imperative iteration and in a sufficiently high level language you are better of with selection and projection, (iteration for that matter), where you just foreach over each element and don&#x27;t actually need to ask for things by index. Sure, y&#x27;all be hacking C, nothing wrong with that, it depends on what you str doing, but for comprehension, the mere existence of i is a red flag, because most of the time you want something to happen to a collection if things and foreach is perfect for that (or map, fold, filter, pick your poison).<p>Still, a better example than `i` for short identifier names would be x,y,z, or r,g,b These are so common and conventional that I would expect them to have semantics of their own, still they could be chunked to types such as Coordniate, Location, or Point, or Paint, or Color, respectively. But comming up for better names than x,y,z is really difficult, will bloat your code and make it hard to understand what is going on.<p>I am pointing this all out, because many of you seem to favor such very short identifier names - and I am with you: sometimes they make sense, see above - but empirical evidence suggests that very short identifier names, and single-letter identifier names in particular significantly worsen comprehension of code. The same is true for abbreviations (depending on how strongly you abbreviate).<p>[1] <a href="https:&#x2F;&#x2F;ieeexplore.ieee.org&#x2F;document&#x2F;7884623&#x2F;" rel="nofollow">https:&#x2F;&#x2F;ieeexplore.ieee.org&#x2F;document&#x2F;7884623&#x2F;</a> (disclosure: author)<p>[2] <a href="https:&#x2F;&#x2F;doi.org&#x2F;10.1007&#x2F;s11334-007-0031-2" rel="nofollow">https:&#x2F;&#x2F;doi.org&#x2F;10.1007&#x2F;s11334-007-0031-2</a><p>[3] <a href="http:&#x2F;&#x2F;compscinet.dcs.kcl.ac.uk&#x2F;JP&#x2F;jp040302.abs.html" rel="nofollow">http:&#x2F;&#x2F;compscinet.dcs.kcl.ac.uk&#x2F;JP&#x2F;jp040302.abs.html</a><p>[4] <a href="https:&#x2F;&#x2F;dl.acm.org&#x2F;citation.cfm?doid=382080.382628" rel="nofollow">https:&#x2F;&#x2F;dl.acm.org&#x2F;citation.cfm?doid=382080.382628</a><p>[5] <a href="https:&#x2F;&#x2F;link.springer.com&#x2F;article&#x2F;10.1007%2Fs10664-018-9621-x" rel="nofollow">https:&#x2F;&#x2F;link.springer.com&#x2F;article&#x2F;10.1007%2Fs10664-018-9621-...</a><p>Preprint for [1] here: <a href="https:&#x2F;&#x2F;brains-on-code.github.io&#x2F;shorter-identifier-names.pdf" rel="nofollow">https:&#x2F;&#x2F;brains-on-code.github.io&#x2F;shorter-identifier-names.pd...</a><p>[5] is the extended Version of [1] including discussion of how attention is shifted during the experiment. It turns out that, when code uses words as identifier names people don&#x27;t need to read the comments as much.
tribesmanalmost 7 years ago
&quot;Hungarian Notation is the tactical flesh light of source code obfuscation offering max pleasure; use it!&quot;
hashratealmost 7 years ago
This is suppose to be funny , unfortunately this is the sad reality of what you&#x27;ll find in 90% of Enterprise &#x2F; Startups environment as well as THE majority of open source repositories of popular open source projects.<p>I&#x27;ve noticed this especially true for JavaScript and Go.<p>In JavaScript , because the language is still evolving and there is no real standard about how to architect code with this language unless you are using a entire &quot;platform&quot; like Angular &#x2F; Vue &#x2F; React witch will highly influence your coding style.<p>In Go because Go developers are performances obsessed , coming from C&#x2F;C++ background and do believe that naming their variable with one letter will save 1GB of memory allocation , 1000 CPU Cycles and make their code &quot;Clear and Concise&quot;<p>Ultimately , because there is very little standard in this industry in terms of code governance and a large percentage of projects are outsourced to other companies , we end up with what is described in this repository.
评论 #17782300 未加载
评论 #17782219 未加载
评论 #17782209 未加载