I keep tripping over “true, false, true” (allthingssmitty.com)
from codeinabox@programming.dev to programming@programming.dev on 11 May 11:20
https://programming.dev/post/50210491

#programming

threaded - newest

schnurrito@discuss.tchncs.de on 11 May 11:26 next collapse

That’s why IntelliJ shows you, in these kinds of cases, the names of the parameters where the function is called…

There are also languages, like Scala and Swift, with named parameters, which also solve this problem.

nous@programming.dev on 11 May 11:39 next collapse

You shouldn’t need tooling to fix an issue. Better for a design that does not require extra tooling.

qprimed@lemmy.ml on 11 May 11:45 next collapse

but isn’t that just using tooling to de-obfuscate code that should be clear to begin with?

passing structs, dicts, whatever keeps the code clear, expressive and extensible. if I can bake-in flexibility and clarity without being overly focused on performance, I am choosing the former every time.

riskable@programming.dev on 11 May 14:56 next collapse

If your language requires an IDE to show you WTF is going on in the code, it’s a bad language.

Given, there’s ways to write poor code in any language, but some are much, much worse than others. Java and JavaScript being the kings of that kind of thing.

Some day, AI assisted coding will become so intelligent that it will look at your average “enterprise” Java code and ask the user, “WTF were they even trying to do here?” Which is the only correct response a lot of the time.

sbeak@sopuli.xyz on 12 May 08:19 collapse

Conversely, most software developers look at LLM-gen (LLMs are not proper artificial intelligence since they don’t understand what you’re feeding it) code and say “WTF were they even trying to do here?”

Indeed, AI (not LLMs, mind you, but AI) does have its use cases. For instance, in science, there are many fields where mass processing of data by conventional means is unfeasible. And in programming, using AI to help detect bugs so that an experienced developer, knowing how to troubleshoot and with context of the project’s aims and scope, can fix the issue more easily.

LLM-gen code is very fragile and filled with loads of bugs, not to mention how the code it writes does not credit the original authors, as it ignores licensing and attribution requirements of projects that were scraped for its data set. And half the time, people producing LLM-gen code do not understand what it has produced and does not bother to review it before trying to push it to a large project, leaving the burden of filtering it out for those maintaining the project (when that effort could be directed at adding new features, fixing bugs, or doing anything else really)

brianpeiris@lemmy.ca on 11 May 20:49 collapse

IntelliJ doesn’t help when you’re doing a code review, or just reading through hundreds of lines of code, I don’t want to move my mouse or cursor over every line to see the parameter names.

Traister101@lemmy.today on 11 May 21:07 next collapse

By default inlay hints are enabled so you wouldn’t have to do that. But yeah it’s tied to using the editor which makes it a non solution

draco_aeneus@mander.xyz on 11 May 21:38 collapse

I agree with you. However, I think you’ve misunderstood what inlay hints look like. Here’s an example.

<img alt="IntelliJ inlay hint example" src="https://mander.xyz/pictrs/image/2086531c-b36d-4321-aa56-f21c06b5fc02.png">

codexarcanum@lemmy.dbzer0.com on 11 May 12:06 next collapse

Good IDEs have multiple ways of showing you this info, and many languages have named parameters that fix this. Also, just putting a comment on them is not a crime, it just won’t update when you change the function but then your IDE can often only do so much at call sites, so you often need to update the calls (and the comments) manually. I’m sure LLM agents can change both as well.

Or, in JavaScript like these examples were, yeah, just pass in an object. Passing in an object should be standard in JS when you have more than like 2 params anyway because it solves several issues with parameter identification, optional parsms, method overloading, and so on. And JS passes everything by reference so you aren’t losing performance.

SchwertImStein@lemmy.dbzer0.com on 11 May 20:37 collapse

code should be readable without an Ide

draco_aeneus@mander.xyz on 11 May 21:36 collapse

Indeed. There will be lots of times when you’ll be reading code without a while IDE attached. When doing code reviews in the browser, when looking at patch files or git diffs in the command line, when browsing code files on some git host, or when you’ve gone to a confrence and you left your laptop in the hotel room because Steve from accounting assured you it would just be a meet-and-greet with clients, but then some production bug hit and every odd-numbered request is returning a 401 for some reason, so you need to borrow Steve’s laptop to fix this.

