Skip to content

Commit

Permalink
length() over .len (#6546)
Browse files Browse the repository at this point in the history
# About the pull request

Replaces `list.len` lookup with `length(list)`.

The following replacements are broadly applied:
- `list.len` -> `length(list)` : except where `len is being assigned or
modified, such as `list.len--`, which is a valid use-case
- `list && list.len`, `!isnull(list) && list.len`, `list?.len` ->
`LAZYLEN(list)` : also the inverted form; `LAZYLEN` is just a define for
`length` but is used here to more clearly communicate that originally
the list was tested for existence
- `if(list.len) list.Cut()` -> `LAZYCLEARLIST(list)` : identical to the
define so instead of replacing `len` with `length` just used the define

A few one-offs:
- `(list?(list.len):(0))` -> `(LAZYLEN(list) || 0)` in `recipe.dm` :
resulting value gets assigned to a variable that is used elsewhere, so
ensured it returned 0 in the same circumstances
- `if(!list || !list.len) { return FALSE } if(list?.len) { ...`->
`if(!LAZYLEN(list)) { return FALSE } ...` in `modify_variables.dm` :
simplified two branching checks into one
- `!list || list.len == 0` -> `!LAZYLEN(list)` in `teleporter.dm` :
removed the `==` test to allow simplifying down to `LAZYLEN` while
ensuring it has the same truthiness

<!-- Remove this text and explain what the purpose of your PR is.

Mention if you have tested your changes. If you changed a map, make sure
you used the mapmerge tool.
If this is an Issue Correction, you can type "Fixes Issue #169420" to
link the PR to the corresponding Issue number #169420.

Remember: something that is self-evident to you might not be to others.
Explain your rationale fully, even if you feel it goes without saying.
-->

# Explain why it's good for the game

`length(list)` is preferred over `list.len` since it inherently handles
nulls and is allegedly slightly more performant.
# Testing Photographs and Procedure
Boots without issue.


# Changelog
No player-facing changes.
  • Loading branch information
Doubleumc committed Jul 3, 2024
1 parent f6596a7 commit 72fa385
Show file tree
Hide file tree
Showing 381 changed files with 1,252 additions and 1,261 deletions.
2 changes: 1 addition & 1 deletion code/__DEFINES/tgs.config.dm
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
#define TGS_WARNING_LOG(message) log_world("TGS Warn: [##message]")
#define TGS_ERROR_LOG(message) log_world("TGS Error: [##message]")
#define TGS_NOTIFY_ADMINS(event) message_admins(##event)
#define TGS_CLIENT_COUNT GLOB.clients.len
#define TGS_CLIENT_COUNT length(GLOB.clients)
#define TGS_PROTECT_DATUM(Path) GENERAL_PROTECT_DATUM(##Path)
10 changes: 5 additions & 5 deletions code/__HELPERS/_lists.dm
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
/proc/bitfield_to_list(bitfield = 0, list/wordlist)
var/list/return_list = list()
if(islist(wordlist))
var/max = min(wordlist.len, 24)
var/max = min(length(wordlist), 24)
var/bit = 1
for(var/i in 1 to max)
if(bitfield & bit)
Expand Down Expand Up @@ -148,10 +148,10 @@
* Returns TRUE if the list had nulls, FALSE otherwise
**/
/proc/list_clear_nulls(list/list_to_clear)
var/start_len = list_to_clear.len
var/start_len = length(list_to_clear)
var/list/new_list = new(start_len)
list_to_clear -= new_list
return list_to_clear.len < start_len
return length(list_to_clear) < start_len

///Return a list with no duplicate entries
/proc/unique_list(list/inserted_list)
Expand All @@ -174,6 +174,6 @@
if(!inserted_list)
return

for(var/i in 1 to inserted_list.len - 1)
inserted_list.Swap(i, rand(i, inserted_list.len))
for(var/i in 1 to length(inserted_list) - 1)
inserted_list.Swap(i, rand(i, length(inserted_list)))

2 changes: 1 addition & 1 deletion code/__HELPERS/chat.dm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
if((!admin_only || channel.is_admin_channel) && (channel_tag in applicable_tags))
channels_to_use += channel

if(channels_to_use.len)
if(length(channels_to_use))
world.TgsChatBroadcast(message, channels_to_use)

/**
Expand Down
4 changes: 2 additions & 2 deletions code/__HELPERS/game.dm
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

/proc/recursive_mob_check(atom/O, list/L = list(), recursion_limit = 3, client_check = 1, sight_check = 1, include_radio = 1)

//debug_mob += O.contents.len
//debug_mob += length(O.contents)
if(!recursion_limit)
return L
for(var/atom/A in O.contents)
Expand Down Expand Up @@ -294,7 +294,7 @@
* * cache_only - Whether to not actually send a to_chat message and instead only update larva_queue_cached_message
*/
/proc/message_alien_candidates(list/candidates, dequeued, cache_only = FALSE)
for(var/i in (1 + dequeued) to candidates.len)
for(var/i in (1 + dequeued) to length(candidates))
var/mob/dead/observer/cur_obs = candidates[i]

// Generate the messages
Expand Down
2 changes: 1 addition & 1 deletion code/__HELPERS/guid.dm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/proc/GUID()
var/const/GUID_VERSION = "b"
var/const/GUID_VARIANT = "d"
var/node_id = copytext_char(md5("[rand()*rand(1,9999999)][world.name][world.hub][world.hub_password][world.internet_address][world.address][world.contents.len][world.status][world.port][rand()*rand(1,9999999)]"), 1, 13)
var/node_id = copytext_char(md5("[rand()*rand(1,9999999)][world.name][world.hub][world.hub_password][world.internet_address][world.address][length(world.contents)][world.status][world.port][rand()*rand(1,9999999)]"), 1, 13)

var/time_high = "[num2hex(text2num(time2text(world.realtime,"YYYY")), 2)][num2hex(world.realtime, 6)]"

Expand Down
10 changes: 5 additions & 5 deletions code/__HELPERS/icons.dm
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ world
/proc/getFlatIcon(image/appearance, defdir, deficon, defstate, defblend, start = TRUE, no_anim = FALSE, appearance_flags = FALSE)
// Loop through the underlays, then overlays, sorting them into the layers list
#define PROCESS_OVERLAYS_OR_UNDERLAYS(flat, process, base_layer) \
for (var/i in 1 to process.len) { \
for (var/i in 1 to length(process)) { \
var/image/current = process[i]; \
if (!current) { \
continue; \
Expand All @@ -347,7 +347,7 @@ world
} \
current_layer = base_layer + appearance.layer + current_layer / 1000; \
} \
for (var/index_to_compare_to in 1 to layers.len) { \
for (var/index_to_compare_to in 1 to length(layers)) { \
var/compare_to = layers[index_to_compare_to]; \
if (current_layer < layers[compare_to]) { \
layers.Insert(index_to_compare_to, current); \
Expand Down Expand Up @@ -403,7 +403,7 @@ world

var/curblend = appearance.blend_mode || defblend

if(appearance.overlays.len || appearance.underlays.len)
if(length(appearance.overlays) || length(appearance.underlays))
var/icon/flat = icon(flat_template)
// Layers will be a sorted list of icons/overlays, based on the order in which they are displayed
var/list/layers = list()
Expand Down Expand Up @@ -556,15 +556,15 @@ world
/proc/sort_atoms_by_layer(list/atoms)
// Comb sort icons based on levels
var/list/result = atoms.Copy()
var/gap = result.len
var/gap = length(result)
var/swapped = 1
while (gap > 1 || swapped)
swapped = 0
if(gap > 1)
gap = floor(gap / 1.3) // 1.3 is the emperic comb sort coefficient
if(gap < 1)
gap = 1
for(var/i = 1; gap + i <= result.len; i++)
for(var/i = 1; gap + i <= length(result); i++)
var/atom/l = result[i] //Fucking hate
var/atom/r = result[gap+i] //how lists work here
if(l.layer > r.layer) //no "result[i].layer" for me
Expand Down
Loading

0 comments on commit 72fa385

Please sign in to comment.