It's called yoda style and mostly meant to prevent you from accidentally writing "variable = null" instead of "variable == null".
The former would (in some languages) fail silently and lead to strange bugs. If you flip your comparisons, you get a compile error instead, since you can't assign to null.
It’s a very easy thing to avoid rather than using an entirely new style that looks super goofy. Even in C most compilers will throw a warning unless a second set of parenthesis is used for the assignment.
I was curious about this because I've seen minifiers in some languages convert x == null into null == x. The only reason I can find is to avoid accidental assignment e.g if you write x = null by mistake, the code will still run (depending on the language) but in an unexpected way. If you write null = x, it'll throw an error and you catch the mistake.
Python is probably the most common one where trying to do that is a syntax error.
Unlike in most c-descendant/inspired languages, assignment doesn't produce a value in python, and trying to evaluate it is a syntax error.
Fun fact: In R, there are multiple assignment operators, two of which function almost identically in expressions: = and <-. It is a syntax error to use = in an if statement, but you can use <- if you really want to perform an assignment while you're evaluating the conditions.
In some cases, I’m not sure if Java is one of them, null itself is actually a value. In JavaScript, you can check if a variable is null or undefined and you’d be checking two very different things.
!= null checks whether a variable has an assigned value other than null.
!= undefined checks whether a variable exists at all.
In not retarded languages a variable exists when you define it in code, so an != undefined check is unnecessary.
In either case with Java, this code remains, indeed, bad.
Someone else mentioned that it's to prevent accidentally using = instead of == in languages where the former would not return an error, because "null =" would return an error as you cannot assign to null.
public static File file(File directory, String... names) {
Assert.notNull(directory, "directorydirectory must not be null");
if (ArrayUtil.isEmpty(names)) {
return directory;
}
File file = directory;
for (String name : names) {
if (null != name) {
[The following line was changed, with the file function call being the sole change:]
That many languages support this? I can think of Java and maybe C# (I don't know the language), but I can't think of any more. The high level languages I know in the style of Python or JS and low level languages I know in the style of C or Rust will have functions as a variable type
I think you're right, and I'm also not sure wtf this code is trying to do in the first place. Is it trying to explore a directory to find all subdirectories and files? Why is it just called "file" then? Why doesn't it do anything with the directories/files it does find? So many questions!
It ends up returning the last index in the array of names from the original call. It helps to consider that every time file() is called recursively, there’s only one array element being passed. This results in that single element being returned. But the original interation doesn’t care about that, because it’s going to keep looping until it’s run through every element it was passed. Honestly? This smells like someone not understanding a finding from a security scanner like Fortify that involves unsanitized user input
Aside from the strange looking code (though I don’t mind the inverted yoda-style null check), doesn’t this result in infinite recursion if names has at least one non null value?
Sure, to make the method call you’d have to pass something in for the names param, so null if you have no names. ArrayUtils would catch that and return the first param.
I meant you could call it as file(some_file, null, null, null, null)
That would make it an array that contains 4 strings (not empty), but every single string is null, which would be ignored in the for-loop and so it still wouldn't recurse.
So to get the recursion you need to pass at least one valid name and the file has to be valid also.
Ah, I had a misunderstanding on how that isEmpty check works, assuming it would act like the same method call on commons StringUtils, where null or “” is empty.
That may be what they meant to do, or like, find the last non-null name or something. But I think that the only thing it can ever possibly return is the first parameter. It just returns its own first parameter. That's it.
What they (probably) meant to do is sequentially append the names, i.e. given input file('dir', 'a', 'b', 'c') end up with an object representing the file dir/a/b/c. This code, however, infinitely recurses because it never handles the case of a single non-null name correctly.
Other than the fact that the variable named “file” should probably be named “dir” it isn’t particularly hard to follow for anyone who understands the difference in contextual usage of different identities.
I don't really see any issue here. From the looks of things, this is just getting a file from the given directory and passing a list of subdirectories/filename as optional arguments.
If no list is provided, return the directory.
Otherwise, cycle through the given directories and step into them until you get to the last one which must be the filename/directory you want to return, in which case set the return value to that file (a file and a directory can both use the File class).
I would probably throw an exception when name == null, but otherwise this looks fine. Maybe some better variable names would help. Also, I think the first check for if the list is empty is redundant.
Where do you see this "stepping into"? This code only used the same File and copies it into other variables, but never actually steps into them. I'm pretty sure this code would get stuck if you passed it a valid file and name because it calls itself recursively without ever changing the input variables.
155
u/ddruganov Jul 15 '22
Offtopic but why the fuck would anyone write “null != file” in their code?
32
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jul 15 '22
Image Transcription: GitHub Commit Summary
[The following line was changed, with the
filefunction call being the sole change:][Changed line ends.]
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
207
u/C0R0NASMASH Jul 15 '22
The only situation where I'd write a function like this would be after a 16 hour debug session, just to get it over with...
79
u/oj_mudbone Jul 15 '22
The goal of writing Java code is to write the same word as many times as possible in each line
24
u/Naeio_Galaxy Jul 15 '22
Just to me sure, this is java and thus, the method
fileis not the same as the variablefile?16
u/SanianCreations Jul 15 '22 edited Jul 15 '22
Doesn't this code halt/recurse infinitely once it reaches the highlighted line? It calls itself recursively without ever changing the input variables.
The only way for this function to not halt is if you pass a file that's null, or if all of the names you pass are null.
9
u/funkture Jul 15 '22
Aside from the strange looking code (though I don’t mind the inverted yoda-style null check), doesn’t this result in infinite recursion if
nameshas at least one non null value?14
u/DangyDanger Jul 15 '22 edited Jul 15 '22
I thought this was C# until the flair. The lowercase method name really got me going.
Who is this person and where can I
killfindyell at them?Also,
directorydirectory10
u/[deleted] Jul 15 '22
Which file are you trying to find?
4
u/Putnam14 Jul 15 '22
Infinite recursion if there’s anything passed as
names?3
u/mirapalheta Jul 15 '22
so nice that names are actually never used, which make this code just a prank so everyone keeps looking at the code…
2
u/BigJunky Jul 15 '22
What the hell is that? Are they looping through the names and just returning the last one?
1
u/youngbull Jul 26 '22
`File file = directory;` : literal spit-take.
1
u/[deleted] Jul 16 '22
Fileception.
1
u/ollieonboarding3 Jul 29 '22
Test
1
u/i860 Jul 15 '22
Other than the fact that the variable named “file” should probably be named “dir” it isn’t particularly hard to follow for anyone who understands the difference in contextual usage of different identities.
-1
u/JimJamJahar Jul 15 '22
I don't really see any issue here. From the looks of things, this is just getting a file from the given directory and passing a list of subdirectories/filename as optional arguments.
If no list is provided, return the directory.
Otherwise, cycle through the given directories and step into them until you get to the last one which must be the filename/directory you want to return, in which case set the return value to that file (a file and a directory can both use the File class).
I would probably throw an exception when name == null, but otherwise this looks fine. Maybe some better variable names would help. Also, I think the first check for if the list is empty is redundant.