from Hacker News

How to Write Unmaintainable Code – Naming

by dhatch387 on 5/26/15, 3:02 AM with 42 comments

  • by yonran on 5/26/15, 5:26 AM

    When you need to prevent quick grepping in Java, you can also take advantage of the fact that unicode escapes can occur everywhere in a java file, not only within quotes. So you can declare an int by writing “\u0069\u006e\u0074 \u0069 = 1;” (equivalent to “int i = 1;”)
  • by rosser on 5/26/15, 5:54 AM

    I once worked on a project where every class was named after a martial arts weapon. As cute as that might have seemed, I quickly grew tired of asking questions like, "Should I be using the Shuriken or the Wazikashi here?"

    We ended up scrapping that code and doing a ground-up rewrite.

  • by zyxley on 5/26/15, 6:47 AM

    If working in PHP, make sure to use lots of variable variables (https://php.net/manual/en/language.variables.variable.php).

      $a = 'cat';
      $$a = 'hello';
    
      echo "$a"; // "cat"
      echo "$cat"; // "hello";
  • by fezz on 5/26/15, 5:25 AM

    Use hipster baby names if your code will be seen by hipsters: http://nameberry.com/list/264/Hipster-Baby-Names
  • by billforsternz on 5/26/15, 5:18 AM

    Also; Always name binary variables to avoid hinting to the maintenance programmer which way round they work. Good: result, status, flag. Bad: okay, error, valid.
  • by x0054 on 5/26/15, 4:48 AM

    OP forgot an important tool: store as many variable as possible in one long array. When ever possible use predefined constants like "one," "first," and "superVar" to refer to the index of the desired value in the Array, make sure that all of the above mentioned constants have the same value and are defined in different parts of the code.
  • by bdcravens on 5/26/15, 6:14 AM

    Remember with unicode support for variable names you can use emoji:

    http://stackoverflow.com/questions/29696768/which-emoji-can-...

  • by MichaelGG on 5/26/15, 8:41 AM

    Short variable names are fine with context. "cs : Customer list" - we know it's a list of customers from the type, no need to repeat. And a loop on it could easily be "for c in cs". Single letters are also useful in local or anonymous functions.

    Reuse identifiers? Yes! If I've got a parameter foo that's a string, then I convert it into an int and never use the string again, why not rebind the identifier? It makes it clear the old foo is no longer in use, and prevents such use.

  • by mercurial on 5/26/15, 5:09 AM

    In the interest of fairness, and in regards to the "Underscore, a friend indeed", I'll point to the existence of eg, $_ and @_ as built-in Perl magic variables (you can get away with not using $_, but not @_). I have also been know to use _ for a variable I will not use later (eg, if a function returns two values but I'm only interested in one).

    Additionally, in languages with pattern matching, '_' usually works like '*' and matches everything.

  • by FrankenPC on 5/26/15, 6:03 AM

    Number 11 is unbelievably devious. I wonder how many compilers are OK with that?

    ----------------------------------------------- Use accented characters on variable names, e. g. --typedef struct { int i; } ínt; where the second ínt’s í is actually i-acute. With only a simple text editor, it’s nearly impossible to distinguish the slant of the accent mark.

  • by jmpeax on 5/26/15, 5:19 AM

    "If anyone even hints at breaking the tradition honoured since FØRTRAN of using i, j and k for indexing variables, namely replacing them with ii, jj and kk, warn them about what the Spanish Inquisition did to heretics."

    Hold on, should we use i instead of ii, or not use it? I fucking hate these double-negative articles.

  • by BinaryIdiot on 5/26/15, 5:11 AM

    My favorite way of doing naming in JavaScript is using ಠ_ಠ as a variable (especially if I need to create some sort of quick "hack" to make some sort of urgent issue go away).

    Also fun is using reserved JavaScript keyboards inside of JavaScript objects which, while legal, rarely color correctly in code editors.

  • by rodgerd on 5/26/15, 5:21 AM

    The broader site is like window into an Internet largely long-gone, like digging out Usenet postings.
  • by spdionis on 5/26/15, 10:30 AM

    When you access arrays by index do not use magic numbers. Define FIRST = 0 and SECOND = 1 for example.
  • by reacweb on 5/26/15, 11:02 AM

    When you fix a function, leave the broken function still present with another name.
  • by rogeryu on 5/26/15, 9:56 AM

    Please don't forget to use multiple properly named symlinks here and there.
  • by gargalatas on 5/26/15, 12:06 PM

    Just for the history, i stands for the (i)terator and not (i)ndex..
  • by wirrbel on 5/26/15, 5:37 AM

    another great way of making code unmaintainable:

    Either with or without the underscore: Characters are for free, why don't you turn your variable name into a short story?

  • by alreadyAlready on 5/26/15, 5:54 AM

    Hmmm... nothing about XML... pity.
  • by newman8r on 5/26/15, 6:15 AM

    val lyricsFromAshittyRapSongYouNeverHeardOf = true