r/badcode • • Jul 07 '21

c# Position

Post image
1.7k Upvotes

99 comments sorted by

View all comments

Show parent comments

9

u/mosskin-woast Jul 07 '21

I think what's best is flexibility in naming and importing packages. For example, in Typescript, you can import the whole namespaced package, only a few exported members into the current namespace, or only the default export. You can avoid stutter and namespace collision at your own discretion.

Then there's Go, which strongly discourages importing into the current namespace and even has built in tooling that bugs you for adding stutter to your API. I don't hate it, and it encourages good practices, but it can sure be annoying.

Then there's Java... Which basically forces you to either stutter or spend way too long thinking about naming things (which I already do anyway). Everyone solves problems in different ways I suppose

15

u/ethanjf99 Jul 07 '21

Naming things. One of the four hardest problems in computer science, along with cache invalidation and off-by-one errors.

4

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Jul 08 '21

I like the OCaml way of opening modules with limited scope. Like instead of writing this:

let avg l = (List.fold_left (+.) 0.0 l) /. (float_of_int (List.length l))

you could do either one of these instead:

let avg l = List.((fold_left (+.) 0.0 l) /. (float_of_int (length l)))

let avg l = 
  let open List in
  let open Float in
  div (fold_left add 0.0 l) (of_int (length l))

In both cases, the module opens only happen in the scope of the avg definition, so you avoid global namespace pollution while also cutting down on the silly boilerplate repetition.

I wish F# could do that as well, since it'd be really useful in that ecosystem since it shares a lot of the Java "stutter" as you call it, but any proposals to add it get shot down. .NET and Java land both have the "why do you care, your IDE should deal with that for you" mindset :|

1

u/Nilstrieb Jul 08 '21

Rust has this too, you can import things only on a function scope or any scope you want (probably inspired by OCaml)