Hacker Newsnew | comments | show | ask | jobs | submit | lazydon's commentslogin

For those complaining about the "quality" of this piece - that somehow it degrades his past essays - its a response like others here: http://paulgraham.com/kedrosky.html Where even in past he usually explains when he thinks he is being misunderstood.

For that matter, I think this was more of a response too: http://paulgraham.com/speak.html (written when suddenly the whole world seemed to be worried about pg saying um a lot). I think this article did calm those people a bit.


For me the route was via StackOverflow (SO). I looked for a question like what other sites are there like SO for gaining some real world wisdom.

I've always found good stuff with this technique - It's like poor man's machine learning algo applied to oneself. People who liked this item also liked...

One such thread of my current favorites looks like:

SO --> HN --> Reddit --> Less Wrong --> Yudkowsky's blog --> Marginal Revolution blog


We have an expression for this in Hindi I’m not sure if it translates well – trying to use a sword as a needle. To use mindfulness which is transcendental in nature and can open secrets of the life for few more bucks! Yes, as someone becomes mindful, it does helps to concentrate and focus more and be more creative and productive – but that’s a side product and yes, we still have to earn bread. But to make that the entire aim is not the right direction for that energy.

But Zen is such an open philosophy it won’t mind it and judge it as corrupt or otherwise. It will allow people to naturally see the difference – that they are giving away their diamonds in return of pebbles. Not profitable in my opinion.


Whenever I read a story like this I inevitably remember Greenspan. Its always good to start with an extreme to begin with whats wrong with most systems today.

As he once said - there shouldn't be any law against financial fraud; as the markets themselves will take care of that.

And he himself got away with all the mess by saying there was a flaw in his model of the world.


The post is very negative. If you take away from someone what you think of as false, don't run way without providing a better alternative. Even if that is just thinking for yourself and not to follow anybody.

Yes, most blog posts go on to follow the same path as that of so called self-help books. But once in a while a sincere anecdote can make you think of fresh perspective on how to handle a situation in which you did not do better.

I think we as a generation should feel lucky to be living when Experimental Psychology is blossoming. Books from authors like Daniel Kahneman, Dan Ariely, Daniel Gilbert have had a life changing impact on my thinking. To really understand that our brains are not perfect - but are infact a kludge - makes you think about taking necessary precautions about these limitations.

And for productivity, number one thing that we need to outsmart is our impulsiveness.


The post is negative because the productivity posts are bad and make HN worse.

The alternative is to post ANYTHING other than "These 5 weird things are making you procrastinate" and similar.


Negativity that suppresses distractions is good.


Oh God! Please no. For someone, who lost his dad in an accident due to a distracted scumbag, this is heart breaking.

Just say no to phones (in any form) while you drive.

Multitasking is a myth. Try driving on a new road while chitchatting to someone siting next to you; or, even try walking fast while thinking something hard or even recollecting something.


I wonder how the author keeps such a long list in his mind while working. I'm not sure how, when his brain is actually not cooperating, he would say "brain, here's the remedy, so stick to it". Most points have this pattern "don't..", "resist..", "seize..". Whom are we kidding - as if its that easy directing our quirky mind.

I'm reminded of following Daniel Kahneman's closing notes in his books 'Thinking Fast & Slow'. So, after listing all biases and quirks (to which he has devoted his life), he writes:

What can be done about biases? How can we improve judgments and decisions, both our own and those of the institutions that we serve and that serve us? The short answer is that little can be achieved without a considerable investment of effort. As I know from experience, System 1 is not readily educable. Except for some effects that I attribute mostly to age, my intuitive thinking is just as prone to overconfidence, extreme predictions, and the planning fallacy as it was before I made a study of these issues. I have improved only in my ability to recognize situations in which errors are likely: “This number will be an anchor...,” “The decision could change if the problem is reframed...” And I have made much more progress in recognizing the errors of others than my own.


Singletons are always a lazy solution to the problem. For me the rule of thumb is if there's some class requiring lots of work to test, I've been lazy. So, go back and review my sloppiness.

The questioner was in a way implementing a poor man's cache using Singletons (I wonder how was he planning to test it); some use it for connection pooling instead of a specific library.

I've heard even GoF regretted including Singletons in the s/w patterns club.

I'm a big fan of Misko Hevery's (now working on Angular) work on this.




I miss this guy in Java world.


>"For me the rule of thumb is if there's some class requiring lots of work to test, I've been lazy."

Unit tests are great, but when they get in the way I always look it as a chicken and egg paradox. Is my simple and easy to maintain code inferior because it is not test friendly? Or is the test friendly alternative inferior because is more complex and harder to maintain?

And this haven't happened to me just with singletons, sometimes unit tests just like to get in your way.


I find the biggest barrier to unit tests is not noticing when it's more appropriate to create a public static function (probably on another class) instead of a private one. If a private method is so complex that you cannot appropriately test it using the public interface, then it really needs to be its own entity.


