News for hackers. Stuff that matters.
Re:Golden rules for becoming a better programmer by davnicwil on Tuesday January 26, 2016 @08:46PM (#10977665)
> 4. No literals

Generally I agree with you here, but I think what the post is getting at is more the one case where you do want a variable in place of a literal - when passing the literal into a function where its purpose isn't immediately obvious. Passing the literal behind a more meaningful variable name can provide cleaner, self-documenting code in cases where, for example, modifying the function isn't possible and writing an adaptor would be overkill.

Re:Golden rules for becoming a better programmer by akkartik on Tuesday January 26, 2016 @09:23PM (#10977798)
The second-most egregious pattern I see at work:

  const char* kEmpty = "";
Makes me want to poke my eyes out with a stick everytime I see kEmpty used..

(The most egregious thing is spaces inside parens. But that's just a personal preference. In general I tend to be quite laissez-faire about tolerating different styles, and consider style guides to be a pox because they tend to give the subliminal message that your code is done when it complies with the style guide.)

Re:Golden rules for becoming a better programmer by davnicwil on Wednesday January 27, 2016 @06:13AM (#10979453)
I'm 100% with you, the variable name kEmpty here is completely tautological - it just describes the literal in a different, clunkier way, and doesn't add any information. Kind of similar to something like nameOfSteve = "steve"

The case where it's useful is where instead you'd call the variable referring to a constant something like dollarCurrencyCode = "USD", where you have to use an api which takes a currency code but you will always pass "USD". To the uninitiated reader, what purpose does the literal "USD" serve - what does it mean? It's not clear, because they don't know the api, and may not intuit currency codes. Putting it behind the dollarCurrencyCode variable name makes it a lot clearer on first glance. If they care to know what the dollarCurrencyCode is 'under the hood', they can then look, but this is a lower level detail of the underlying api and doesn't help with understanding.

Re:Golden rules for becoming a better programmer by imron on Wednesday January 27, 2016 @12:19AM (#10978381)
You mean like this: call_function( some_argument );

If so, I have to disagree strongly. Having the spaces inside the parens makes it far easier to scan the code.

Re:Golden rules for becoming a better programmer by rhinoceraptor on Wednesday January 27, 2016 @09:13AM (#10980041)
I always prefer to use literals in tests, I had the case once where I was checking that foo was equal to bar. In reality, both were null, so the test passed.