jtrek@startrek.website on 11 May 12:58 next collapse

I use keyword arguments in Python to minimize this pain. Instead of

create_user("Bob", True, False)

it’s

create_user(name="Bob", admin=True, send_email=False)

JavaScript makes that more cumbersome with the object thing , but it’s better than nothing.

HairyHarry@lemmy.world on 11 May 13:37 next collapse

You could use jsdoc comments.

eager_eagle@lemmy.world on 11 May 14:36 next collapse

This! There’s also a longstanding open issue on rust to allow a similar way to pass keyword/named arguments. IMO every modern language should have something like this. Makes all the difference when reading code.

Inlay hints from your editor are a good help, but this should be built into the language.

FizzyOrange@programming.dev on 11 May 17:09 next collapse

Rust doesn’t need this as much because it has enums so you can just do create_user(user, Role::Admin, Notify::None).

eager_eagle@lemmy.world on 11 May 18:22 collapse

you can have a better data structure in any language, but rarely someone will bother doing that for booleans

Traister101@lemmy.today on 11 May 21:05 collapse

Right cause the boolean isn’t a named type. If you have two possible states that can be represented with a boolean, or an enum of the two possible states which embeds more info into the callsite

calcopiritus@lemmy.world on 12 May 05:38 collapse

Not saying that named parameters are bad, but the builder pattern serves the same purpose and imo it’s more ergonomic.

TrainBuilder::new()
  .with_electric_motor(true)
  .with_width(1.0)
  .build()

I don’t care about the color of the train or the amount of wheels, I just want the default train with a few changed parameters.

If you do named parameters in rust, you would need to set every parameter. The only way to not set every parameter is to give a special meaning to the Default trait. But that is uncommon to happen in rust. And many structs that could easily derive Default don’t.

riskable@programming.dev on 11 May 14:50 next collapse

Thank you for posting this comment. I came here to write the exact same thing and now I don’t have to!

👍

ugo@feddit.it on 11 May 15:16 next collapse

zig uses anonymous structs for the same effect

doWork(.{
    .flurbify = true,
    .flurbification_intensity = 1001,
});
Kissaki@programming.dev on 11 May 17:13 collapse

C# uses CreateUser(name: “Bob”, admin: true, sendEmail: false)

PrinzKasper@feddit.org on 12 May 06:02 collapse

PHP is basically identical

kibiz0r@midwest.social on 11 May 14:19 next collapse

TypeScript doesn’t really save this

TypeScript tells me the values are booleans. That’s not really the problem.

The types are technically correct. I still have to remember what the arguments mean.

You could use branded/tagged types. Probably not useful in these narrow, one-off occurrences. But if you have a concept that applies across your whole domain, they can be useful.

SchwertImStein@lemmy.dbzer0.com on 11 May 20:37 collapse

branded booleans?

[deleted] on 12 May 07:37 collapse
.
samc@feddit.uk on 11 May 19:57 next collapse

Agree with several people here that named parameters are a good solution, they add minimal overhead at the call site and function declaration and look very natural.

Another option for languages that want function arguments to have fixed size is bitmasks. I wonder if it could be a useful language feature to infer the flag names from the function declaration. Something like

def my_function(arg1, arg2, [FLAG1, FLAG2]) {
    if (FLAG1) {
        do thing
    } 
   ...
}

my_function(val1, val2, FLAG1 | FLAG2)
JakenVeina@midwest.social on 11 May 22:05 next collapse

Yup, I’ve taken to doing this exact same thing in TS/JS. That is, defining functions to take an options object instead of multiple parameters.

It’d be much nicer if TS/JS could support named parameter binding, like many other lanaguages, cause that’s what I do as a nearly universal rule, in every language that supports it.

No, having an IDE inject paramter names in the editor is not a solution. Not only does it always look like shit, because none of them have any concept of formatting for readability, it also means the code then becomes unreadable outside of an IDE (and user configuration) with that feature.

TomasEkeli@programming.dev on 12 May 08:42 collapse

Opinion: if the method takes a boolean it should be split to two methods. If it takes two bools it’s four methods in a trench-coat.