Posts tagged ‘Stackoverflow’

An open source StackOverflow clone by a n00b web dev

I wanted to share a small learning exercise I underwent recently. I decided to learn how to build a website, and share the experience here. Lacking an original idea at the moment, I decided to create yet another StackOverflow clone – not original, but a good exercise nonetheless. The code for the project is hosted at GitHub, although I don’t have a live showcase up at the moment. Yes, I am aware a google search reveals an existing open source SO clone called Stacked – I thought building one from scratch might teach me more than reading someone else’s code base (I could be wrong, but this is how I wanted to roll).

Day 1

The choice of database engine was easy: The one database I had any experience with was mysql, and being open source, free and easy to work with, I went with it. Next, an ORM library. After some digging, I’ve decided to go with NHibernate + Castle ActiveRecord. ActiveRecord is great for easily mapping simple classes and relations, and NHibernate to complement it in ‘advanced queries’. I don’t get any LINQ magic from these ORMs, which is a pity. I then proceeded to create a solution and some projects, added NUnit as a test framework, and setup Castle MicroKernel as an IOC framework.

I tried to found an open hosted source CI server, but didn’t find anything that worked. Oh well, not essential for now.

Day 2

Before proceeding any further, I had to choose a source control provider. If this were a production project I would probably have gone with SVN hosted on Google Code. However, Ken told me about Git long ago, and I thought this was a good chance to experience it. So, on to GitHub. Opening the project was a no brainer, but finding a decent client was more challenging. I had some fun learning about Git’s private keys, and configuring TortoiseGit (I tried GitExtensions, but it doesn’t support Visual Studio 2010 beta 2 yet). Overall, TortoiseGit gets the job done, after some tweaking and .gitignore files.

Git’s distributed source control model is interesting and worth a try.

I created the User, Question & Vote tables, learned about composite keys in AR, and wrote my first NH query:

GetVoteCount – “SELECT Vote, COUNT(*) FROM ” + VotesTableName + ” WHERE PostId = :postId GROUP BY vote”

I currently don’t have any caching on the vote count – am simply storing the votes as relations between users and questions, and counting them on the fly.

Day 3

I would like to implement full features, not write the entire DAL first and then the application logic. So, it’s time to start learning about web development. At work people are using Monorail, but after reading this question I decided to try out ASP.Net MVC instead. So, I read a basic tutorial and starting coding. Some things I learned:

  1. I finally got the meaning of Global.asax.cs – it’s simply the ‘main’ of the web application.
  2. By default, ASP.Net MVC creates the controllers by itself and does not support IOC. Fixed (remember to setup the Controller’s lifestyle as LifeStyle.PerWebRequest).
  3. Some Asp.Net MVC basics:
    1. Use <%= … %> to write to the output stream (that gets sent as the HTML), and <% %> to simply execute code.
    2. Use Html.RenderAction() to create links to other pages (~= Actions)
    3. Your pages are butt ugly without tweaking the CSS

image

Day 4

  • I quickly caught myself duplicating code, and turned to learn about Partial Views, which are reusable View pieces.
  • I realized that having my model entities derive from ActiveRecordBase is damn ugly, because it makes my entire application dependant on AR even if I was using repositories to access the data. I switched the repositories to using ActiveRecordMediator instead.
  • The magic that is ReturnUrl – an extra request parameter that controllers use to return you to your original page after you login.
  • An interesting usage for anonymous object creation syntax in C#, to pass query parameters: new { ReturnUrl = “foo”}
  • I decide to create a base class for all my controllers – UserAwareController. This was needed because every controller needs to access the current user, so I put all this logic in the UserAwareController base class.
  • Since every View needs the User to render, I created a Model base class that contains the current user. I’m not sure if this is the best way to go here, but it worked (what are your recommended best practices to store the user data?)
  • I implemented OpenID login using DotNetOpenAuth, and it was quite a breeze. No need to store user credentials, just store his public open id and let other websites handle the authentication for you.

image

Day 5

After allowing users to login and post questions, the next thing I wanted to implement was voting. So far all the code was server-side, but voting requires javascript because when a user votes you don’t want to refresh the page, but rather just change the vote icon. So:

  • I learned about jquery basics and wrote events to handle clicking the voting buttons.
  • Sent the vote information to a dedicated controller using JSON. The way JSON requests translate to controller methods is really seamless.
  • Initially I had an ‘AddVote’ method, but quickly switched to ‘UpdateVote’, which makes more sense.
  • Some css tweaks to make the cursor change to a pointer while on the voting buttons

image

Day 6

  • I finally had to cache vote count on questions & answers. The total vote count / score of a question has to be indexed, because we’ll have pages that get the ‘hottest posts’, and so keeping the User-Question vote relation is not enough.
  • So far, all my entities were strictly mapped to database rows. Now, I had to create a new ‘rich entity’ that contained a post and the current users’ vote on this post.
  • Finding myself duplicating logic between questions & answers, I create a Post base class and factored the entities and repositories to work on abstract posts.

