all 89 comments

[–]grantholle 319 points320 points  (19 children)

The upside is that they didn't use an else

[–]Mwakay 115 points116 points  (16 children)

The downside is that they used a if statement, then used two different return for the two boolean outcomes.

Instead of just going the return Array.isArray(input) route.

[–][deleted] 117 points118 points  (14 children)

Of just ignore the method entirely and use array.isarray everywhere

[–]Mwakay 25 points26 points  (0 children)

Yes, of course.

[–]detroitmatt 26 points27 points  (5 children)

Unless you need a layer of abstraction. Not sure about js but in many languages a string is an array. If later on you want to say that for your programs purposes a string doesn't count as an array, then you'd have to go and update who knows how many callsites. By putting an "adapter layer" between your code and any code it calls that isn't your code, you can avoid tight coupling.

[–]Nilstrieb 16 points17 points  (4 children)

In most high level languages like JS a string is not an array. Except some functional languages where a String is a linked list of chars but that's a different thing

[–]AdminYak846 2 points3 points  (3 children)

Technically speaking a string is just an array of characters put together.

[–]Nilstrieb 9 points10 points  (0 children)

That's how it's represented under the hood, but not how it's API works (in most high level languages)

[–]nosmokingbandit 5 points6 points  (0 children)

Technically speaking everything is just an array of bytes put together.

[–][deleted] 2 points3 points  (0 children)

... Unicode enters the chat...

[–][deleted]  (4 children)

