Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended word order of names? [closed]

From my perspective there can be (at least) two aproaches how to create a name for a variable, function, class etc.

  1. Structured:

    history_clear()
    history_save_to_file()
    cache_remove_entry()
    cache_find()
    browser_window_url_trim()
    
  2. Readable for humans:

    clear_history()
    save_history_to_file()
    remove_entry_from_cache()
    find_in_cache()
    check_for_updates()
    

What is the preferred word order? Is there any? I'm quite new to the programming but I'd like to acquire the good coding habits from the beginning. I've briefly read the PEP 8 Style Guide but I didn't find the answer there.

like image 391
Jeyekomon Avatar asked Jul 05 '17 23:07

Jeyekomon


2 Answers

While this question is hard to answer objectively, I think you should go to my favorite python standard library module for this, courtesy of Tim Peters.

>>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren't special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one-- and preferably only one --obvious way to do it. Although that way may not be obvious at first unless you're Dutch. Now is better than never. Although never is often better than right now. If the implementation is hard to explain, it's a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!

I believe Readability counts. is the most relevant here. With little exception, code is to be written and maintained by other humans, not machines. It should be "readable for humans" as you suggested.

like image 115
foslock Avatar answered Oct 22 '22 00:10

foslock


I think there isn't really any preferred order out there and you should use the one that makes working easy and more smooth. For example, if you had a lot of variables/classes/functions with the key word "history" and "cache", with repeatable words (like "history_add" and "cache_add") using the structured version would probably be better. But in any other cases, the readable version is going to be good solution.

like image 43
Maffey Avatar answered Oct 22 '22 01:10

Maffey