diff options
author | Lewis Russell <lewis6991@gmail.com> | 2024-07-19 11:00:13 +0100 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-07-30 22:43:29 +0100 |
commit | d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 (patch) | |
tree | ca5a4a60fca9e814d0a3937cc4ff21ed9e037520 /src/nvim/garray.c | |
parent | 1b5a394ffd4bb638ec9cbbb5e4d12d11225389cf (diff) | |
download | rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.tar.gz rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.tar.bz2 rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.zip |
refactor: collapse statements in single assignments
Problem:
Variables are often assigned multiple places in common patterns.
Solution:
Replace these common patterns with different patterns that reduce the
number of assignments.
Use `MAX` and `MIN`:
```c
if (x < y) {
x = y;
}
// -->
x = MAX(x, y);
```
```c
if (x > y) {
x = y;
}
// -->
x = MIN(x, y);
```
Use ternary:
```c
int a;
if (cond) {
a = b;
} els {
a = c;
}
// -->
int a = cond ? b : c;
```
Diffstat (limited to 'src/nvim/garray.c')
-rw-r--r-- | src/nvim/garray.c | 8 |
1 files changed, 2 insertions, 6 deletions
diff --git a/src/nvim/garray.c b/src/nvim/garray.c index f87a196361..4f8ba30522 100644 --- a/src/nvim/garray.c +++ b/src/nvim/garray.c @@ -78,16 +78,12 @@ void ga_grow(garray_T *gap, int n) } // the garray grows by at least growsize - if (n < gap->ga_growsize) { - n = gap->ga_growsize; - } + n = MAX(n, gap->ga_growsize); // A linear growth is very inefficient when the array grows big. This // is a compromise between allocating memory that won't be used and too // many copy operations. A factor of 1.5 seems reasonable. - if (n < gap->ga_len / 2) { - n = gap->ga_len / 2; - } + n = MAX(n, gap->ga_len / 2); int new_maxlen = gap->ga_len + n; |