I could be a bit off base, and to be honest am only now coming to grips with unit testing fully, but I find if something is painful to test my implementation wasn't good to begin with. I'm guessing this is where writing tests first really comes in handy as you have to implement in the right way from the start.


Something is bugging me about the examples he's using there, I've never seen singletons used that way. these are just static initializers.

  testCreditCardCharge() {
	  CreditCard c =  new CreditCard(
	    "1234 5678 9012 3456", 5, 2008);
Instead I would expect a singleton to be used this way. Which takes care of initialization where its needed.

		class CreditCard {
			private number;
			function charge(amount){
				processor = CreditCardProcessor.getInstance();
				processor.go(number, amount);
I'm not saying this is better way to process this datamodel, but it is the correct way to use a singleton.


There's not really a whole lot of difference between this:

    processor = CreditCardProcessor.getInstance();
    processor.charge(number, amount);
And this:

    CreditCardProcessor.charge(number, amount);
The only difference is whether the state of the CreditCardProcessor is stored statically on the class, or is stored in one magical instance. They will both have the exact same pathology in the code and during testing. The former will be slightly easier to refactor, since at least it's already based on instances.


But the whole point of the singleton pattern is to solve these exact issues. To enforce a single entry point that is 1. Lazy evaluated to ensure initialization. 2. Preferably locked to solve threaded access. 3. Instance based to ensure you are manipulating an object vs truly static variables.

I honestly think there's a huge potential difference between

  CreditCardProcessor.charge(number, amount);

  CreditCardProcessor.getInstance().charge(number, amount);


Solve what issues? I did not mention any issues. I see no difference between the two, other than one basically enforces initialization by gating it through a "getInstance" call. (And thereby also enforcing an additional function-call penalty on every functionality presented. Nothing is free.) I could do the same, with the same trade-offs, by simply invoking init() at the start of every method call.

To address your points specifically:

1. Whether the item is lazily initialized or not is an implementation detail. In fact, the call of "init" on the first format is lazy initialization; an eager initialization would have completed at class-load.

2. Either method can implement exclusionary locks. There is no difference here.

3. This is the big difference between the two, but you write it as if it's a benefit unto itself. From an API perspective, why do I care whether I'm manipulating an variables that live on an object instance or statically on the class?

Here's an exercise. How would you even know whether the code to a Singleton class looks like this?

    class SomeNumber {
        private static Integer value;

        public static SomeNumber getInstance() {
            if (value == null) value = 5;
            return new SomeNumber();

        public Integer getValue() {
            return value;
If you cannot tell whether the Singleton class was implemented this way from the external API, then there is no difference between the two implementations.


Invoking init at the start of every method call won't be caught by the compiler if you forget to do it in one spot. Static safety is usually a win - on a big project, someone will do something wrong and it's best if it's caught right away when they try to compile.

I've actually been wanting ways of enforcing arbitrary rules on the structure of my code at compile time...


Honest question as I'm wondering if I could have done something differently.

I have a third-party library that has expensive initialization done inside of its class's parameterless constructor. Once created the library's instance is fully thread safe. Because I only wish to incur the initialization cost once I have a straightforward singleton to wrap the instance:

    hold mutex
    if instance is null:
      instance = ExpensiveLibraryConstructor()
    return instance
Now the code I'm writing code plugs into a framework I don't control. I create a class that conforms to the framework's client interface and the framework creates instances of my class and calls a Run() method. My Run() method needs to be able to make use of the third-party library, so I'm calling my singleton's GetInstance() method there.

Not that it would make much practical difference but ExpensiveLibraryConstructor() needs some environment initialization done before it will succeed so I cannot invoke the constructor in global scope.

Do you know of a better way?


>Singletons are always a lazy solution to the problem

The fact that you said x is always y in programming is troubling. Sure singletons are usually a lazy solution, but saying there isn't a single case EVER where a single is the proper solution is ridiculous.


> saying there isn't a single case EVER where a single is the proper solution is ridiculous.

Is it? Some things are simply mistakes to and through. Consider nailing your penis to a table, is it ever the right solution to anything, aside from "how could I get my penis nailed to a table"?


I really loved Misko's blog back in the day, and these are the exact articles I always quote to people when they ask "why is the singleton pattern a bad thing?".


wait. isn't angular full of singletons? I mean that's what a service is in angular. I just wrote like three of them.


As the author would say: singletons (single instances), yes. Singletons (as in the pattern), no. In his articles, it appears that he uses the word 'singleton' to mean 'Singleton' though, which is mildly annoying.


That great stuff and especially when writing after about two years of no posts on his blog.


Peter Schiff explains this by taking example of two trading islands in his book: http://www.amazon.com/How-Economy-Grows-Why-Crashes/dp/04705... It's a fascinating book that mostly appeals to Austrians though but a good one to grasp fundamentals.

Also, Sal Khan has this video on the topic: https://www.khanacademy.org/economics-finance-domain/core-fi...



Applications are open for YC Summer 2015

Guidelines | FAQ | Support | Lists | Bookmarklet | DMCA | Y Combinator | Apply | Contact