Live data from Hacker News

Ask HN: Concepts that clicked only years after you first encountered them?

news.ycombinator.com

41–50 of 946 posts

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#41
post #2

Unit testing and using dependency injection to write test-able code. I'm not sure if it was years, but it wasn't immediate. I just didn't understand why dependency injection was good at first, and not just someone's weird personal code style choice. I thought it was just people being "Enterprisey" which I'd encountered many times over the years. Once I committed to unit testing, I realized how necessary it is. Unfort…

Another solution is to eliminate classes and only use structs or similar plain objects. Makes mocking and testing functions much easier. At this point I see no reason for OOP whatsoever and consider it a big mistake.

I agree with OOP not being needed at all, but I don't agree on this being an alternative to dependency injection.

However you structure it, there will always be "glue code" that ties the nice inmutable code with the outside-interacting bits, and if you want to unit test those, dependency injection (with functions or state, not classes or instances) is still the way to go.

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#42
post #2

Unit testing and using dependency injection to write test-able code. I'm not sure if it was years, but it wasn't immediate. I just didn't understand why dependency injection was good at first, and not just someone's weird personal code style choice. I thought it was just people being "Enterprisey" which I'd encountered many times over the years. Once I committed to unit testing, I realized how necessary it is. Unfort…

I‘ll add to that: the difference between "offline" unit testing and spring integrarion testing with test containers and real application contexts + all the related concepts like @SpyBean, @Mock, @MockBean...

I always hated testing and I still do, but every time I commit to doing it right I catch so many errors before QA.

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#44
post #26

Earlier quoted context omitted.

Seven years into my career I'm increasingly convinced that the emperor has no clothes with respect to unit tests that are just transcripts of the code under test with "mock.EXPECT()" prepended to everything - 95% by volume of the unit test code I've ever read, written, or maintained.

Yeah, the mere presence of unit tests is not enough. It has to actually assert something useful. When I code review, I try to make sure I call out "fake tests".

The useful assertion to be made about gateway/repository layer code is that it gets the expected behavior out of the dependency. This is not an assertion you can make when you've mocked out the dependency. You must make it in an integration test, not a unit test. Unit tests in these layers just make assertions about "it calls this method on the client" or "it sends this string," which tells you nothing about whether doing that is actually correct.

It's relatively uncommon for handler/controller code to have logic worth testing, most of the time it's just maintaining the separation of layers and concerns by wrapping gateway/repository calls. All there is to assert about it is that "it calls this function in the next layer."

Every once in a while there's nontrivial functionality to test in the middle, and unit tests can often be a good fit for that, but in my experience it's more the exception than the rule.

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#46
post #2

Unit testing and using dependency injection to write test-able code. I'm not sure if it was years, but it wasn't immediate. I just didn't understand why dependency injection was good at first, and not just someone's weird personal code style choice. I thought it was just people being "Enterprisey" which I'd encountered many times over the years. Once I committed to unit testing, I realized how necessary it is. Unfort…

> dependency injection The term is unfamiliar to me -- is it related to "fault injection"?

Going to steal a description I wrote for a blogpost several years ago, when I had recently understood DI for the first time so it was very fresh in my mind:

https://hasura.io/blog/build-fullstack-apps-nestjs-hasura-gr...

Dependency Injection solves the problem of when you want to create something, but THAT something also needs OTHER somethings, and so on.

In this example, think about a car.

A car might have many separate parts it needs:

  class Car {
    constructor(wheels: Wheels) {}
  }

  class Wheels {
    constructor(tires: Tires) {}
  }

  class Tires {
    constructor(rims: Rims, treads: Treads)
  }

  class Rims {
    constructor() {}
  }

  class Treads {
    constructor() {}
  }
We can manually construct a car, like:

  const car = new Car(new Wheels(new Tires(new Rims(), new Treads())))
But this is tedious and fragile, and it makes it hard to be modular.

With dependency injection, it allows you to register a sort of "automatic" system for constructing an instance of "new Foo()", that continues down the chain and fetches each piece.

  class NeedsACar {
    constructor(@Inject private car: Car) {}
  }
And then "class Car" would have an "@Inject" in it's "constructor", and so on down the chain.

When you write tests, you can swap out which instance of the "@Injected" class is provided (the "Dependency") much easier.

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#47
Socialism

When I got out of my middle class bubble, made friends with people working multiple jobs and struggling to make ends meet, and experienced a period of financial instability; I began to realize that something wasn't working in our current system.

Then I started struggling with burnout and other issues and found that corporations were happy to just replace me. I also found that management wanted programmers to be replaceable cogs instead of professionals. At that point I started to suspect that the idea of a dignified professional lifestyle may not be true.

I observed that technology and products were getting worse over time. For example, Google search has become mostly useless and it's hard to find products that are made to be repaired. I concluded that the invisible hand and/or the price theory of value were not true.

Then I saw Republicans gain power and run up the national debt. I also observed that when wages actually started increasing the economy fell apart and the Fed started taking steps to prevent wage increases. I concluded that "free market" rhetoric was a lie.

At that point I looked for alternatives and found socialism. In particular the "social democracy" strains of socialism as opposed to those advocating central planning or anarchist organization.

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#48
post #38

One of the biggest realizations recently for me was realizing that a nearly all of software development is basically about turning a slow, manual process into a faster, automated process. Modern CI/CD stems from a bunch of shell commands that somebody wrote and manually executed to test an app and upload it to a server. Modern automated software testing stems from humans writing small test apps and running them to co…

To add on to this, when you think in term of software development, that's already pretty meta because you skipped level 1: Formalizing any type of process. Something that is the entry level requirement to get, well, anything done via programming, is actually only done sporadicly in a lot of not-software fields.

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#49

It took a couple years in college for me to understand entropy. Entropy in classical thermodynamics is presented in a mysterious way that leads to confusion. Entropy in statistical thermodynamics, however, is logical. Once one understands basic statistical thermodynamics, entropy isn't mysterious. The book in my statistical thermodynamics class was An Introduction to Thermal Physics by Daniel Schroeder, which is an e…

I had to study entropy twice in college for different courses that were 2 years apart from each other, and I still remember this one quote I read somewhere: The first time you study entropy, you won't understand it. The second time you study it, you'll think you understood it until you realize you didn't. By the third time you study it, you just don't care anymore and just use it. 10 years after graduating, and I hav…

Entropy is basically a useful quantity. It's no more mysterious than enthalpy or Gibbs free energy, both of which have also caused me confusion in the past.

To me, the issue with entropy is that it's initially presented without clear justification, so people don't know why it's important. Statistical mechanics made it clear to me that the state with the most entropy is the most likely to occur in equilibrium. (Statements about entropy representing "disorder" and whatnot in my opinion are handwavy, often lead to confusion, and should be avoided.)

Re: Ask HN: Concepts that clicked only years after you first encountered them?

#50
Differential equations took a couple of years to grok. I first encountered them in high school, while preparing for physics Olympiad. I could solve basic differential equations by following the "rules" (such as for dampened oscilator), but I didn't understand what was going on under the hood. When in university I did some more math courses, suddenly differential equations clicked and made sense (and I could even derive some of the rules)
Post reply on HN