People have IDEs policing their language now?
I never understood the pearl clutching that happens over code. If I leave a comment describing something as weird, and the original author gets offended somehow, that simply isn’t my fuckin problem.
If they choose to. I have the natural language suggestions enabled since it often helps me make my comments flow better. For this suggestion I’d add it to the ignore list, which you can always do.
Of course you are not getting a post for the vast majority of actually useful suggestions it gives, like fixing accidental commas or typos (that result in a valid word, but one that doesn’t make grammatical sense where it is).
Forget the comment, the design is already awful from just three lines of code.
I’d love to know why you say that. I’m learning the language and could use some insights
We are missing a lot of context but strong type systems give you the superpower to make unwanted state unrepresentable:
Here we can see the class member
invalid
being used as a flag to express that this method didn’t work out as expected. It is very easy for someone else down the line to ignore/forget (/never even knew about it) to check thatinvalid
flag and just use the result regardless, which will likely lead to more failures later on.Instead the author should directly return something that indicates invalidness. This could be as simple as returning a boolean or especially if this is a constructor it would be better to throw an error.
That way the invalid class never even gets instantiated and is therefore impossible to misuse. All validated by the type system.
It could be an OUT variable? (Does java have them?)
Not as far as I’m aware of. You can kinda fake it with references like you would in C++.
With all the missing context it would also be possible that the author is trying to track invalid whatevers and the
invalid
flag is actually wanted state.In Java, all objects are passed to methods ‘by reference’, and there is no way to mark them as immutable. So strictly speaking, they’re all ‘out variables’. This is the cause of a lot of mistakes in Java, where you eg. pass a list to a method, which then mutates it in some way. That will change the original that the caller passed in, which is normally unintended and may break class invariants. So Java tends to have an absurd number of ‘safety copies’ and immutable wrappers of collections.
I’d probably describe the inability to mark things immutable as the main problem with Java. The golden rule of concurrency is that if you share mutable state, you must use an appropriate synchronisation primitive. It’s not easy to mark things immutable (
final
doesn’t do whatconst
does in C++) and although you can make class internalsprivate
if you like, the junior devs at my work will come along and add accessor methods.tl:dr; yes it does. Passing an
AtomicBoolean
as a method argument will do as a built-in ‘mutable object that holds a boolean and can be checked by caller’, although it’ll be slower than your own custom object since it does sync you won’t need.I’m from C# that has the following for your list example:
// If you add an item to this list, it will effect usages outside. You can't reassign it though. public void Example(List<string> exampleParam) //Full passing by ref, if you re-assign it to a complete new object, outside usages will be effected public void Example(ref List<string> exampleParam) //Output only, this acts as if the method assigned a variable named exampleParam. public void Example(out List<string> exampleParam)
How do you know “the language” here? The snippet would be valid in many languages that IntelliJ supports.
First, I was exaggerating for humor, partly to make fun of the example and partly or make fun of programmers who would criticize even three lines of code out of context, because they like to feel smart. 😉
Even so, if I played “What’s not to like about this code?” I would point out a few things to look at more closely.
Writable shared memory always draws my attention. It’s not bad, but I tend to prefer to minimize it. A writable field in an object is often perfectly fine and sometimes it’s a signal of a poorly-drawn boundary between methods.
Writing to a field, then immediately
return
ing falls into a similar category. Often this is perfectly fine, but sometimes it’s a symptom of a misdrawn boundary between methods. I would prefer a function or method that calculates and returns the value, then separate code that assigns it to a field. That makes the calculation easier to inspect, such as with a test.Naming a boolean value for the “no” state always feels strange to me. I always prefer to name it for the “yes” state, so I would challenge the name
invalid
for that field.Having a boolean value to represent that a thing is valid tends to be a step in the direction of a little parser. Often, the design gets stuck at a spot where there is a boolean combined with another variable in such a way that the values of those variables is commented in some way. That design can almost always be improved by introducing a little parser that returns Maybe or Either or throws an exception. (https://blog.thecodewhisperer.com/permalink/a-guard-clause-is-maybe-a-tiny-parser)
Naming an argument with the prefix
arg
is usually either following an obsolete habit purely out of habit or a survival tactic for identifying arguments to loooooong functions. Sometimes we do this because we have to, but I would almost always prefer to split up the function to make this naming convention unhelpful and unnecessary.And it’s very important to clarify that I raise these as areas of interest for further attention. They are at most risks. I can think of good reasons to do all these things, but I would tend to treat them as very temporary choices that I’d probably prefer to change sooner than later.
Was any of that helpful at all? Any further questions about it?
Yes this was a super interesting read as were most of the other replies. Thanks for sharing some info!
I glad it helped! Peace.
Not really software gore. This is a setting you can toggle in Jetbrains IDEs. If you want natural language suggestions then this is what you signed up for.