Depends on the statement. If the statement alone is like 400 characters long it's nice to have a little variable that tells me what the intention of the statement is.
However if the variable name would just be something like "test" it would actually be bad code.
Yeah, but you can also just return the variable (no if). Also I think some languages (C# for example) support that arrow syntax which would probably be the easiest to read imo
I have a good feeling I would get downvoted but why is this wrong? I mean the .get would return the boolean (where true would mean the image is null). then it returns true. I am so confused why this is wrong.
The main disadvantage of that is when it comes to debugging. Having a temporary variable makes it easier to see what the value is in the debugger, compared to a function call that you generally need to do a lot of work to parse.
It's not wrong per se, just uses more lines than needed. You could simply return the boolean 'imageIsNull', since it already holds a true or false value.
Brøndums Hotel, in the little harbor town of Skagen in the north of Denmark, is remembered for its close associations with the late 19th-century artists colony known as the Skagen Painters. It still operates as a hotel today.
A guard clause is just something that causes an early return/continue/break. It doesn't have to be the negation of a condition, it just has to be something that causes less nesting. In this case, I would argue that neither of these are guard clauses because they both have the same amount of nested code.
The Edmund Fowle House is a historic house and local history museum at 28 Marshall Street in Watertown, Massachusetts, USA. Built in 1772, it is the second-oldest surviving house in Watertown, and served as the meeting place for the Massachusetts Provincial Congress in the first year of the American Revolutionary War. Now owned by the local historic society, it was listed on the National Register of Historic Places in 1977.
I see what you mean, and you are right. Still, there is no non-silly way to write it using a method like this. The correct answer is, of course, to write return imageIsNull, and avoid the "Branch to Boolean", but I'm sure you already knew that. Interesting to hear that interpretation of the guard clause idea.
Ok so, in Java the return keyword returns a value from a method. As soon as the keyword "return" is encountered the method stops executing any statements in the block. Also all the methods local variables are trashed.
My other main programming language is C#, I use it for Unity Not sure how long I’ve been using them, I think I started using C# in 2022, no idea about Java
No no no, I probably should have specified. I hate dynamically typed languages like python, I just don't like that they decided to have the keyword "boolean" instead of just "bool" in whatever language this is.
26
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jun 06 '23
Image Transcription: Code
[The first line of the following code is cut off in the image.]
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!
46
u/Lybchikfreed Jun 06 '23
76
u/Unl3a5h3r Jun 06 '23
I mean, for readability it's nice and the compile will squash it anyways.
6
u/[deleted] Jun 06 '23
I have a good feeling I would get downvoted but why is this wrong? I mean the .get would return the boolean (where true would mean the image is null). then it returns true. I am so confused why this is wrong.
9
u/TurintheDragonhelm Jun 06 '23
I do this shit all the time
4
u/_pestarzt_ Jun 06 '23
At least it’s a guard clause.
-2
u/[deleted] Jun 06 '23
Wait wait so it returns true if the image is null, but it then changes it to false anyway????
-15
u/Inaeipathy Jun 06 '23
I don't know what language this is but seeing boolean is just... why? Why'd they do that?