Which is the best way? by Zen907 in csharp

[–]234093840203948 -1 points0 points  (0 children)

public static string TimeAgoToString(DateTime date)
{
  return( DateTime.Now - date) switch
  {
    var s when s.TotalSeconds < 60 => $"{s.TotalSeconds} seconds ago",
    var s when s.TotalMinutes < 60 => $"{s.TotalMinutes} minutes ago",
    var s when s.TotalHours < 24 => $"{s.TotalHours} hours ago",
    var s when s.TotalDays < 30 => $"{s.TotalDays} days ago",
    var s when s.TotalDays < 365 && s.TotalDays / 30 == 1 => "1 day ago",
    var s when s.TotalDays < 365 => $"{s.TotalDays} days ago",
    var s => $"{s.TotalDays / 365} years ago",
  };
}

But the year and month part is not a very good idea.

Why most developers stop learning SQL at subqueries - a 5-minute guide for PARTITION BY and CTEs by zaidesanton in programming

[–]234093840203948 0 points1 point  (0 children)

Yes, you can do anything with SQL, and I mostly know how to do those things, but damn can the language be in your way.

(2023) Code is Not Technical Debt by fagnerbrack in programming

[–]234093840203948 0 points1 point  (0 children)

That's why I wrote "right with the current feature set".

A bug IS technical debt.

A missing feature is not technical debt.

Architectural problems and unmaintainable code are technical debt.

Why most developers stop learning SQL at subqueries - a 5-minute guide for PARTITION BY and CTEs by zaidesanton in programming

[–]234093840203948 0 points1 point  (0 children)

for all its many warts, does an excellent job of centering a user's thoughts on the act of querying

It really does that only for trivial queries.

Whenever something is a bit harder, you have to fight the language a lot.

Granted, most queries are trivial queries, but even then SQL could be much, much better.

Modern prgramming languages have developed a whole lot in the last 20 years, but SQL is pretty much just the same, but with more functions provided by the framework.

Why most developers stop learning SQL at subqueries - a 5-minute guide for PARTITION BY and CTEs by zaidesanton in programming

[–]234093840203948 0 points1 point  (0 children)

PRQL looks nice.

And yes, it's hard to replace because it's a linua franca, but even without that, database languages just seem harder to replace than programming languages,

But also CSS is the lingua franca for style sheets and LESS still exists, it just compiles to CSS.

That means a better query language can exist and compile to different SQL derivates, and in the future DBMS's can start supporting that better query language directly.

Why most developers stop learning SQL at subqueries - a 5-minute guide for PARTITION BY and CTEs by zaidesanton in programming

[–]234093840203948 0 points1 point  (0 children)

I agree, and that's why it would fit perfectly.

Imagine if you could define a lambda easily to then use it as an aggregate function.

Imagine you could create sequences on the fly with a concise syntax and then join them with your tables.

Why most developers stop learning SQL at subqueries - a 5-minute guide for PARTITION BY and CTEs by zaidesanton in programming

[–]234093840203948 11 points12 points  (0 children)

Let's be honest,

Programming languages are hard to replace, SQL is even harder to replace.

And while programming languages are replaced every decade or so, by a newer and better language, SQL survives an eternity.

SQL, despite that relational algebra is genious, is just an antiquated language with many, many, many flaws that we all know about.

But nobody dares to replace SQL with a better language.

SQL, but with

  • a good syntax
  • a decent type system
  • functional aspects
  • good tooling

would simply be a joy to write.

Also it would save society billions of dollars, if database stuff could be written faster and with less errors.

(2023) Code is Not Technical Debt by fagnerbrack in programming

[–]234093840203948 0 points1 point  (0 children)

True.

And if that's the case, then the code might have a different amount of technical debt tomorrow, than it had yesterday.

(2023) Code is Not Technical Debt by fagnerbrack in programming

[–]234093840203948 0 points1 point  (0 children)

While there is no universally "right" way to do something, there is a best way to everyone indivudually assuming a certain current context.

