Hacker News new | ask | show | jobs
by ouija 1433 days ago
I prefer the Python code here. The import is explicit, so if you want to know where "date" comes from, that's easy. In a modern IDE I don't write imports manually anyway.

You could write it as a one-liner in Python as well, but I would prefer not to.

  print("It's Saturday") if date.today().weekday() == 5 else None
That the Python library doesn't define constants for days is unfortunate, but not a limitation of the language.
5 comments

Why do I need to remember that Saturday is 5?

    >>> import calendar
    >>> calendar.SATURDAY
    5
Yeah, I'll take `Date#saturday?` thanks.
¿Por qué necesito recordar que el sábado es "Saturday"?
Rest of the language doesn't bother you?
Porque en ese caso no usas un IDE moderno.
Tampoco "if", "for", "while"...
You are allowed to define your own constants.
S looks like 5 if you squint. not hard to remember
So Sunday?
No! Sunday is 6 because "Sunday" has 6 letters.
That's it, I'm switching to Python!
The ruby oneliner looks way cleaner and more natural than the python one.
Would you ever use either in a real codebase though?
Well, yes. In fact it's the recommended style for simple conditions.
As opposed to a fake codebase? This sounds a bit like https://en.wikipedia.org/wiki/No_true_Scotsman.
As opposed to either a snippet in a REPL or a comment on hacker news.
The code is part of a standard, used-almost-everywhere library, `date`. Code in these libraries, from what I undersand, have a ton of thought behind them. It might follow that there were good real-life use-cases ready to use the code that influenced its presence in that library?

Also: https://github.com/search?l=Ruby&q=Date.today.saturday%3F&ty...

So the answer to the original question was simply “yes”.
No, not as opposed to a fake codebase. I think it's clear what they meant.

One incredibly common alternative to a codebase is a script.

Ok, I'll bite. I don't, I think it was a knee-jerk reaction post that didn't think things through for more than a couple seconds. I'm guilty of many of these, it's not the end of the world.

I know many "scripts" that have far more curation, history, and accumulated effort than any "codebase" sensu your post, I don't think this has any relevance to his question. Use is use. "Real" or "Fake" in terms of a standard library like "date" (see my other post) just isn't a useful dichotomy IMO.

I'm not a fan of gatekeeping either, but on HN the proper etiquette is to give the poster the best possible reading of their post.

I just interpreted their post as talking about bigger, organized projects, instead of small scripts.

I personally draw a distinction between "codebases" and most everything else. One of the key differences is "would I start a new git repo?"

"Real projects" get a repo for sure.

It's not too diminish smaller projects. Almost all of the most useful things I've ever built were very small. But when you can grok the whole script, and you're likely to be the only user..."codebase" isn't the word I'd bust out.

I'm sure you can think of many exceptions to this. Thinking of exceptions is a good quality for programmers.

I don't think the word "real" (codebase) was a very important modifier.

Agreed, in complicated Ruby packages with layers and layers of dependencies it quickly becomes a nightmare to identify which class comes from which import, where it’s defined, etc. Never an issue in python
Preferring the Python import syntax doesn't preclude preferring the Ruby library design.
The else None is redundant, though. If you want a one-liner in Python just ignore the indentation:

    if date.today().weekday() == 5: print("It's Saturday")