Live data from Hacker News

How to name things in programming

slideshare.net

61–70 of 177 posts

Re: How to name things in programming

#61

When structuring code, elegance and efficiency always take top priority. However, when naming, my philosophy is always clarity over elegance. for example (in python): def reverseListAndAddOneToEachElement(input_list): Ugly, but clear. I don't even need to write out the full definition you already know exactly what it does.

CamelCase for functions in Python? Why.

Re: How to name things in programming

#62
This seems like its trying to force advice for one domain to another when its not appropriate.

I disagree strongly with one or two of the rules. But an even bigger problem is that some (or even most) of the advice either has little value or doesn't really apply to programming. In english, shorter is better, but I'd much rather a longer class name that I can understand than one that's abbreviated to the point where I am forced to look up what its doing. A good rule of thumb that I try to follow is that for the most part, your code should be self explanatory (comments should cover the rest). We have these rules in programming for how to write variable names, and they're better than the ones in that slide. Another rule, that trumps any other is consistency. If you're working on a project that uses the passive voice (which I think works better for some cases) then use that. The nature of programming is different than natural language writing, and so it makes more sense that these rules that are trying to be transferred over are ill equipped to do so.

I mentioned that some of the things just don't make sense As an example, this was one of the slides, "when writing a novel a writer should create living people; people not characters. A character is a caricature." What does this mean? Its ironic that the slide preaches being short and to the point, and yet is bloated with abstract fluff.

Re: How to name things in programming

#63
The author states:

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

This is true. Here is my excuse:

1. Follow all rules for writing good comments 2. Comment is now short, crisp and concise 3. Apply "Extract Function" refactoring, use comment as function name 4. Your comment is a compiled entity now 5. Write testcases for the function that explicitly explain corner cases and weird behavior 5. You have no comments in the code any more

This rule can be applied almost all the time, although it can be quite hard to use when global variables or wide scopes are used (e.g. 1000 line for-loops in C). Comments are either obvious or they're lying. Why would you write something that the compiler will ignore eventually?

Re: How to name things in programming

#64
post #58
post #56

Earlier quoted context omitted.

Hopefully a function that can be corectly named.

What do you think is a better name? reverseListAndAddOneToEachElement is describing the function but I agree it feels ugly.

For this level of grouping, I'd probably prefer something related to the actual domain.

    def convertDF4ToMX6(data):
        # The MX6 format is read as a stack rather
        # than a queue, and requires data to be 1
        # indexed rather than 0 indexed like DF4
        # link_to_MX6 format spec 1.3.2 (v5)
        # link_to_DF6 format spec 1.2.0 (v1)
Why are we reversing and adding one? What's the reason we need to do that so much that we're combining those two functions into a single call?

This would then mean the calling points might look something like

    mx6_data = convertDF4ToMX6(loadDF4(file))
    return processMX6(mx6_data)
rather than

    mx6_data = reverseAndAddOneToEachElement(loadDF4(file))
    return processMX6(mx6_data)
Rather contrived, I know.

Re: How to name things in programming

#65
post #63

The author states: "Most of the things programmers say about comments in code are excuses for not writing any comments at all" This is true. Here is my excuse: 1. Follow all rules for writing good comments 2. Comment is now short, crisp and concise 3. Apply "Extract Function" refactoring, use comment as function name 4. Your comment is a compiled entity now 5. Write testcases for the function that explicitly explain…

Design decisions and reasons for why you wrote the code the way you did are hard to encapsulate in test cases. Describing the limitations of the obvious method which made you use a less obvious method is pretty instructive for the next person left scratching their head at your implementation.

Re: How to name things in programming

#66
Nice article. I loved the hieroglyph page; I think that functional programming failed for years as an engineering discipline because of the culture of that style of code.

My early days of OOP, I started keeping a thesaurus on my bookshelf at work. Probably went overboard on naming for a while, but reigned it in. (Every writer shudders at their first efforts. Hoo boy).

We would also hold short discussions about names for things (I don't see this in the article's list of tools: Involve other people in the names you are choosing. After all, they'll be using the code, too).

Re: How to name things in programming

#67
post #7

When structuring code, elegance and efficiency always take top priority. However, when naming, my philosophy is always clarity over elegance. for example (in python): def reverseListAndAddOneToEachElement(input_list): Ugly, but clear. I don't even need to write out the full definition you already know exactly what it does.

I like the logic of this, but it goes against all books that i've read to keep it short. Why do we need to keep it short?

Easier to keep short names in programmers memory and less chance of spelling mistakes.

Re: How to name things in programming

#68

An important rule is missing here: variables should be named with their scope in mind. So if a variable is longer lived and has larger scope its name should be that much more descriptive because when you're looking at it the only thing that will tie the value of the variable to the context within which it can be used is its name. So 'i' is fine for a loop control variable with a scope of five lines but totally inadeq…

continuing with the anecdotes:

i heard a tale of some graph layout code that ended up as part of a Microsoft product - all of the things were named after birds.

Post reply on HN