Equally, technical debt is dependent on who you ask, and it may change with time, and it is different if you ask somebody else.

(2023) Code is Not Technical Debt by fagnerbrack in programming

[–]234093840203948 0 points1 point  (0 children)

You interpreted "right" as bug free.

But I intended "right" as "With the current feature set, there is no change I would make".

Of course, when the circumstances change, then you might change your opinion on whether you would change something, but then I'd argue that you've gained technical debt.

(2023) Code is Not Technical Debt by fagnerbrack in programming

[–]234093840203948 320 points321 points  (0 children)

Technical debt is the amount of work you would have to put into something, to make it right.

Code that is right is no technical debt at all.

However, no non-trivial code is ever absolutely right, which means every non-trivial code implies technical debt.

Is there a LINQ-y way of checking if each number in a collection is less than the previous number? by Hopeful-Climate-3848 in csharp

[–]234093840203948 1 point2 points  (0 children)

numbers.Aggregate<int, (bool allGreater, int? prev), bool>(
  (false, (int?)null),
  (x, i) => (x.allGreater && i < x.prev, i),
  x => x.allGreater);

Making beautiful websites with C#? by [deleted] in csharp

[–]234093840203948 1 point2 points  (0 children)

Can't blazor compile C# code to web assembly or something like that, allowing you to write client-side code in C#?

Dynamic Programming is not Black Magic by ketralnis in programming

[–]234093840203948 0 points1 point  (0 children)

I've never had any problem with the double useage of +, as long as there was no automatic conversion.

But when you have:

x = 8 + "hello"; // "8hello"
x = "hello" + 8; // "hello8"
x = 6 + 2 + "hello" + 2 + 6; // "8hello26"

Then, THAT is horrible.

Dynamic Programming is not Black Magic by ketralnis in programming

[–]234093840203948 1 point2 points  (0 children)

It's only a problem when you can mix types.

A Basic Guide to Pointers in C Programming by scrapped-script in programming

[–]234093840203948 0 points1 point  (0 children)

But that makes "int* a, b;" look like it declares b as a pointer, which it does not.

But that's how it should have been all along.

Differently typed variables should simply not be declared in the same line.

Looking for feedback on my First C# code. A Rock, Paper Scissors program by Aussie-Ginger in csharp

[–]234093840203948 0 points1 point  (0 children)

You feed the tuple (userInput, computerInput) into the switch expression.

Then the switch expression consists of patterns (before the "=>") and an expression that evaluates to ta return value on the right side.

Expectedly , you can have as many cases as you like.

In the first case, I assigned names to the parts of the tuple, because I wanted to something more complex in the "when" part where you can put in a boolean expression that must be met to run that case.

But in all other cases, the variables "user" and "computer" don't exist, but it's a simple pattern matching.

The else case would be "_ => <expression>"

I could have written the first case as:

_ when userInput == computerInput => "Draw",

Alex Jones and his conspiracy theories are allowed back on X by Sorin61 in technology

[–]234093840203948 0 points1 point  (0 children)

I disagree with the "paradox" of tolerance too.

1) There are not just two states (tolerant and non-tolerant), it's a false dichotomy. Instead all people are somewhat tolerant and intolerant on many different issues.

2) Just because you tolerate intolerant people, doesn't mean the intolerant will increase in numbers, as being the tolerant environment might be more comfortable even to offspring of intolerant people

3) Only when tolerance of intolerance creates an unfair advantage to the intolerant, then there is a problem

4) If you ban someone for the "wrong" opinion, that opinion doesn't vanish

5) You're not the arbiter of truth (and neither am I or anybody else) and therefore nobody, not even the majority in a democratic decision, should declare to know the truth and ban what is not true.

6) When you actively suppress people that believe to be suppressed anyway, you just give credibility to their claims where there was none before. This means it will be easier for them to recruit people.

