Live data from Hacker News

How to name things in programming

slideshare.net

161–170 of 177 posts

Re: How to name things in programming

#161
post #160
post #124

Earlier quoted context omitted.

Your argument is not an argument against comments, it's against any kind of description. Your code works just as well if you assigned each function a unique number starting from 'func0' - that's how code obfuscators or minifiers work. Unless the act of shoving comments into function names makes the compiler type-check them, you are still writing something that the compiler will ignore eventually.

I think his argument is: "Comments is not a substitute for short functions and javadoc". That sounds extremely valid, imho.

If that was his argument I'd be happier with it. As described, his goal was to encode comments in the function name and describe caveats in test cases; none of those steps sound like 'write a javadoc'.

Javadoc and comments are both forms of code documentation. Perhaps we can generalise OP's point to the following:

"Most of the things programmers say about documentation of code are excuses for not writing any documentation at all"

I'm not going to use a library without documentation in favour of one that is documented. Above a certain minimum standard, it doesn't matter how 'clean' the code is if you don't understand why it exists. Cute naming simply doesn't make up for a lack of documentation, and avoiding documentation should be considered a cone of shame, not a badge of honour.

Re: How to name things in programming

#162

Earlier quoted context omitted.

I think the thing is in addition to all instances of the variable called a your search will also stop on any word that contains a. Now of course you can search for " a " in whatever way your editor prefer but programmers who write such variable names might easily write a=10 one time and a = 10 the next and before you know it you have an interesting regex there in your search. I think he was contrasting it to an ide t…

"I think the thing is in addition to all instances of the variable called a your search will also stop on any word that contains a." Yes, that's what was being said. But it won't. In vi (and friends), \ is "end of word", so \ will not match the a in "absolute" or "tundra" or "fabulous", but will match the a in "(3+a)" and "a=7". Certainly there are text editors that don't let you express this, but they're so broken a…

If all the variables are 'a' then how do you find the one that's yours?

Re: How to name things in programming

#163

Earlier quoted context omitted.

Might be an outlier, IMO. And I guess this is exactly why author wants programmers to learn from the masters of spoken langauges-- ability to use the right word(s) to explain the concept/abstraction/property/behaviour/type. If one is unsure about how something works, then more often than not one would end up naming the thing wrong. By far and large, the most frustrating thing I've come across during code-reviews is g…

What does "Make life of" mean? As a native English speaker, I don't understand it; I presume it means "Make life better for", though the closest we have in English is probably "Make light of", which means something completely different (and is slightly funny in this context).

Sorry, English isn't my first language, or second.

Here's a link to the talk: https://youtu.be/bo36MrBfTk4?t=3446

Re: How to name things in programming

#164
post #143

Earlier quoted context omitted.

pairwise . reverse . map (+1) . flatten . replicate 2 . map (*3) $ input With all the functions being pretty standard (pairwise is my own name for subdivideListEtc, but it seems like a terse name that still explains what the operation is). Every fragment in this composition is smaller than the names you supplied, and all but one of them are standard Prelude functions that should be considered basic knowledge in Haske…

More elegant, more concise but less understandable. This is subjective but I would argue it's harder to get a higher level understanding of what your code is doing versus what mine is. Also, honestly, nobody will know what "pairwise" is without looking at the definition; I mean the name is appropriate and elegant, but that doesn't mean it's clear. Here's another perspective: My code, despite having several complicate…

I think both versions of the code need to be wrapped with an appropriate name. That name is necessarily more functional, as too much is happening to describe technically. After being named, that name would be sufficient for most readers. For those for whom it isn't sufficient, who are interested in the implementation, it can go two ways: either they like that your version explains the algorithm in words, or they dislike it for introducing an extra layer of indirection around 'trivial' function calls, while they are interested in the actual implementation.

I think your expanded example may be a bit of a special case, because it is at the level where for anything 'larger', technical names would be unwieldy or imprecise, while for anything smaller, abstraction would basically be aliasing. I think most commenters, or I at least, were erring towards the interpreting the original example as one of the first category. The large example shows how it could be appropriate.

Re: How to name things in programming

#165
post #162

Earlier quoted context omitted.

"I think the thing is in addition to all instances of the variable called a your search will also stop on any word that contains a." Yes, that's what was being said. But it won't. In vi (and friends), \ is "end of word", so \ will not match the a in "absolute" or "tundra" or "fabulous", but will match the a in "(3+a)" and "a=7". Certainly there are text editors that don't let you express this, but they're so broken a…

