Skip navigation
``` if myString.count > 0 { // `myString` isn't empty } ``` This code seems pretty reasonable…
0:00 / 1:31

``` if myString.count > 0 { // `myString` isn't empty } ``` This code seems pretty reasonable…

Vincent Pradeilles

@v_pradeilles36K followers

Visit on X
86 likesDownloadOriginal post

19,050 views

``` if myString.count > 0 { // `myString` isn't empty } ``` This code seems pretty reasonable, doesn't it? 😌 But it actually hides quite a serious issue! Can you guess what that issue is? 🤨

8 Comments

Comments from the original post on X (Twitter)

  • jestem's profile picture

    @megakoxem

    Any examples of situations Where those Two would have different results? Or is this just speed differences?

    1 like

  • Vincent Pradeilles's profile picture

    @v_pradeilles

    Normally is it should be only speed differences.

  • deni 🏛's profile picture

    @denizakya

    It is O(n) operation

    1 like

  • Dosbol Duysekov's profile picture

    @ZGV2ZWxvcGVy

    comparing ‘== false’ is slow for the compiler to type check, why wouldn’t you use ‘!myString.isEmpty’?

  • Tziki's profile picture

    @SionSasson

    @avi_tsadok ☝🏽

  • Frank Tinsley's profile picture

    @franktinsley

    How does isEmpty check if there is at least 1 character?

  • LE Roi's profile picture

    @hakidu_Roi

    Merci frérot

  • hozetonoze's profile picture

    @hozetonoze

    why tf is .count not a constant time op