Python

Advantage of Not Re-Assigning Values

Efficiency

Not re-assigning values saves memory and CPU cycles, making code more efficient.

Clarity

Code is easier to read and understand when variables have consistent values throughout.

Debugging

Debugging is easier when variables have consistent values and unexpected changes are minimized.

Maintenance

  • Code is easier to maintain when variables have consistent values.
  • Changes to variable values can have unintended consequences.

Best Practice

Not re-assigning values is a best practice in Python and is recommended by the PEP 8 style guide.

def sum_accounts_balance_view(account_ids):
    balance_sum = 0

    for account_id in account_ids:
        account = None

        if get_account_data_from_backend(account_id):
            account = get_account_data_from_backend(account_id)
        elif get_account_data_from_external_system(account_id):
            account = get_account_data_from_external_system(account_id)
        else:
            continue

        if "debit" in account:
            if "value" in account["debit"]:
                balance_sum += account["debit"]["value"]

    return balance_sum
def sum_accounts_balance_view(account_ids):
  
    balance_sum = sum(get_amounts_from_accounts(accounts))
  
    return balance_sum
def get_amounts_from_accounts(accounts):
    for account in accounts:
        if "debit" in account:
            if "value" in account["debit"]:
                balance_sum += account["debit"]["value"]
                #MESS AGAIN
            
            
def sum_accounts_balance_view(account_ids):  
    
    balance_sum = sum(get_amounts_from_accounts(accounts))
  
    return balance_sum

NESTING = BAD

Flat is better than nested.
def get_amount_from_account(account):
    if "debit" not in account:
        return 0
      
    if "value" not in account["debit"]:
        return 0

    return account["debit"]["value"]
                
            
def sum_accounts_balance_view(account_ids):  
    
    balance_sum = sum(map(get_amount_from_account, accounts))
  
    return balance_sum
def get_amount_from_account(account):
    if "debit" not in account:
        return 0
      
    if "value" not in account["debit"]:
        return 0

    return account["debit"]["value"]
                
            
def get_accounts_from_ids(account_ids):
    accounts = []
    
    for account_id in account_ids:
        if get_account_data_from_backend(account_id):
            account = get_account_data_from_backend(account_id)
        elif get_account_data_from_external_system(account_id):
            account = get_account_data_from_external_system(account_id)
        else:
            continue
        
        accounts.append(account)
    
    return accounts
      
      
def sum_accounts_balance_view(account_ids):  
  
    accounts = get_accounts_from_ids(account_ids)
    
    balance_sum = sum(map(get_amount_from_account, accounts))
  
    return balance_sum
def get_amount_from_account(account):
    if "debit" not in account:
        return 0
      
    if "value" not in account["debit"]:
        return 0

    return account["debit"]["value"]
                
    
def get_account_from_id(account_id):
    if account := get_account_data_from_backend(account_id):
        return account
    
    if account := get_account_data_from_external_system(account_id):
        return account
    
    return None

    
def get_accounts_from_ids(account_ids):
    accounts = []
    
    for account_id in account_ids:
        account = get_account_from_id(account_id)
        
        if account:
             accounts.append(account)
    
    return accounts
      
      
def sum_accounts_balance_view(account_ids):  
  
    accounts = get_accounts_from_ids(account_ids)
    
    balance_sum = sum(map(get_amount_from_account, accounts))
  
    return balance_sum
def get_amount_from_account(account):
    if "debit" not in account:
        return 0
      
    if "value" not in account["debit"]:
        return 0

    return account["debit"]["value"]
                
    
def get_account_from_id(account_id):
    if account := get_account_data_from_backend(account_id):
        return account
    
    if account := get_account_data_from_external_system(account_id):
        return account
    
    return None

    
def get_accounts_from_ids(account_ids):
    return filter(None, map(get_account_from_id, account_ids))
   
      
def sum_accounts_balance_view(account_ids):  
    accounts = get_accounts_from_ids(account_ids)
    
    balance_sum = sum(map(get_amount_from_account, accounts))
  
    return balance_sum
def sum_accounts_balance_view(account_ids):
    balance_sum = 0

    for account_id in account_ids:
        account = None

        if get_account_data_from_backend(account_id):
            account = get_account_data_from_backend(account_id)
        elif get_account_data_from_external_system(account_id):
            account = get_account_data_from_external_system(account_id)
        else:
            continue

        if "debit" in account:
            if "value" in account["debit"]:
                balance_sum += account["debit"]["value"]

    return balance_sum
def test_sum_accounts_balance_view_if_no_accounts(): ...
def test_sum_accounts_balance_view_if_no_accounts(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_fails(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system_fails(): ...
def test_sum_accounts_balance_view_if_no_accounts(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_fails(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system_fails(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_have_debit_no_value(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_have_debit_and_value(): ...
def test_sum_accounts_balance_view_if_no_accounts(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_fails(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system_fails(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_have_debit_no_value(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_have_debit_and_value(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system_have_debit_no_value(): ...
def test_sum_accounts_balance_view_if_accounts_from_external_system_have_debit_and_value(): ...
def test_sum_accounts_balance_view_if_accounts_from_backend_and_external_system_have_debit_and_value(): ...
def get_amount_from_account(account):
    if "debit" not in account:
        return 0
      
    if "value" not in account["debit"]:
        return 0

    return account["debit"]["value"]
                
    
def get_account_from_id(account_id):
    if account := get_account_data_from_backend(account_id):
        return account
    
    if account := get_account_data_from_external_system(account_id):
        return account
    
    return None

    
def get_accounts_from_ids(account_ids):
    return filter(None, map(get_account_from_id, account_ids))
   
      
def sum_accounts_balance_view(account_ids):  
    accounts = get_accounts_from_ids(account_ids)
    
    balance_sum = sum(map(get_amount_from_account, accounts))
  
    return balance_sum
def get_amount_from_account_if_no_debit(): ...
def get_amount_from_account_if_debit_no_value(): ...
def get_amount_from_account_if_debit_and_value(): ...
def get_account_from_id_for_backend(): ...  
def get_account_from_id_for_external_system(): ...  
def get_account_from_id_when_user_not_found(): ...  
def get_accounts_from_ids_when_no_accounts(): ...  
def get_accounts_from_ids_when_accounts(): ...  
def sum_accounts_balance_view_(): ...

Thank You!

For Attending Pykonik

Kraków

Enjoy the beauty of this city

Made with Slides.com