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.
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
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
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.
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
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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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?
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.
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.
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.
317
u/grantholle Jul 10 '21
The upside is that they didn't use an else
72
u/valschermjager Jul 10 '21
seems risky. I’d feel a whole lot better if it was inside yet another function called seriouslyCheckTheArray(); then just call that.
255
u/twoPoundsOfGoldfish Jul 10 '21
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
22
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 10 '21
Image Transcription: Code
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!
47
u/ssj4VB Jul 10 '21
can't you just flat out return Array.isArray(input);
7
u/[deleted] Jul 10 '21 edited Jul 10 '21
I don't know js, but...
return Array.isArray(input);7
u/Sechan9 Jul 10 '21
As a dumb programmer can anyone explain me why this is bad?
2
u/jimmypompom Jul 10 '21
This is when it's time to go to bed
2
u/Tushar0905 Jul 10 '21
Just to clarify ; this code is on production and was approved by this guy
2
u/SlaimeLannister Jul 10 '21
2
u/[deleted] Jul 10 '21
Lol we could make a whole sub with pointlesscode seperate from badcode
2
u/RobMig83 Jul 10 '21
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.
2
u/djcraze Jul 10 '21
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.
2
u/janaagaard Jul 11 '21
arrayCheckcould have been created beforeArray.isArrayexisted / 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.arrayCheckis 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.1
u/LoneFoxKK Jul 10 '21
Enterprise software be like
1
u/dale_cinger Jul 10 '21
Whoosh, i though that's freakin string again
1
u/_Guigui Jul 10 '21
But is the array check really an array check ? Or does it need more?
1
u/thanatica Jul 10 '21
This probably used to be something else before
Array.isArraybecame widely available.1
u/arzen221 Jul 10 '21
LGMT: Ship it
1
u/cormac596 Jul 10 '21
Tuppence says it's a corporate environment/project.
Don't ask why I think so
1
u/pumpkinplight Jul 10 '21
My manager codes this way :(
1
u/Nickbot606 Jul 10 '21
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
1
u/dasbodmeister Jul 10 '21
Vibe check
1
u/5p4n911 Jul 10 '21
It might be unfinished code that's supposed to have more checks... I hope
1
u/B0ltSn1per Jul 10 '21
public static void add(float input1, float input2){ return input1 + input 2; }
1
u/Keftcha Jul 10 '21
I hope it's avairable as an NPM package
1
u/BlooShinja Jul 11 '21
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.
1
u/iEatPlankton Jul 11 '21
🌎checkArray()🧑🚀”wait it’s all isArray()?” 🔫🧑🚀”always has been”
1
u/[deleted] Jul 25 '21
I wonder if the array WAS checked? I’m assuming that snippet might be for checking an array?
1
u/EishLekker Jul 10 '21
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?
0
u/[deleted] Jul 10 '21
[deleted] — view deleted comment
1
u/[deleted] Jul 10 '21
[removed] — view removed comment