Hacker News new | ask | show | jobs
by gersh 5040 days ago
Read good code, and think about how they've done it. Read all the major open source projects. Really try to understand how different people organize different types of projects. Learn different programming languages, and learn how to code in different styles.
1 comments

I feel like higher level organizational stuff could be taught in a codeacademy like format. Just simple lessons for different design patterns:

"This is the delegation pattern, we use it to _________" "This is a singleton method, it's best used for ____"

"Create an MVC structure using the strategies we discussed"

You can get that out of the Gang of Four book.

The problem is that until you're faced with a real-world software system with real-world problems - and in particular, faced with the consequences of your misuse - you'll be tempted to use them in places where they're not the best solution. It's one thing to read something in a textbook. It's another thing to wrestle with a million-line codebase and try to make sense of it.

Singletons, for instance, almost always turn out to be an antipattern in the long run. You're better off injecting your dependencies explicitly, even if you only think there is one in the whole program. Delegation is used all the time, but can also be overused, and it's important to know when you're better off modifying the original class rather than trying to wrap it with something else.

Singleton method? I'm admittedly not a DP geek, could you please tell me what it is used for and point to example reification(s)?
Just yesterday used a singleton in Objective-C due to a webservice that worked with a challenge-response system. Whenever I perform a request, I receive a code. The code is required in the next request to retrieve the data. The issue I was having, was sending lots of requests in different threads; this could cause a code retrieved in a previous request to become invalid, making an older request for data fail.

I figured that by creating a singleton class responsible for executing requests sequentially, this problem would be fixed (and it did). This singleton class internally uses a FIFO-queue to execute all requests sequentially. I think it was an appropriate solution, especially since this singleton class is used internally, not exposed through interfaces to other classes interacting with my 'framework'.

I have nothing against singletons! (I'm referring to your somewhat defensive "I think it was an appropriate solution, especially since this singleton class is used internally,...")

I was just curious about what 'singleton method' meant, as opposed to 'singleton object'. Sadly, it seems that it would be very hard to implement something similar in Python (my main language at the moment) without creating a metaclass. But I certainly saw this pattern in Smalltalk before, just didn't know how it's called.

I use these in objective C, I usually create a data object and stick the networking function in it as well. Some use core data, but I haven't found a need for it yet.

static MySingleton *sharedSingleton;

+ (void)initialize { static BOOL initialized = NO; if(!initialized) { initialized = YES; sharedSingleton = [[MySingleton alloc] init]; } }

+1 Informative. Thanks! :)