DEV Community

The art and difficulty of naming in programming

Horace FAYOMI on February 26, 2022

Important note: Do not read any of the comments in the thread without solving the challenge. This article is a follow of my very first article Ev...
Collapse
 
fayomihorace profile image
Horace FAYOMI

I drop the solution here anyways.

def print_items_counts(array):
    item_map_count = {}
    for item in array:
        if item_map_count.get(item):
            item_map_count[item] += 1
        else:
            item_map_count[item] = 1

    for item, count in item_map_count.items():
        print(f"{item}: {count}")
Enter fullscreen mode Exit fullscreen mode
Collapse
 
geraldew profile image
geraldew

If we're talking about clarity then it is misleading to use

 if item_map_count.get(item):
Enter fullscreen mode Exit fullscreen mode

when there is no apparent use for that value. Instead it is clearer to use

 if item in item_map_count :
Enter fullscreen mode Exit fullscreen mode

So it's not just choice of variable names that affect clarity.

Collapse
 
fayomihorace profile image
Horace FAYOMI

You're right geraldew, thanks.

Collapse
 
yannickkiki profile image
yannickkiki • Edited

Nice article, waiting for the part 2!

Just for the record, we could also write this part

if item_map_count.get(item):
    item_map_count[item] += 1
else:
    item_map_count[item] = 1
Enter fullscreen mode Exit fullscreen mode

as this

item_map_count[item] = 1 + item_map_count.get(item, 0)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
fayomihorace profile image
Horace FAYOMI

Beautiful indeed, I like shorthands. Thanks.

Collapse
 
fayomihorace profile image
Horace FAYOMI

And the same thing could be achieved using build in python library collections.Counter like this:

collections.Counter(array)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jonaspetri profile image
Jonas Petri

That was a great article!
It took me a while to figure out what the program actually did, just because of how poorly the variables were named.

Collapse
 
fayomihorace profile image
Horace FAYOMI

Thanks Jonas.

Collapse
 
ooling profile image
Sam oo Líng

Great! I'm waiting for part 2

Collapse
 
fayomihorace profile image
Horace FAYOMI

Thanks Sammy. Don't, it will come ^_^.

Collapse
 
tomasforsman profile image
Tomas Forsman

This was not really an example of simply bad naming, this was intentionally misleading naming with "booleans".

Here is how I would set bad names:

def process_list(lst):
    result_dict = {}
    for item in lst:
        if result_dict.get(item):
            result_dict[item] += 1
        else:
            result_dict[item] = 1

    for key, value in result_dict.items():
        print(f"{key}: {value}")
Enter fullscreen mode Exit fullscreen mode

And here is how far I could take this with help from the other comments. Perhaps a bit overkill...

from collections import Counter
from typing import List, Any

def print_item_counts(array: List[Any]) -> None:
    """
    Print the count of each item in the input array.

    Args:
        array (List[Any]): The list of items to be counted.

    Example:
        >>> print_item_counts(['apple', 'banana', 'apple'])
        apple: 2
        banana: 1
    """
    if not array:
        print("The array is empty.")
        return

    item_count = Counter(array)
    for item, count in item_count.items():
        print(f"{item}: {count}")
Enter fullscreen mode Exit fullscreen mode

I know Docstring isn't part of this but for the sake of a clarity example I included it; not for readability (it actually makes the code a bit less readable imho) but for documentation and IDE tooling.

Reminder to post the next part 😀