# Needless temporary variable
> No shortcuts for shortcuts.
This [[Anti-Pattern|anti-pattern]] happens because many people thinks that some operations like [[Call|calling]] [[len]] or accessing a [[dict]] value is an heavy computing operation, especially on large data structures.
## About len()
On [[Built-in|builtins]] [[Type|types]], the length of a data structure is already known by [[Python]] even before [[Call|calling]] the [[function]], because it's computed at the data structure creation and updated at each modification.
```python
# Don't
if (names_len:=len(names)) > 10:
do_stuff(names_len)
# Or
names_len = len(names)
if names_len > 10:
do_stuff(n)
# Do
if len(names) > 10:
do_stuff(len(names))
```
Since custom implementation of [[len]] can be implemented it in a way that each run run in [[Algorithmic Complexity|O(n)]] time, it's possible to use this property of [[len]] to add a [[semantic]] charge on our code:
- To use [[len]] every time it's needed without temporary [[Variable]] means that the operation is time [[Algorithmic Complexity|O(1)]]
- To use [[len]] once and store its result for later use means that this operation is not time [[Algorithmic Complexity|O(1)]]
## About dict access
Accessing a [[dict]] value through a key is an [[Algorithmic Complexity|O(1)]] operation.
```python
# Don't
joe_grade = grades["joe"]
do_something(joe_grade)
# Do
do_something(grades["joe"])
```
---
Bibliography:
- [Title - website.com](need bibliography)