Skip to main content
Commonmark migration
Source Link

The question is: which one is more readable and easier to understand/maintain?

Duplicate code is not bad per se and your concerns about introducing variables are valid, because they increase cognitive load while reading the code (i.e. you have to keep in mind each one and pay attention if they change)

So over these two options I would prefer the first one. But I'd suggest a third:

###Introduce functions instead of variables

Introduce functions instead of variables

def item_1_from_dict(dict):
    return dict['not_to_short_key_one']['second_lvl_key_one']


do_something(item_1_from_dict(some_dict))

Of course item_1_from_dict should have a meaningful name.

###Advantages:

Advantages:

  • The "do something with the dict" code block gets more succinct
  • No temporary variables to keep track of
  • => Less temporary coupling, each line works on its own
  • No need to abbreviate names, the language is still clear and explicit

The question is: which one is more readable and easier to understand/maintain?

Duplicate code is not bad per se and your concerns about introducing variables are valid, because they increase cognitive load while reading the code (i.e. you have to keep in mind each one and pay attention if they change)

So over these two options I would prefer the first one. But I'd suggest a third:

###Introduce functions instead of variables

def item_1_from_dict(dict):
    return dict['not_to_short_key_one']['second_lvl_key_one']


do_something(item_1_from_dict(some_dict))

Of course item_1_from_dict should have a meaningful name.

###Advantages:

  • The "do something with the dict" code block gets more succinct
  • No temporary variables to keep track of
  • => Less temporary coupling, each line works on its own
  • No need to abbreviate names, the language is still clear and explicit

The question is: which one is more readable and easier to understand/maintain?

Duplicate code is not bad per se and your concerns about introducing variables are valid, because they increase cognitive load while reading the code (i.e. you have to keep in mind each one and pay attention if they change)

So over these two options I would prefer the first one. But I'd suggest a third:

Introduce functions instead of variables

def item_1_from_dict(dict):
    return dict['not_to_short_key_one']['second_lvl_key_one']


do_something(item_1_from_dict(some_dict))

Of course item_1_from_dict should have a meaningful name.

Advantages:

  • The "do something with the dict" code block gets more succinct
  • No temporary variables to keep track of
  • => Less temporary coupling, each line works on its own
  • No need to abbreviate names, the language is still clear and explicit
Source Link

The question is: which one is more readable and easier to understand/maintain?

Duplicate code is not bad per se and your concerns about introducing variables are valid, because they increase cognitive load while reading the code (i.e. you have to keep in mind each one and pay attention if they change)

So over these two options I would prefer the first one. But I'd suggest a third:

###Introduce functions instead of variables

def item_1_from_dict(dict):
    return dict['not_to_short_key_one']['second_lvl_key_one']


do_something(item_1_from_dict(some_dict))

Of course item_1_from_dict should have a meaningful name.

###Advantages:

  • The "do something with the dict" code block gets more succinct
  • No temporary variables to keep track of
  • => Less temporary coupling, each line works on its own
  • No need to abbreviate names, the language is still clear and explicit