Code Smells Catalog (luzkan.github.io)
from lysdexic@programming.dev to programming@programming.dev on 05 Sep 2024 06:34
https://programming.dev/post/19005727

#programming

threaded - newest

Kissaki@programming.dev on 05 Sep 2024 07:56 next collapse

The items don’t seem concise and always clear. But seems like a good, inspiring resource for things to consider.

If it is expected that a method might fail, then it should fail, either by throwing an Exception or, if not - it should return a special case None/Null type object of the desired class (following the Null Object Pattern), not null itself.

I’ve never heard of evading null with a Null object. Seems like a bad idea to me. Maybe it could work in some language, but generally I would say prefer result typing. Introducing a result type wrapping or extending the result value type is complexity I would be very evasive to introduce if the language doesn’t already support result wrapper/state types.

lysdexic@programming.dev on 05 Sep 2024 08:56 next collapse

I’ve never heard of evading null with a Null object.

This is quite standard, and in fact it’s even a safety feature. C++ introduced nullptr defined as an instance of std::nullptr_t explicitly with this in mind.

en.cppreference.com/w/cpp/language/nullptr

This approach is also quite basic in monadic types.

Kissaki@programming.dev on 05 Sep 2024 13:08 next collapse

with this in mind

With what in mind? Evading NULL?

Languages that make use of references rather than pointers don’t have this Dualism. C# has nullable references and nullability analysis, and null as a keyword.

What does your reasoning mean in that context?

FizzyOrange@programming.dev on 05 Sep 2024 13:35 next collapse

Languages that make use of references rather than pointers don’t have this Dualism.

It’s not about references vs pointers. You could easily have a language that allowed “null references” (edit: too much C++; of course many languages allow null references, e.g. Javascript) or one that properly separated null pointers out in the type system.

I agree with your point though, using a special Null value is usually worse than using Option or similar. And nullptr_t doesn’t help with this at all.

[deleted] on 05 Sep 2024 14:34 collapse

.

FizzyOrange@programming.dev on 05 Sep 2024 16:18 collapse

Not sure I follow you…

[deleted] on 05 Sep 2024 17:14 collapse

.

lysdexic@programming.dev on 05 Sep 2024 15:50 collapse

With what in mind? Evading NULL?

Depends on your perspective. It’s convenient to lean on type checking to avoid a whole class of bugs. You can see this either as avoiding NULL or use your type system to flag misuses.

Languages that make use of references rather than pointers don’t have this Dualism. C# has nullable references and nullability analysis, and null as a keyword.

C#'s null keyword matches the monadic approach I mentioned earlier. Nullable types work as a Maybe monad. It’s the same concept shoehorned differently due to the different paths taken by these languages.

baseless_discourse@mander.xyz on 07 Sep 2024 04:53 collapse

as far as I know, C# don’t have proper ergonomic monadic bind as in F# (computation expression), Haskell (do expression), and Ocaml (let*), but I could be wrong.

NigelFrobisher@aussie.zone on 08 Sep 2024 07:01 collapse

Correct.

AbelianGrape@beehaw.org on 05 Sep 2024 13:40 collapse

“Monadic type” has something like three meanings depending on context, and it’s not clear which one you mean. One of them is common in math, but not so common in programming, so probably not that. But neither “parametric types with a single argument” nor “types that encode a category-theoretic monad” have the property you say, as far as I know.

I imagine you’re probably referring to the latter, since the optional monad exists. That’s very different from returning null. The inhabitants of Integer in Java, for example, are the boxed machine ints and null. The inhabitants of Optional[Integer] (it won’t let me use angle brackets here) are Optional.of(i) for each machine int i, Optional.empty(), and null.

Optional.empty() is not null and should not be called a “Null object.” It’s also not of type Integer, so you’re not even allowed to return it unless the function type explicitly says so. Writing such function types is pretty uncommon to do in java programs but it’s more normal in kotlin. In languages like Haskell, which don’t have null at all, this is idiomatic.

lysdexic@programming.dev on 05 Sep 2024 15:52 collapse

I think you’re trying too hard to confuse yourself.

[deleted] on 05 Sep 2024 16:00 collapse

.

[deleted] on 05 Sep 2024 16:04 collapse

.

[deleted] on 05 Sep 2024 15:50 next collapse

.

LemoineFairclough@sh.itjust.works on 07 Sep 2024 22:05 collapse

This might be educational: docs.oracle.com/javase/8/docs/…/Optional.html

There are issues that the Optional class alleviates that are common enough to be documented: jetbrains.com/…/ConditionalCanBeOptional.html (more detail is available at places like github.com/…/ConditionalCanBeOptional.html (I believe this information used to be visible with the “inspectopedia” URLs but I don’t see that today))