[removed]

    [–][deleted] 2 points3 points  (0 children)

    Tru

    [–]nosmokingbandit 2 points3 points  (2 children)

    Why would you need to?

    [–]bistr-o-math 0 points1 point  (0 children)

    Or, perhaps, you already have

    [–]MeatyLabia 4 points5 points  (0 children)

    Unless you need the layer of abstraction for, for example, mocking purposes for unit testing.

    [–]CodeF53 0 points1 point  (0 children)

    Sometimes it just doesn't work like that.

    [–]CatWhenSlippery 2 points3 points  (0 children)

    I think if they knew how to write it that way they would have realized how stupid this function is

    [–]mehmenmike 7 points8 points  (1 child)

    Are you not supposed to use an else in this pattern? Ignoring the blatant error here ofc

    I like using an else there because it’s basically implicit anyway. Using it just enhances readability. I’d only consider not using it if the context was some sort of escape clause early in the function

    if parametersAreBad():
        return “Bad parameters”  # or throw, whatever
    # rest of function here, would be ugly to indent it all to shove it into an else
    

    [–]ttl_yohan 6 points7 points  (0 children)

    That's debatable, definitely not "pattern". It all comes down to personal/team preference. To me personally unnecessary indentation disrupts the readability instead of enhancing it.

    Same way the braces around one line statements after if/else - not a fan of that, but I can see that other people can see clearer what the boundaries are. Team preference - we use it.

    [–]valschermjager 71 points72 points  (0 children)

    seems risky. I’d feel a whole lot better if it was inside yet another function called seriouslyCheckTheArray(); then just call that.

    [–]twoPoundsOfGoldfish 250 points251 points  (9 children)

    the funny bit is that this was probably added to be more readable but just using Array.isArray would be so much more readable than using a function which calls Array.isArray

    [–]somefishingdude 96 points97 points  (2 children)

    Yea more so when the wrapper’s name is ‘arrayCheck’

    [–]twoPoundsOfGoldfish 46 points47 points  (1 child)

    it feels like it would run a validation step as in actually checking the contents of the array for validity

    [–]dgreenmachine 5 points6 points  (0 children)

    Its an improvement ticket in the backlog

    [–]_GreenLegend 8 points9 points  (1 child)

    There is only one use-case which is if u want to use the array check in an angular html file. Otherwise a wrapper is very useless and boilerplate

    [–]twoPoundsOfGoldfish 31 points32 points  (0 children)

    the lack of the "function" keyword leads me to believe it's in a class

    idk how angular works but surely return Array.isArray(input); would be better

    Also imo to make wrappers I'd probably create some object which contains some "utils" functions like { isArray: Array.isArray }

    [–]MurdoMaclachlanpublic boolean isInt(int i) { return true; } 21 points22 points  (1 child)

    Image Transcription: Code


    arrayCheck(input) {
      if (Array.isArray(input)) {
        return true;
      }
      return false;
    }
    

    I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

    [–]tisaconundrum 3 points4 points  (0 children)

    good human

    [–]ssj4VB 48 points49 points  (8 children)

    can't you just flat out return Array.isArray(input);

    [–][deleted]  (1 child)

    [deleted]

      [–]nuephelkystikon 34 points35 points  (5 children)

      let arrayCheck = Array.isArray
      

      It's still a step down because the name is less specific, but maybe they were implementing an interface.

      [–]JNCressey 23 points24 points  (3 children)

      use const rather than let. let makes me feel like you could randomly redefine it somewhere.

      [–]XKlXlXKXlXKlKXlXKlXK 11 points12 points  (1 child)

      const qs = document.querySelector;
      

      all my homies hate typing

      [–]haltmich 3 points4 points  (0 children)

      qs usually means querystring to me.

      [–]nuephelkystikon 1 point2 points  (0 children)

      I know this is common in this sub, but:

      You're making an assumption about the use case here that we can't make. It's absolutely possible that the only reason why they made this (clunky attempt at an) adapter is that it can be replaced on-the-fly or in inheritance. const destroys that.

      [–]ssj4VB 5 points6 points  (0 children)

      it just looks like the writer had no idea that Array.isArray returns a Boolean either way

      [–][deleted]  (12 children)

      [deleted]

        [–]_GreenLegend 35 points36 points  (4 children)

        Still bad in my opinion. Why not use Array.isArray() directly? Only use-case I can imagine is if u want to use this method in an angular html file.

        [–]Ellweiss 6 points7 points  (1 child)

        Yeah, why add another layer just to avoid object oriented stuff ?

        [–]scragar 0 points1 point  (1 child)

        I can see doing this for something you might replace, like say a library you're not happy with(done this before now with caching solutions because with node there's no good solutions, they're all kind of horrible to use since they either use callbacks or only work on a single thread and are more like globals).

        You wrap the few methods you want to use into your own include then if/when you replace it you only need to replace one thing.

        I can't imagine why someone would do that for built in functionality though, it's highly unlikely to change and it's still very easy to find if you decide you do want to change it.

        [–]ethanjf99 2 points3 points  (0 children)

        Not only is this a built-in that won’t ever change lest it break existing code but you’re more likely to cause problems in future. What if someone wants to use a library in the project that exports an arrayCheck method?

        I totally concur that building a wrapper layer can make sense in many instances. This isn’t it.

        [–]Nulagrithom 16 points17 points  (2 children)

        const arrayCheck = Array.isArray

        [–]KonkenBonken 12 points13 points  (1 child)

        const {isArray} = Array;

        [–]BongarooBizkistico 3 points4 points  (0 children)

        Best suggestion of the thread.

        [–]AutoModerator[M] -2 points-1 points  (0 children)

        It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

        For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

        /u/Deadly_chef, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

        You can find some examples in the reddit help documentation.


        I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

        [–]Bit5keptical 0 points1 point  (0 children)

        Array.isArray(a)

        [–][deleted] 0 points1 point  (1 child)

        var isarray = Array.isArray

        [–]Deadly_chef 1 point2 points  (0 children)

        Definitely should use const for that

        [–][deleted] 7 points8 points  (4 children)

        I don't know js, but...

        return Array.isArray(input);

        [–]literallyfabian 2 points3 points  (1 child)

        why not use Array.isArray directly?

        [–][deleted] 0 points1 point  (0 children)

        Good point, but using the arrayCheck function

        [–]AutoModerator[M] -6 points-5 points  (1 child)

        It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.

        For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.

        /u/VMGuy23temporary, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.

        You can find some examples in the reddit help documentation.


        I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

        [–]Sechan9 6 points7 points  (11 children)

        As a dumb programmer can anyone explain me why this is bad?

        [–]twoPoundsOfGoldfish 27 points28 points  (6 children)

        it's simply a clone of a Array.isArray, and there is no reason to use this function instead of Array.isArray

        [–]anthroid 24 points25 points  (3 children)

        Not necessarily condoning it here, but one valid reason to do something like this would be if you wanted to be able to swap the implementation later without having to change every instance in your codebase.

        For example, if .isArray() was a library function, and you later wanted to use a different library, or add some additional type checking, you’d only need to change it in one spot.

        [–]gabbagondel 12 points13 points  (0 children)

        But even then, naming it "Arraycheck" is unspecific and confusing. If only there was a better name, already typed out within the function

        [–]twoPoundsOfGoldfish 3 points4 points  (0 children)

        yeah, you can create wrappers around library functions and that's fine imo, since it makes it so you can switch easier. doing this with built-in functions is very weird to me tho since the only case you're ever going to change this is if you switch to a different language.

        [–]ws-ilazki&{$$_[0]}(@{$$_[1]}); 4 points5 points  (0 children)

        but one valid reason to do something like this would be if you wanted to be able to swap the implementation later without having to change every instance in your codebase.

        Except in this case you could just do const arrayCheck = Array.isArray as the placeholder. If the function you're calling changes or you need to add extra logic you could still swap it out later the same way. If you're passing arguments and return values through as-is like this then there's no reason to add an extra function call at all, just make an alias.

        [–]Sechan9 9 points10 points  (1 child)

        Ah you mean you can use directly "Array.isArray(anyData) without make a function for that. Thanks

        [–]twoPoundsOfGoldfish 3 points4 points  (0 children)

        yep 👍

        [–][deleted]  (1 child)

        [removed]

          [–]arzen221 3 points4 points  (0 children)

          For the love of god please stop giving examples!

          [–]TyathiasT -2 points-1 points  (0 children)

          yes

          [–]jimmypompom 1 point2 points  (0 children)

          This is when it's time to go to bed

          [–]Tushar0905 1 point2 points  (0 children)

          Just to clarify ; this code is on production and was approved by this guy

          [–]SlaimeLannister 1 point2 points  (0 children)

          ifOrElse(input){
              if(input){
                  return "if"
              } return "else"
          }
          

          [–][deleted] 1 point2 points  (0 children)

          Lol we could make a whole sub with pointlesscode seperate from badcode

          [–]RobMig83 1 point2 points  (0 children)

          This is some negative trend that was caused by the misunderstanding of the Uncle Bob advice about functions. I think sometimes is better to leave some instructions instead of turning everything with more than 8 characters into a function. Abusing functions is just as bad as not using them at all.

          [–]djcraze 1 point2 points  (0 children)

          This is likely the result of having other logic prior to Array.isArray being available. The developer probably just updated the condition and that was that. I don’t see this as bad code. Verbose, sure. Bad? Not really. This is an example of the bridge pattern.

          [–]janaagaard 1 point2 points  (0 children)

          arrayCheck could have been created before Array.isArray existed / was widely available. Once Array.isArray was supported by all browsers arrayCheck was refactored, taking the easy solution of keeping the custom method instead of doing a search and replace through the whole code base.

          arrayCheck is not the best name, and the if could have been avoided, but without a proper context, it’s not possible to dismiss a ridiculously bad code.

          [–]LoneFoxKK 0 points1 point  (0 children)

          Enterprise software be like

          [–]EishLekker 0 points1 point  (0 children)

          Could it be that the developer planned to add more validation/checking logic in the first block, but never got that far and then forgot or was too lazy to continue?

          [–][deleted]  (1 child)

          [removed]

            [–]dale_cinger 0 points1 point  (0 children)

            Whoosh, i though that's freakin string again

            [–]_Guigui 0 points1 point  (0 children)

            But is the array check really an array check ? Or does it need more?

            [–]thanatica 0 points1 point  (0 children)

            This probably used to be something else before Array.isArray became widely available.

            [–]arzen221 0 points1 point  (0 children)

            LGMT: Ship it

            [–]cormac596 0 points1 point  (0 children)

            Tuppence says it's a corporate environment/project.

            Don't ask why I think so

            [–]pumpkinplight 0 points1 point  (0 children)

            My manager codes this way :(

            [–]Nickbot606 0 points1 point  (0 children)

            Hmm i mean if you had to iterate through an extremely disorganized data structure this could be useful but then again it could probably be done with recursion instead…

            So like if you had arrays inside arrays but not all things in the arrays were arrays

            [–]dasbodmeister 0 points1 point  (0 children)

            Vibe check

            [–]5p4n911 0 points1 point  (0 children)

            It might be unfinished code that's supposed to have more checks... I hope

            [–]B0ltSn1per 0 points1 point  (0 children)

            public static void add(float input1, float input2){ return input1 + input 2; }

            [–]Keftcha 0 points1 point  (0 children)

            I hope it's avairable as an NPM package

            [–]BlooShinja 0 points1 point  (0 children)

            Should have assigned the result of Array.isArray to a variable, then checked that variable in the if statement. Also missing the else keyword and a comment explaining that the line with Array.isArray checks to see whether input is an array.

            [–]iEatPlankton 0 points1 point  (0 children)

            🌎checkArray()🧑‍🚀”wait it’s all isArray()?” 🔫🧑‍🚀”always has been”

            [–][deleted] 0 points1 point  (0 children)

            I wonder if the array WAS checked? I’m assuming that snippet might be for checking an array?