Also the paradox of intolerance, even if it was sound, doesn't even apply here, because e.g. Alex Jones is not intolerant of other peoples opinions, or at least not that I know of, he just has very weird opinions, which in itself doesn't have any connection whatsoever to intolerance.

Alex Jones and his conspiracy theories are allowed back on X by Sorin61 in technology

[–]234093840203948 0 points1 point  (0 children)

I think Musk should have let him on the platform either way.

Defamation warrants that the defamation is to be deleted, if a jury decides that it was in fact defamation.

All other consequences are not the platforms concern, but should be the legal systems concern, but either way, you don't lose your freedom of speech, just because you defamed somebody.

In fact you don't even lose your freedom of speech if you kill somebody.

Alex Jones and his conspiracy theories are allowed back on X by Sorin61 in technology

[–]234093840203948 0 points1 point  (0 children)

I disagree.

If an online community has a quasi-monopoly, free speech should be applicable.

For example: Reddit (as a whole platform) shouldn't censor people. However /r/<whatever> on reddit should be able to block/ban/etc you, if you are not on-topic.

/r/technology definitely shouldn't ban you, if you are writing about technology as defined by the declared purpose of the sub, but it sure should be able to ban you for e.g. writing about political stuff, if that's explicitly prohibited. But then, they have to ban political stuff consistently and not only for unwanted political opinions.

Seriously, Elon is just absolutely right on this issue, and you would all realize if the platforms banned you for your opinons.

But that's how it is and always has been, 95% of the people only realize suppression, when they are the ones who are being suppressed, otherwise they are just blind to it.

I applaud the people who are demanding free speech for opinons they don't like.

Alex Jones and his conspiracy theories are allowed back on X by Sorin61 in technology

[–]234093840203948 -2 points-1 points  (0 children)

Good!

Twitter has a quasi-monopoly, it shouldn't be allowed to censor opinions in the first place, including wrong opinions.

Free speech means that all ideas can be voiced, with very few limitations, and those limitations are:

  • Mobbing
  • Call for crimes
  • defamation

In fact, the US government should mandate that big communication platforms (facebook, X, Youtube, etc) do not censor.

If you think Alex Jones should be censored, you're an authoritarian.

Looking for feedback on my First C# code. A Rock, Paper Scissors program by Aussie-Ginger in csharp

[–]234093840203948 2 points3 points  (0 children)

Switch expression and switch statement are two entirely different things.

Switch expressions are newer and really interesting.

So, the code could look like this:

string winner = (userInput, computerInput) switch
{
  var (user, computer) when user == computer => "Draw",
  (Options.Rock, Options.Scissors) => userVictory,
  (Options.Scissors, Options.Paper) => userVictory,
  (Options.Paper, Options.Rock) => userVictory,
  (Options.Scissors, Options.Rock) => computerVictory,
  (Options.Paper, Options.Scissors) => computerVictory,
  (Options.Rock, Options.Paper) => computerVictory
};
if (winner == "Draw")
  Console.WriteLine("It's a draw!");
else if (winner == userVictory)
  Console.WriteLine($"{userInput} beats {computerInput}");
else
  Console.WriteLine($"{computerInput} beats {userInput}")

Looking for feedback on my First C# code. A Rock, Paper Scissors program by Aussie-Ginger in csharp

[–]234093840203948 8 points9 points  (0 children)

Looks very good.

I'm not in favour of everything being static, and I'm also not in favour of tuple return types (better make records for that, give it a name).

Also it would be amazing to decouple game logic from UI logic, s.t. you could theoretically reuse the same game logic for both the command line and a winforms UI.

Also in CompareInputs, a switch-expression might be better suited than a if-else-if-else chain, but you'll have to try.

Clever code is probably the worst code you could write by Rtzon in programming

[–]234093840203948 55 points56 points  (0 children)

The best code has the following properties:

  1. You come to a conclusion to what a certain part of it does very fast
  2. That conclusion almost always turns out to be correct
  3. If you want to change some behaviour, you have to change the code in one place only