ID:1303527
 
(See the best response by Stephen001.)
Problem description: Now this is mainly for checking how many players in the world. If say their is only one player I want to be able to create an NPC to fight since this is for a tournament style game.

Code:
// i tried this way didn't work as you can imagine i'm mainly experimenting with things right now.

if(var/mob/M in players <= 1)




Best response
There's a length() procedure you can use to get the 'length' of things. For lists, that is the number of elements inside the list:

if (length(players) < 2)
Oh...I should really should check the reference more. Thankyou.
No problem. As always in dev help, vote for the answer that most helps you!
In the case of lists, isn't it more efficient if you are able to use the len var instead? Depending on how the list is created, in most cases, length(mylist) == mylist.len
List.len is indeed more efficient than length() but the difference is negligible; you only start to see a significant difference if you're using it in a loop, and in that case you should be using list.len instead.
That tells me it's bad practice to use the length() proc to check the length of a list. In what case is it going to be preferable to use the length() proc, rather than list.len?
Bad practice would imply some kind of gotcha or non-obvious/unintended behaviour. Arrays in Java for example tend to perform better than ArrayList, but I don't think you'd find anyone sane suggesting to use arrays in preference of ArrayList, probably the opposite in fact.

The obvious case where length() would be preferable would be where you don't actually know (or necessarily require) it's a list. Similarly, length() is null-safe, returning 0. Meaning you can roll the length test and a null-check into one call, you're guaranteed not to miss the null case now etc etc
Stephen001 wrote:
The obvious case where length() would be preferable would be where you don't actually know (or necessarily require) it's a list.
That makes sense, although the issue of not knowing probably doesn't come up very often. It would be useful in making procs that can take various argument types.

Similarly, length() is null-safe, returning 0.
That's a good point, and it does make length() that much more convenient, but I wonder if even an extra if() check combined with list.len would still end up more efficient than length() alone.

In the OPs case, he knows that he's dealing with a list. With some kind of safety check in place, it should then be ideal to simply use list.len in that case.
It would, but it would also be an efficiency you don't care about and no-one notices. Where-as length() offers you a robustness, and that quality I'd value much higher: The ability to not care.

Bugs avoidance that involves no extra code is always the best kind.

A runtime off a missed null-check is something your user will notice, in all probability. The speed difference on the way you'd fetching list length, probably isn't.
It might become noticeable in an exceptionally huge list. Anyway, I don't think length() should be used as an excuse to be careless about part of your code. I think maximum efficiency is a good habit to get into, as long as your code is bug free.
And that exceptionally huge list is a 0.001% scenario, and almost always one you can spot in advance and make an intelligent decision about how you're going to implement. The null runtime, particularly for newer people, is much more common, and easy to do, despite trying to write bug-free code. Having that null check for free is not 'careless', it's helping yourself out, doing you an extra favour.

No-one intentionally writes buggy code, and yet, 90% of post-development effort is on fixing bugs seen by users. If I had to weigh up a performance difference I know people will not see 99.99% of the time, against a mechanism that offers me a bit of an extra safety net against bugs, it's a no brainer. Bugs are embarrassing, and crashing bugs like the kind you usually get out of null runtimes, doubly so. Problem is, they are also not something you can always anticipate in advance.

I cannot on good faith recommend len over length() to people in developer help, in the general case. It's lunacy.