If all the variables are 'a' then how do you find the one that's yours?

That's why an editor that actually understands the language can do better.

That said, this is also where the scope consideration comes into play. If something is used in 100 different lines, 300 lines apart across 3 files, then of course naming it 'a' is crazy. If something is only in scope over 5 or 10 lines, then you simply restrict your search to those lines (either explicitly with marks, line numbers, or pattern bounds; or implicitly by exiting the search when it gets to a region you don't care about - find-and-replace in vim has a "confirm" option).

Re: How to name things in programming

#166
post #143

Earlier quoted context omitted.

pairwise . reverse . map (+1) . flatten . replicate 2 . map (*3) $ input With all the functions being pretty standard (pairwise is my own name for subdivideListEtc, but it seems like a terse name that still explains what the operation is). Every fragment in this composition is smaller than the names you supplied, and all but one of them are standard Prelude functions that should be considered basic knowledge in Haske…

More elegant, more concise but less understandable. This is subjective but I would argue it's harder to get a higher level understanding of what your code is doing versus what mine is. Also, honestly, nobody will know what "pairwise" is without looking at the definition; I mean the name is appropriate and elegant, but that doesn't mean it's clear. Here's another perspective: My code, despite having several complicate…

I disagree on the understandable part.

But another point is that it is much easier to have a typo that compiles in code than in variable names.

Re: How to name things in programming

#167

Slide 41: "If you don't know what a thing should be called, you cannot know what it is. If you don't know what it is, you cannot sit down and write the code." A few years ago, I programmed in Max/MSP (max4live, technically) for a while and one thing I found super-refreshing is the ease at which you could prototype and experiment your way to a solution just by not having to figure out what something should be called u…

As long as you have good refactoring tools (and aren't doing much stringly-typed reflection/XML based crap), renaming things as the design evolves isn't that difficult.

Typically when I'm doing something new, it starts as a pretty free-form prototype, which gets progressively refined as I figure out what in the hell I'm actually doing, and start pulling components and subfunctions out of the original spaghetti ball.

Re: How to name things in programming

#168

Earlier quoted context omitted.

An editor probably not know this but an IDE would. The IDE actually partially understands your code. If your function definition was (in python): def nocars but you called the function with: n0cars() The ide would highlight n0cars because it could not find the definition. The IDE literally destroys the possibility of making trivial mistakes like this in a non-compiled language. Of course this depends on the ide. I us…

I was referring to a variable not a function and as you say IDE's can have heavy over head not every one can afford a full copy of visual studio or a system powerful enough to run eclipse

Visual Studio Community Edition, there you go -> https://www.visualstudio.com/en-us/products/visual-studio-co...

Agreed Eclipse is a hog that should be lead to the slaughter. But an acceptable dev machine can be had for $250 at Best Buy.

Re: How to name things in programming

#169

Earlier quoted context omitted.

I try to pick words with different lengths. They're more visually distinguishable.

And try to pick words that are lexicographically distant from each other to minimize the chance of single letter typos changing the meaning of the code.

Of course it's a tiny matter of terminology, but you probably mean Hamming distant, not lexicographically distant. `zaaaaa` is lexicographically farther from `aaaaaa` than is `bbbbbb`, but Hamming closer.

Re: How to name things in programming

#170

Earlier quoted context omitted.

Functional programming languages use single-letter parameter names as a tool to help condense functions and allow easy parsing. This is largely due to their desire to emulate mathematical formulae. Interestingly, functional programming leads to a different naming custom that this presentation didn't really cover. To write "functionally" you want to have pure single-purpose functions and the name is supposed to descri…

Actual mathematical functions have some of the most useless, non-descriptive or vaguely metaphorical names going, so I'm not sure promoting an isomorophism with FP is a good thing. Examples: "bessel", "spherical_harmonic", "delta", "zeta"... Math is hard, and the terseness of mathematical notational conventions is one of the things that makes it so. Names that carry meaning with them are an aid to the naive user, and…

> Secondly, naming a function "json" requires the user read the code or the comments to figure out what it does, and blocks the user of the name for anything else that returns json. Suppose rather than a string you have a list that you need translated to json... what do you name the function?

`json` --polymorphic languages make naming easy!

Post reply on HN