image

This is it for now. I hope I didn’t make too many glaring mistakes in the process. As I mentioned, the code is available at GitHub – if you’re interested in helping develop it or have any questions, please let me know.

There’s a (meta) community emerging

As you probably know, I joined the new wave and got my own Stack Exchange site. It’s really amazing how many different SE sites are emerging so rapidly. There’s a strong sense of (meta) community with most of the site owners heavily participating in discussions on meta stackexchange.

There is some overlap between different sites, but most sites I’ve seen are original and useful. The owner/s of one SE site have been nice enough to post links to other SE sites, including Draw3Cards, at the top of their homepage!

Today I convinced a friend at work to open his own website, and I think you should to – before all the good ones are taken. Come and join us, it’s really that easy.

Trying out the StackExchange platform

For the last couple of days I’ve been obsessing over a new toy. Jeol & Jeff, the powers behind StackOverflow, have launched a public beta of StackExchange – a platform for hosting Q&A sites. Browsing the list of stack exchange sites, I saw several more and less successful sites, including:

I decided to try and open my own site, about Magic: The Gathering. The setup itself was rather easy. I:

  • Bought a domain and set it up, with some help
  • Seeded the site with some questions and answers
  • Tweaked the color scheme, logo and favicon (thanks Eran)
  • Got some friends to help out (though so far they’ve only posted answers, not questions)
  • Setup Google Analytics, and noticed incoming search traffic only stayed for 42 seconds on the site
  • Opened a small AdWords campaign, paying 1.5$ a day to test the water.
  • Post the site on this blog, of course.

Now what remains to be seen is whether the site can accumulate the needed critical mass. For now, the site is in beta, meaning I don’t have to pay anything to keep it running (except time and effort). When the beta is over, the cost will be $129 a month, which is quite challenging to make using adsense.

Keep your fingers crossed for me :)

P.S.

Just got a $100 coupon for AdWords by visiting Google Webmasters Tools – how cool is that? In a second look, it appears the coupon was lying in my Webmasters Tools inbox for two months now, just waiting for the perfect opportunity.

I also forgot to mention opening a uservoice forum (+ adding the widget to my site).

So this is what an empty StackOverflow looks like

Well, I said this would happen.

StackOverflow’s flood gates are open, and there are many new sites based on the stackoverflow engine.

One of those that I think has great potential is Ask Science, though at the moment it’s rather desolate:
asksci

StackOverflow Overflow

StackOverflow is branching out heavily in the past months. First, SystemFault, a sysadmin-targeted site. Then, they purchased SuperUser.com (coming soon) – a site for home users (questions like ‘how to configure my router’).

And now, a meta discussion forum. It seems the process of cloning or ‘flavoring’ SO is now almost streamlined. I wonder how many stack overflows will we have a year from now. In two years? I think it’s rather unfair that only us programmers (and now sysadmins) have such a great resource. What do you think – will SO clone into Q&A forums for other non-computer topics (a suggested name for a law-based SO – ObjectionDenied :) )

P.S.

Apparently,  there’s already a  release date for the platform! Prepare for the onsalught of StackOverflows.

Never rely on tail recursion elimination

Corollary: Never write a recursively method with an unbounded stack depth.

I previously posted a recursive implementation of Factorial, and asked the readers “not to bother saying it’s inefficient because of recursion, since it will probably be optimized away anyway”. Not true.

I don’t really understand why (just posted a question to StackOverflow), but C# does not generally perform tail recursion elimination (at least as my measly test show).

The following method can be easily optimized, both to work faster, consume less space and not to cause a StackOverflowException … but it is not optimized by the compiler (verified by Reflector and the fact a StackOverflowException was indeed thrown).

private static void Foo(int i)
{
    if (i == 1000000)
        return;
 
    if (i % 100 == 0)
        Console.WriteLine(i);
 
    Foo(i+1);
}

So … if for some reason you prefer writing recursive methods over using loops, do it only where performance is not a consideration and you have a good understanding of the maximal stack depth.

A Good Question

C# programmers – you should read this question and its many answers, I’m sure you’ll learn a thing or two. For example, ThreadStaticAttribute – a clean, type-safe way to get thread local storage in C#.

Stackoverflow.com

A few days ago, stackoverflow.com launched in public beta. I’ve played with it a bit and I think it has the potential to become a great tool for programmers.

It’s basically a combination of blog/wiki/digg/forum. Seems like a most questions get answers (on various degrees of relevance) within minutes-hours. Here are some of the questions I’ve asked, in case you’re interested.

Here’s a review of the beta site (about a month old), but instead of reading it I encourage you to just try it next time you need a programming question answered and Google fails you (don’t forget to subscribe to your question’s RSS feed).