News for hackers. Stuff that matters.
Re:Please put units in names by geokon on Monday March 21, 2022 @05:40AM (#30752069)
I'm sorry, but this whole comment section is in some collective psychosis from 2010. You don't need to mangle variable names or create custom types (and then the insane machinery to have them all interact properly)

Have none of you ever refactored code..? Or all of your writing C?

Your library should just expose an interface/protocol signature. Program to interfaces. Anything that is passed in should just implement the protocol. The sleep function should not be aware or care about the input type AT ALL. All it requires is the input to implement an "as-milliseconds()" function that returns a millisecond value. You can then change any internal representation as your requirements change

Re:Please put units in names by pif on Monday March 21, 2022 @06:07AM (#30752243)
> You don't need to ... create custom types > > All it requires is the input to implement an "as-milliseconds()" function

A.k.a. custom type!

Re:Please put units in names by flohofwoe on Monday March 21, 2022 @06:20AM (#30752306)
That's also a custom type. But for libraries, custom types for everything in interface definitions are problematic. Let's say you use library A which has some functions that take a time delta, and another library B, that also has functions which take time deltas. Now both library would define their own time delta type, and you have two types for time deltas in your application and most likely need to add an additional 'meta time delta type' which can be converted to the library specific time deltas types. This will explode very quickly into lots of boilerplate code and is a good reason to use 'primitive types' in interfaces.

If you replace 'time delta' with 'strings' then the problem becomes more apparent. Each library defining its own string type would be a mess.

Re:Please put units in names by geokon on Monday March 21, 2022 @06:53AM (#30752505)
If you are working with a crappy language then that's probably true. But it doesn't have to be a lot of boilerplate and it can be local in scope. In Clojure it's a one-liner.

    (extend-protocol
    libA/stringable
    myLib/InputThing
    as-string [x] (myLib/to-string x) )

    (libA/some-func (myLib/make-a-thing))
And sure it can just be doing something as simple as returning an internal value directly or calling some common stringifying method. You do have a good point that you may have redundant stringifying protocols across libraries - which sucks.

> This will explode very quickly into lots of boilerplate code and is a good reason to use 'primitive types' in interfaces.

I feel you were so close and missed :) The natural conclusion would be to have primitive interfaces - not primitive types. This way libraries only need to agree to adhere to an interface (and not an implementation)

Re:Please put units in names by qayxc on Monday March 21, 2022 @05:52AM (#30752150)
> Have none of you ever refactored code..? Or all of your writing C?

You seem to ignore the much broader context here: the article wasn't just about code. It included configuration files and HTTP requests.

It's also worth noting that depending on the language in question and the usage pattern of the function/method, using interfaces or protocols can cause considerable overhead when simply using a proper descriptive name is free.

Re:Please put units in names by ReleaseCandidat on Monday March 21, 2022 @05:59AM (#30752192)
> [...] create custom types (and then the insane machinery to have them all interact properly)

That's an argument against languages with type-systems that make this a PITA, not against the idea itself.

Re:Please put units in names by thrwyoilarticle on Monday March 21, 2022 @08:08AM (#30753099)
'Why do at build time what you could do with bugs at runtime'?
Re:Please put units in names by gpderetta on Monday March 21, 2022 @07:31AM (#30752790)
premature abstraction is the root of all evil.