On the other hand, it seems there are some features / situations that require null to be present: developer.mozilla.org/en-US/…/Optional_chaining www.jetbrains.com/help/…/OptionalToIf.html

GBU_28@lemm.ee on 05 Sep 2024 14:39 next collapse

This is like SCPs but for devs

JackbyDev@programming.dev on 06 Sep 2024 23:39 collapse

You’re thinking of CVEs.

spartanatreyu@programming.dev on 05 Sep 2024 23:59 collapse

This doesn’t seem overly useful.

It’s a list taken out of a bunch of books with no regard for how something can be the best path in one language and a smell in another language.

Look at this page for example: luzkan.github.io/smells/imperative-loops

It suggests using functional loop methods (.map(), .reduce(), .filter()) instead of using imperative loops (for, for in, for each) but completely disregards the facts that imperative loops also have access to the break, continue, and return keywords to improve performance.

For example: If I have an unsorted list of 1000 cars which includes a whole bunch of information per car (e.g. color, year manufactured, etc…), and I want to know if there were any cars were manufactured before the year 1980, I can run an imperative loop through the list and early return true if I find one, and only returning false if I haven’t found one by the end of the list.

If the third car was made in 1977, then I have only iterated through 3 cars to find my answer.

But if I were to try this with only functional loops, I would have to iterate through all 1000 cars before I had my answer.

A website with blind rules like this is going to lead to worse code.

mbtrhcs@feddit.org on 06 Sep 2024 12:01 next collapse

…what? At least with Java Streams or Kotlin Sequences, they absolutely abort early with something like .filter().first().

metiulekm@sh.itjust.works on 06 Sep 2024 13:35 next collapse

Same in Python, Rust, Haskell and probably many others.

But apparently JS does work that way, that is its filter always iterates over everything and returns a new array and not some iterator object.

nous@programming.dev on 07 Sep 2024 12:55 collapse

The old methods on Array will eagerly evaluate all elements. But JS has a new Iterator type with methods that works lazily instead.

mrkeen@mastodon.social on 06 Sep 2024 13:42 next collapse

@mbtrhcs @spartanatreyu well Java Streams try to, but it's not too hard to get them to accidentally process too much, or even blow up completely.

(This isn't a comment on coding styles or the article though)

sik0fewl@lemmy.ca on 07 Sep 2024 02:25 collapse

Ya, streams may seem tedious (why do I have to call stream and collect?), but it’s like that for performance (and probably backwards compatibility).

If writing readable code is not peformant, then the language implementation needs to be fixed.

baseless_discourse@mander.xyz on 07 Sep 2024 04:49 collapse

Honestly, it is much more code to use loop with non-local control like break, continue etc. (variable initialization, append, variable mutation in loops…) than just calling a collect function (which I assume just means to_list). In the above example, in most programming language I know, you don’t even need to collect the result into a list.

Not to mention, large loops with non-local control is a breeding ground for spegatti code. Because you no longer have a consistent exit point to the loop, thus making the semantics hard o reason about.

In many languages, there are type class / trait / interfaces (whatever you want to call them) that allows lazy structures to share the same API as strict ones.

mbtrhcs@feddit.org on 07 Sep 2024 11:28 collapse

Yeah, in Java calling first() on a stream is the same as an early return in a for-loop, where for each element all of the previous stream operations are applied first.

So the stream operation

cars.stream()
    .filter(c -> c.year() < 1977)
    .first()

is equivalent to doing the following imperatively

for (var car : cars) {
    if (car.year() < 1977) return car;
}

Not to mention Kotlin actually supports non-local returns in lambdas under specific circumstances, which allows for even more circumstances to be expressed with functional chaining.

nous@programming.dev on 07 Sep 2024 12:48 collapse

These are not quite equivalent. In terms of short-circuiting yeah they both short-circuit when they get the value. But the latter is returning from the current function and the former is not. If you add a return to that first example then they are equivalent. But then cannot be used in line. Which is a nice advantage to the former - it can be used inline with less faff as you can just assign the return to a value. The latter needs you to declare a variable, assign it and break from the loop in the if.

Personally I quite like how the former requires less modification to work in different contexts and find it nicer to read. Though not all logic is easier to read with a stream, sometimes a good old for loop makes the code more readable. Use which ever helps you best at each point. Never blindly apply some pattern to every situation.

mbtrhcs@feddit.org on 07 Sep 2024 15:15 collapse

Well yes, I was simplifying because I wanted to address the main (incorrect) criticism by @spartanatreyu@programming.dev. I agree with your comment

JackbyDev@programming.dev on 06 Sep 2024 23:38 next collapse

Also, Effective Java specifically says to use streams judiciously and prefer traditional for loops in general.

CookieOfFortune@lemmy.world on 07 Sep 2024 02:30 collapse

That’s a pretty bad example since most functional frameworks include an any or some function that returns early.