To Async or Not To Async… That is the ReWritten Cliche Quote.

One of the things I’ve been wondering as a of late, besides why Micheal Bay still is allowed to make movies… and no money is NOT and excuse, is whether it’s better to just load and entire page and hide/show stuff on some click OR only load what’s shown and have said click handle loading the stuff through Asynchronous Calls and Json.

I bet that made sense!

Here’s the situation, I have a blog thing I’m working on and it allows for multiple blogs per site. The “splash” page, if you will, looks like this:

BAT_BlogSplash1

And when you click “Newest” it looks like this:

BAT_BlogSplash2

As you can see, Newest brings up a pop out div thing that shows the latest post.

Side note, I was going to have it just show the first X characters of the post as a teaser, but turns out that if there is any broken HTML because of that, IT’S A HUGE PAIN TO FIX. Something like <div> this is something that <di… OH SNAP THE DIV IS BROKEN! ALERT! ALERT! So this was a “solution”.

Originally I had it loading the needed post in it’s respective pop out div and just hiding all of them. Then it would show the post when the Newest “button” was clicked. Then it dawned on me that this might be silly since only one is going to be open at a time. Why send the client a ton of unneeded html? So, I moved on to using jQuery/Json to grab the needed post data when the Newest “button” is clicked. Then I just populate the pop out div with the returned information as it opens… or whenever it gets back to the client. Can’t always assume it will be there immediately. Interweb can be slow at times.

Anyways, this brings up an issue: Am I better off with the first or second solution? Given that all things are equal, it seems like a better idea to have the jQuery solution due to the fact you aren’t sending useless html to the client. This will most definitely help with initial load time for the client. Only problem is, that jQuery method is run every time one of the Newest “buttons” is pressed meaning you could have an infinite amount of requests per person. I suppose this is a matter of gambling really.

Do I go with full html and because I assume the user will look at all the latest posts anyhow and therefore cut down the needed requests?

Do I go with the jQuery method because more often than not, users will not look at all the latest posts OR at least will take some time in between the Newest “button” pressing so that it’s ok there might be a ton of requests over time?

At this point I think the second is the way to go, but it’s most likely situational. Time will tell.

What I Hate About Programming

Lately I have been screwing around with creating a blog framework using MVC and jQuery, the former being more troublesome from a design stand point. For example, The use of controllers and actions can cause a great deal of second guessing. Do I put all the admin view attached actions, like one for viewing a list of posts to select and edit, on an Admin controller? Do I have a Post Admin Controller and a Comment Admin Controller? Should anything having to do with Posts be on one Post controller and just seperate the actions based on attributes? (As in MustBeAdminAttribute as opposed to MustBeLoggedIn) After all controllers are about grouping things, but how the h— should they be grouped? Of course, that’s a sort of gray area an subject to debate. The problem is I can’t help but think not matter what, I’m making the wrong choice. Why? Mostly because I know I’m not a super programmer.

Now this isn’t going to be an Atwood, back patting, I’m so good I know I’m bad kind of post but something has really occurred to me more and more lately. As I’ve gone deeper and deeper into things like Entity Framework, MVC, and jQuery I’ve only been left with the feeling that someone else, somewhere is doing this a lot better. Maybe it’s because my lack of experience with them, maybe it’s just a self depreciating nature thing, but it’s also having access to too many good programmers due to the plethora of them on the internets. Yeah right, that’s a bad thing? Well it kind of is. It’s tough doing anything anymore when you know there are people far better than you doing a far better job. And with the advancement of information sharing, you can’t help but find proof of that. With that comes a certain guilt that you aren’t the top of the food chain and in fact you just might be a programmer the other better people complain about. That’s right, you. Someone at this very moment is straight up banging his/her head against a desk because of something you wrote.

Some would suggest this is the sign of a good programmer. Someone who is never completely happy with his/her performance. F— that, it just plain sucks and makes what can be enjoyable an epic pain in the a–. No one wants to be that guy, you know the one that names every variable with numbers (v1 + v2 = v3) or has one gigantic method that does everything. Problem is, this is all relative. One person’s perfection is another guy’s multi purpose stored procedure that runs 10 different query using a switch and a passed in string. Code perfection is in the eye of the beholder. And by now we all know there are plenty of high end beholders.

What does that mean? You can never win at the game of programming. Read it again. You can never win at the game of programming. That is unless you are beyond a doubt the sharpest bulb in the bunch. For the rest of us chumps, it’s grin and bear it time.

That’s it. That’s the answer. Suck it up. Don’t like that answer? Refer back to the answer. Fact is, you’ll never be perfect and if you were, chances are you wouldn’t be reading this nonsense. You’d be out in the middle of the Gulf of Mexico kicking it back on some island you own. For the rest of us mortals (Or dumb immortals), it’s time to join the Tool Fiesta. Start dancing, drinking, and accepting. I’m not saying that you can’t better yourself. Hell maybe you’ll be one of the top people out there… and optimistically thinking, considering this site, maybe you are (What the hell are you doing here though?). But you aren’t going to be perfect and neither is your code. However, sitting around and worrying about producing perfect code is just sitting around. Have to figure some code is better than no code at all right? Just lie to me.

Javascript: Scope and Learning Something New Everyday.

Course for me it doesn’t take much to learn something new, but let’s not get into that.

So pop quiz, and one that doesn’t involve a hostage and shooting.

jQuery(document).ready
(
  function()
  {
    for(var loopCounter = 0; loopCounter < 3; loopCounter++)
    {
      var tempThing = loopCounter;
      alert('in' + tempThing); 

      jQuery(".someButton").click
      (
        function()
        {
          alert('out' + tempThing);
        }
      );
    }
  }
);
....
  <button class="someButton">click!</button>

What happens when you click that button? Well for one it will alert three times but what will it print out?

The first alert (in the loop) does what you would expect. It gives 0, 1, 2. But what about the button click? Well that’s a little different. It gives 2, 2, 2… Wait what? The f— is that? That makes no real sense.

Given that this was C#, it would output 0, 1, 2 because we would know that every loop the tempThing field is a new field. Sure it has the same name in code, but every iteration would create a new space in memory for a new tempThing. And then with closures, a reference would be made to that tempThing so that when the method actually runs, the values are what they should be. Javascript does something a little different. Go figure, right? Can’t do anything like anyone else… anyone else being C#.

Now what does happen is it does give you the correct value of tempThing. What you didn’t know (Assuming you didn’t know because if you are reading this you either are really bored or don’t know… or both) is that tempThing is global to that method. Turns out variables are global to the current scope no matter if in a loop or not. tempThing, no matter where in that function is created/instantiated/ect is the same tempThing through out the method. So what’s happening is that the first time in the loop it’s being created and every iteration after it’s just being updated with the current loopCounter value.

How do you solve this? Well you have to make a method to create a method to set the value. AWESOME!!111

jQuery(document).ready
(
  function()
  {
    for(var loopCounter = 0; loopCounter < 3; loopCounter++)
    {
      alert('in' + tempThing); 

      jQuery(".someButton").click
      (
        createReturnMethod(loopCounter)
      );
    }
  }

);

//This is used to create a new method scope so that
//tempThing is unique to each call.
function createReturnMethod(loopCounter)
{
  var tempThing = loopCounter;

  return function(event)
  {
    alert('out' + tempThing);
  };
}

As you can see, I created a method to create a method to be run on click. Because tempThing is now in the second method’s scope, it will be different for every time this method is called. Magic!

Here’s a thought, if only you can prevent forest fires then you really should get on that. People have lost a lot of money because of your slacking.

jQuery Whack-A-Mole… Timer, Hide, Slide, and Fun

So I’ll just straight up file this under “I really shouldn’t have” and not bother asking for forgiveness. For some reason I had it in my mind I wanted to see if I could take the stuff I learned from this guy and this guy and see if I could make a simple Whack A Mole game with just jquery and the three include files. (jquery, jquery UI, and jquery Timer) Not thinking of the massive consequences such a thing might bring, I pushed on in the name of science.

So with no further build up or stupidity (I used that all up in building this game) I give you jQuery WHACK A MOLE which oddly enough has somehow clawed it’s way into my hosting here. So if you’re foolish enough to actually want to know how it works, there are plenty of comments in the script files. Just don’t blame me for any loss of intelligence.

I might actually take this to the next horrifying level and make it an mvc application. Not sure I… no… THE WORLD can take it.

jQuery: Slide Menu With Pop Up Divs. Yay!

So you may have seen this post about how I came to create such a wonderful menu with no real purpose as of yet. Well that menu has been improved and can be downloaded here.

Remember that question you never asked? Well once again I’ve answered it.

To take the normal sliding menu farther I thought I should have something happen when hovering over the menu items. Novel! Well as you can see if you clicked on the link above (Sorry, can’t repost the link. Links are expensive.) the menu items have large counter parts that show up when hovered over and don’t go away until the counter part or the main item is left. What does this all mean? I DON’T KNOW but I’ll find out eventually. It’s in the cards man, it’s in the cards…

Ok so additions… For the style sheet, I added a class:

.menuItemBig
{
  height:100px;
  margin-right:5px;
  position:absolute;
  width:100px;
  z-index:100;
}

And have included a child div to the menu item:

Before

  <div class="menuItem floatLeft blue">1</div>

Now

  <div class="menuItem floatLeft blue">
    1
    <div class="menuItemBig red">THIS IS THE BIG ITEM 1</div>
  </div>

I added two methods:

//
// mouseenter: Find any menuItemBig within the given element and show
//             Reset the position of the menuItemBig element to appear
//             to be in the middle of the parent element
// mouseleave: Hide the child menuItemBig
//
function setOnHoverForMenuItems(items)
{
  jQuery(items)
  .each
  (
    function ()
    {
      jQuery(this).mouseenter
      (
        function()
        {
          //Why the positioning?  I wanted the child div to show up in the
          // middle of the parent div which is done by putting the child's left side
          // to half of the width of the parent over from the parent's left side
          var parentPosition = jQuery(this).position();
          var bigItems = jQuery(this).children(".menuItemBig");
          bigItems.css({ left: parentPosition.left - (jQuery(this).width() /2), top: parentPosition.top + 10  });
          bigItems.show();
        }
      );

      jQuery(this).mouseleave
      (
        function()
        {
          jQuery(this).children(".menuItemBig").hide();
        }
      );
    }
  );
}

Why not hover? *EDIT* It should be hover. Turns out an issue I was having with hover was not actually an issue with it… *END EDIT*

The other method I added was a simple one:

//
// Used to find any element of menuItemBig
//
function hideAllBigItems(bigItemParentItems)
{
  jQuery(bigItemParentItems).children(".menuItemBig").hide();
}

And where did I use these? I appended them to the setChildrenDivs method:

function setChildrenDivs(mainHolder)
{
  var menuItems = getMenuItems(mainHolder);

  for (var loopCounter = 0; loopCounter < menuItems.length; loopCounter++)
  {
    if(loopCounter > maximumToShow - 1)
    {
      jQuery(menuItems[loopCounter]).hide();
    }
  }

  setPager(jQuery(mainHolder).children(".leftPager"), getLastVisible, getNextInLineBack);
  setPager(jQuery(mainHolder).children(".rightPager"),getFirstVisible, getNextInLineFront);
  setOnHoverForMenuItems(menuItems);
  hideAllBigItems(menuItems);
}

So in all, this didn’t take much at all, thanks to mouseleave. Next up… not sure. No idea if I will take this any further.

ASP.Net MVC HtmlHelper Method for Creating Buttons With Forms and Optional Images: Part 1

Straight from the “I made this and maybe you can find a use for it” bin, here is one of two ways I create image buttons. Why would you care? I have no idea, but two of the catches that comes with buttons is they have to be wrapped in a form and the fact you have to, far as I know, add hidden value inputs to the form to carry over the values from a url (Action of the form). This take care of both, can ya’ belee it?

This guy takes in a url (ie you already know what it should be, say from pre determined redirect value), breaks down the url to get the request parameters to make hidden inputs, and adds the button. Now I went with the image being a style for the button instead of passing in a location for the image itself, mostly because I find that to be easier to handle from a design perspective since it’s possible that particular image might be used a lot on a site. Seems easier, if you need to change the image, to change one css class then to have to search and replace a million image urls. But what do I know? No seriously, what do I know?

public static String OptionalImageButton(this HtmlHelper helper, String returnUrl, Boolean viewAsImage, FormMethod formMethod, String buttonText, String formClass, String imageClass, String buttonClass)
{
  StringBuilder html = new StringBuilder();
  //Create the form along with the formMethod passed in
  html.Append("<form action=\"" + returnUrl + "\" class=\"" + formClass + "\" method=\"" + formMethod + "\"> ");

  //This is the method from this post, it just give me all the hidden inputs from a url
  html.Append(MvcMethods.CreateHiddenValuesFromUrl(returnUrl));
  //This is where you might hate me.  Instead of having an image for the button
  //I am using a css class to hold the image.  Just a choice, makes it easier
  //in my opinion
  if (viewAsImage)
  {
    html.Append("<input type=\"submit\" value=\"\" class=\"" + imageClass + "\" />");
  }
  else
  {
    //If no image, then just put a text button.
    html.Append("<input type=\"submit\" value=\"" + buttonText + "\" class=\"" + buttonClass + " \" ></input>");
  }

  //End the form
  html.Append("</form>");

  return html.ToString();
}

You might notice the FormClass parameter, or maybe you didn’t because you can’t read. If that’s the case, then we’re both wasting time. However, if you did notice then you might find it odd. As it is, a form has to be inline in order to show buttons next to each other. After all, by default a form is a block element like a div. Therefore, the form itself will need a class or the style set. Now I could default this to inline, but for the sake of giving power to the programmer, it’s left as a parameter. After all, maybe not all buttons should be inline.

And usage:

  Html.OptionalImageButton(Model.ReturnUrl, Model.UserShowImages, "Return", "inline", "returnButton", "button080SmallTextAction")

Pretty easy to use and clean… well clean for MVC at least. Next MVC post I’ll show the route driven version of this “control”.

ASP.Net MVC: Button Post Is Losing QueryString Values And Getting Paramters From A URL

I was going to start this post with a rousing ARE YOU READY FOR SOME PROGRAMMING?, but my lawyer suggested it might cause the NFL to take action against me. He suggested something more simple like PROGRAMMING HAPPENS HERE! since he had high doubts the NBA will sue me. After all they’d have to admit they actually came up with that f-ing slogan in the first place. It’s a win/win situation for me.

So one of the things I’ve run into with MVC is this situation… Say I have a form and a button, and the form has an action that looks like this:

  <form action="/Tools/AddTool/1?redirect=/Tools/ViewTool/1" method="post">
    < button type="submit">GO!</button>
  </form>

Really simple. The idea is that I want to post to the AddTool action, do some stuff, then have the action redirect back to the ViewTool action. Seems like it should be easy right? Well not so much. Because when you view the ActionParameters (Like in this example) for the “redirect” key, it finds the key but loses the value. This may result in a failure if have an attribute making sure it exists or possiblly you just have it set to a default that’s useless like a fourth foot. Either way, not good.

Now I haven’t figured out the reason for this (Don’t act so surprised, you aren’t a very good actor), but I have figured out a way around it. Basically for the form to keep the values, you need to add hidden values. I know, it’s a pain. Good thing I have a method that makes it easier. Also, this post is a prerequisite for a method I have for creating buttons… but that in the future. ITS A TEASER! THIS POST IS A TEASER! THERE I SAID IT! CAN I GO NOW?

public static String CreateHiddenValuesFromUrl(String baseUrl)
{
  StringBuilder html = new StringBuilder();

  if(!String.IsNullOrEmpty(baseUrl))
  {
    //Have to find the ? to know where to begin
    Int32 indexOfQuestionMark = baseUrl.IndexOf('?');

    //If the question mark exists and there is something after it, keep going
    if (indexOfQuestionMark > -1 && baseUrl.Length >= indexOfQuestionMark + 1)
    {
      //Get everything AFTER the ?, something I didn't think of first time around
      //Caused many test failures and much shame to my family.
      String request = baseUrl.Substring(indexOfQuestionMark + 1);

      //Cut up the string by the & since every parameter after the first
      //is divided by &.. I know, Duh.
      String[] splitList = request.Split('&');

      //Go through the list of possible request items
      foreach (var requestItem in splitList)
      {
        String[] splitItem = requestItem.Split('=');
        //This is to make sure the parameter actually has a value to send in
        //the hidden values.  I supposed you could create an empty hidden
        //value to preserve the parameter, but remember the parameter
        //isn't being lost from the form action, just the value
        if (splitItem.Length == 2 && !String.IsNullOrEmpty(splitItem[1]))
        {
          //Create a hidden input with the key name and the value
          html.Append("<input type=\"hidden\" name=\"" + splitItem[0] + "\" value=\"" + splitItem[1] + "\" >");
        }
      }
    }
  }

  return html.ToString();
}

And it’s just that simple. Mind you I could probably make this an extension method for the HtmlHelper, but I don’t really use it for that.

Next up will be more razzle and dazzle using this to make an optional image button. I bet you can’t wait. I know I can.

jQuery Slide Menu… Another Cause I Can Experiment

So for no real reason at all I had it in my mind that I wanted to make a horizontal menu with jQuery that would work like that weird scrolling menu thing that Macs have. No idea what it’s called. So basically I don’t need it, have no reason for it, but damnit I’m going to make it happen and I did it with only 3 things from jquery.com; 1.3.2, ui 1.7.2, and jquery.timer . Now this is still rough in the sense it has no real styling but it works tried and true functionality wise.

The main idea is that there are two scroll arrows, one on each side, and X amount of divs. Now at the start, a certain amount of divs are hidden (global variable). When hovering over the left pager, for example, it causes one on the right to hide while one on the left appears giving the feeling of the items sliding.

EXAMPLE HERE!!11

The markup is simple, a holder with x number of elements that are “menuitems” and two pager divs.

<div class="mainHolder">
  <div class="leftPager green floatLeft"><</div>
  <div class="menuItem floatLeft blue">1</div>
  <div class="menuItem floatLeft red">2</div>
  <div class="menuItem floatLeft yellow">3</div>
  <div class="menuItem floatLeft blue">4</div>
  <div class="menuItem floatLeft red">5</div>
  <div class="menuItem floatLeft yellow">6</div>
  <div class="menuItem floatLeft blue">7</div>
  <div class="rightPager green floatLeft">></div>
</div>

I think from the classes you can tell what you need to know about them.

First thing we need from jQuery is methods to find various elements in the container when paging.

//When using the left pager, it's important to find the first visible element
//then find the item before it so that it can be shown.
function getNextInLineBack(menuItems)
{
  var oneBefore = null;

  for (var loopCounter = 0; loopCounter < menuItems.length; loopCounter++)
  {
    if(jQuery(menuItems[loopCounter]).is(":visible") && loopCounter > 0)
    {
      oneBefore = jQuery(menuItems[loopCounter - 1 ]);
      break;
    }
  }

  return oneBefore;
}

//Find the first visible element from the beginning.
//This will be needed when paging right since it will have to be hidden
function getFirstVisible(menuItems)
{
  var firstVisible = null;

  for (var loopCounter = 0; loopCounter < menuItems.length; loopCounter++)
  {
    if(jQuery(menuItems[loopCounter]).is(":visible") && loopCounter < menuItems.length - maximumToShow)
    {
        firstVisible = menuItems[loopCounter];
        break;
    }
  }
  return firstVisible;
} 

//Get the last possible visible item
//If the item is in an index less than the maximum number to show, then null is returned since there has to be no more or less than the maximumToShow.
function getLastVisible(menuItems)
{
  var lastVisible = null;
  for (var loopCounter = menuItems.length - 1; loopCounter > maximumToShow - 1; loopCounter--)
  {
    if(jQuery(menuItems[loopCounter]).is(":visible"))
    {
      lastVisible = menuItems[loopCounter];
      break;
    }
  }

  return lastVisible;
}

//Find the first visible from the end
//Pretty simple, this will be important when paging left since this
//will be the next item to be hidden
function getNextInLineFront(menuItems)
{
  var lastOne = null;

  for (var loopCounter = menuItems.length-1; loopCounter > -1; loopCounter--)
  {
    if(jQuery(menuItems[loopCounter]).is(":visible"))
    {
      lastOne = menuItems[loopCounter + 1];
      break;
    }
  }

  return lastOne;
}

Next is a method that is just used to stop having to repeat the same thing over and over when needing a list of all the menu items.

function getMenuItems(mainHolder)
{
  return jQuery(mainHolder).children(".menuItem");
}

Next is the method to handle what item to show and what item to hide when the pager has the mouse over it. Instead of having methods for the right and left pager, I just ended up having the methods for finding the item to hide and show sent through as parameters.

function showHideOnHover(pager, timer, getHideMethod, getShowMethod)
{
  //This is just candy for changing the color of the pager when the mouse
  //is over it
  jQuery(pager).removeClass("green");
  jQuery(pager).addClass("orange");

  //Remember those methods I passed through, well here they
  //are in use.  I'm using them to get the item to hide and the item
  //to show along with the list of items.
  var menuItems = getMenuItems(jQuery(pager).parent());
  var hide = getHideMethod(menuItems);
  var show = getShowMethod(menuItems);

  //If neither is null, then go ahead and show/hide
  //If either one is null, something isn't right and the timer
  //needs to be stopped.... timer??  Well I'll get to that
  //next.
  if(show != null && hide != null)
  {
    jQuery(hide).hide( "slide", { direction: "right" } , 0);
    jQuery(show).show( "slide", { direction: "left" } , 100);
  }
  else
  {
    timer.stop();
  }
}

Now for the method above the last one, this one involves the timer passed in the last method. This method actually sets the mouseover/mouseout events (aka hover). When mouseover, the timer is created and the showHideOnHover method is called every 500 units, that’s we’ll call tools, (Not sure how much that is, seems like a half second) after the first time it’s called. On mouseout, the timer is stopped, nulled out, and the pager changes it’s color.

function setPager(pager, hideMethod, showMethod)
{
  //Making the timer variable "global" to the events so that I know
  //I have the same timer for both mouseover and mouseout.
  var newTimer;
  pager.hover
  (
    //Mouseover method
    function()
    {
      var first = true;
      //This sets the timer, consequently starting the method for the first time.
      //Why timer doesn't have a start method I don't know.  Ask jquery.com.
      //The first thing is just so that the first time around it runs right away,
      //then each call afterwards comes every 500 tools.
      newTimer = jQuery.timer
                      (
                         0, //First time through, runs after 0 tools.
                         function(timer)
                         {
                           showHideOnHover(pager, timer, hideMethod, showMethod);
                           //If this is the first time through, reset
                           //timer to run every 500 tools.
                           if(first)
                           {
                              timer.reset(500);
                              first = false;
                           }
                         }
                       );
    },

    //Mouseout method
    function()
    {
      //mouse is done, stop the timer
      newTimer.stop();
      newTimer = null;
      jQuery(pager).addClass("green");
      jQuery(pager).removeClass("orange")
    }
  );
}

Now for the method above the one… above. This is used to set the children of the passed in holder.

function setChildrenDivs(mainHolder)
{
  //Get the items for the holder
  var menuItems = getMenuItems(mainHolder);

  //Hide all the items after the first X items (maximumToShow)
  for (var loopCounter = 0; loopCounter < menuItems.length; loopCounter++)
  {
    if(loopCounter > maximumToShow - 1)
    {
      jQuery(menuItems[loopCounter]).hide();
    }
  }

  //set the pagers.
  setPager(jQuery(mainHolder).children(".leftPager"), getLastVisible, getNextInLineBack);
  setPager(jQuery(mainHolder).children(".rightPager"),getFirstVisible, getNextInLineFront);
}

FINALLY THE END! This is the document.ready method used to set this whole joke in motion. maximumToShow is just how many items to show at a time and is global.

var maximumToShow = 5;

jQuery(document).ready
(
  function()
  {
    //Find every holder on the page and set everything in motion.
    var mainHolders = jQuery(".mainHolder");
    for (var loopCounter = 0; loopCounter < mainHolders.length; loopCounter++)
    {
      setChildrenDivs(mainHolders[loopCounter]);
    }
  }
);

Why the timer? If you haven’t figured that out yet, well it’s because I had issues with how to get the menu to keep doing it’s thing as long as the user had his/her mouse over a pager. I didn’t want this to be a click menu because, let’s be honest, that would be much easier. So as is, the timer is started the moment the mouse is over a pager and hides/shows an item. Then every 500 tools the mouse is over the pager, it continues the hide/show until it runs out of items to show/hide. (End of the list)

Uhg that’s annoying to type out even with cut and paste so I will host it here.

I suppose the next part of this would have the items blow up or something when hovering over them but that should be much easier than this was.

Did Postback ruin the “web”?

If you’ve been reading my drivel as of late, you’ll know that I have moved full steam ahead with MVC, for better or for worse, ’till death do us part… or until MVC catches me shacking up with a much younger framework and sues me for everything I own. Either outcome, I going with it for a while. Why? Well I could name a couple reasons but the main thing I just like how easy it is to use jQuery with it do to how “RESTful” is it. Silly, I know.

Well I have put out some things about certain catches with MVC and it started to make me wonder about why I’m having so much trouble with certain aspects of the framework. It isn’t the modularity. I really like the controller concept. It isn’t the attributes instead of base pages to take on certain repeated checks (Say if the user is logged in), it fact I’ve embraced them. No the problem lies in the last few years of my short but uneventful programming career.

In the beginning there was ASP

I’ve admitted before, or at least I think I have, that I’ve only been doing this for about 7 years. I wasn’t the kid that started programming on an Apple II (E, C, GS, you can take your pick. Had them all growing up), I didn’t find programming those mini computers (You know the ones that basically had a strip of plastic that served as a monitor) all that much fun, and I surely didn’t sit around in my youth trying to hack into anything put in front of me. I just wasn’t that guy. In fact I didn’t start programming until around 2000 or so, the hey day of ASP. Now at that point in time, what a page could do was pretty limited. You could show tables, you could click links, you could post data to save. It wasn’t pretty but it got the job done. For the most part, web programming was just a step up from kiddie scripting and a small step down from VB Weenies. Fact was, it wasn’t very complex and site design reflected this. Sure there were a few that could push the limits, but the limits were pretty low.

You can cut a tin can with it.

ASP.Net… or at the time “.Net”. .Net was this almost mythical name that seemed to be used for everything from solving the complex page dilemma to removing that nasty “cranberry” stain from your expensive sheets that you can only buy in Sante Fe. At first people were sceptical, and why not be? Microsoft was pushing it as the universal key and at the time that seemed pretty impossible. Then people started using it. Sure it was messy at first. A lot of the people from the ASP walk of life either had forgotten how to program in forms or just never really learned. (Yours truly) Now all of a sudden there was a separation of mark up and code, and some crazy thing called post back, and wow what the hell are events? Classes? Double You Tee Eff? It was like this wave of excitement that web programming had grown up and it was the real deal. And it didn’t stop there.

Give a man an inch…

Well it didn’t take long for the over ambitious (IE Sales people) to start pushing the envelope of what could be done. Every time we reached what we thought to be a summit, someone would see if we could climb the next mountain… that happened to be twice as tall. After all it should be safe to assume that if you can make it up 10k feet, then 20k shouldn’t be that much harder right? All of a sudden “Can you do this?” had turned into “What I want is this” and for the most part it was possible. (Though ugly at first)

I want a page with data that I can edit.
-We can do that.

But I don’t want to be taken to another page
-We con do that

And I want it update when I save something without leaving the page
-We can do that

And I want it to seamlessly incorporate a bunch of different information from a million places and have it handle each part of the page differently but also have it handle all of this without leaving the page… and I want it done in two weeks.
-We can do that.

Of course this incredible feeling didn’t really allow us to think about the cost of all of this. Things like Viewstate, Session, gigantic pages with a bountiful of html, inability for smart phones to browse or at least load quickly, ect ect ect. It didn’t matter, because we were able to give the customer what the customer wanted: A Winforms program without the installation process. And over time, this has allowed pages to be so complex in structure that it you really couldn’t tell it was a web site if it weren’t for the browser.

Just because we can do a thing doesn’t mean we must… do that thing.

WebForms, as the cool kids cal them now, opened up a whole can of worms. They made it easy to create incredible complex pages with relative ease. And to do so they bent the “rules” of the web to allow for greater ease of development, at least that’s what you hear from MVC proponents (And REST people). MVC brings us back to the time when programming didn’t pervert the system, and at it’s core it isn’t pretty. Taken just as is, with the tools that the MVC framework has, the complex Web Forms we grew accustomed to are just not possible without a ridiculous amount of work. Heck a simple button press becomes much more complex in order to mimic a Web Forms post back. And that’s the problem… I’m trying to emulate something that MVC wasn’t built for and in a way could be argued that the web wasn’t built for: The post back. The Post Back is one of the key parts of Web Forms that allowed for so much and it really isn’t in MVC. I mean it could be. You could load up all the page/view functionality onto one gigantic controller but that kind of breaks one of the strengths of MVC: Modularity. Fact is, that little guy helped shape the way we think of site design and programming. (Not to mention viewstate but whatever) It allowed for so much to be done with so little effort. It gave us a bridge to winforms. For right or wrong, it gave us a solution and it also reprogrammed our future ideas.

Who has time for principal?

When conversing with other people about MVC, I had stated the whole “It’s the way the web was intended” line (Which personally I really don’t subscribe to because in a way I don’t care) and someone rebuked, “If it gets people what they want, then who’s to say what’s right for the web?” And I have to agree with him on that. I mean, it’s not like there’s life on the line here. If I move to MVC purity there will still be hunger, still be war, still be movies made by those idiots that brought you films like “Meet the Spartans” and “Disaster Movie”. (Although if Microsoft could solve that problem with MVC, I don’t think we’d have Web Forms anymore. Get on it Microsoft…) In the end, isn’t that what matters? Getting things done?

The beat marches on

So where do I go from here? If anything, I think the 800 gorilla is that I have to start designing web sites in a new way. Have to unlearn all of the things I got from WebForms and take myself back to the day when things were more simple. Even with advances with Asynchronous Calls, I still have to develop a completely different state of mind. Is this a bad thing? Time will tell. It’s possible that this is a fool’s errand but it’s also possible that use of MVC will bring forth sites with better structure and more reasonable functionality. After all it’s possible that post back derailed good site design.

Let it be noted

Before the comments about [Insert Javascript Library] start to pile in… HAHA comments on this site… Ok let’s pretend I get comments and the would be comments about [Insert Javascript Library]/Asynchronous calls(Let’s just call it JLA for short) calls, right or wrong I see JLA as a work around. It’s a way to allow for the complexity of said Web Forms without changing the MVC framework or relying postbacks. But even with those you still have a lot of unfun code that barely makes up for the loss.

Modern Programming: Anti-Evolution?

For a moment let’s just not bother arguing the merits of evolution versus whatever, and look at one of the basic ideas behind it.

Every one’s heard the cliche phrase of “the strong will survive”. Most likely it’s been in some movie you watched, some coach spouted it out, maybe you hear it from some business “guru”. Doesn’t matter. (Although there is actually a lot more since it’s not the just being strong but the best suited for the current environment ect, but let’s just play Sesame Street evolution.) Let’s go by the overall idea that those that have traits that are against survival will be removed… ie the weak.

The best code I’ve ever written was someone else’s.


Whenever you do something new, you run into problems mostly due to lack of knowledge. In the programming world, this is an everyday occurence provided you’re actually trying to learn. Can’t figure out how to make something work? Well just go search for it. No matter how new something is, most likely there are already people answering questions about it on a forum, posting it on a blog, or probably some site that is dedicated to it (Say like MSDN) that will give you an idea of what to do. That’s the beauty of programming today. But what about 20-30 years ago? What happened when you just plain didn’t have instant answers or dare I say it? No Stackoverflow. Well some people would be just fine. Those people would just plow through and figure it all out. They would go through whatever written specs there are on… paper… and get it done. They are the people who already have posts about it in their blogs now days before you even asked about it. These are the real programmers. Problem is, for every one of those there are probably 10 people who take what they have and run with it. Then for everyone of those there are 10 who barely understand it, just copy/paste, and post it somewhere. And for every one of those, there are ten that take that code and use it. And guess what? Most likely all those people still have a job. All of them.

The herd isn’t thinning


The question now is: How bad is this? Basically with the wealth of information out there, even the worst of programmers can keep a job that no way in hell they would have held 20-30 years ago. (How can I guess that? Because even 7 years ago when I first started really programming it was difficult to find things and that was with search engines.) With the demand for programmers being so high in so many places, businesses are almost forced to take on people who have just barely (Due to said search engines/copy and paste) made it because hey at least the person has prior work, so he/she must be good right? So the cycle continues, but in the long run most likely it continues in a direction contrary to evolution. Because remember, by my completely accurate and undebatable numbers, there are 1000 bad to every excellent. By that idea, and since the bad aren’t being removed, the amount of bad code out there is growing at an incredible rate and it’s being learned by new people. People who may not be bad but will be eventually due to the amount of horrible stuff out there. The bad are breeding bad, and it isn’t being stopped. At some slippery sloped point you have to wonder if there will only be mostly bad to learn from.

Those that are most capable of surviving in the present conditions will be chosen


So the whole “only the strong will survive” isn’t exactly the whole story. It’s not just strong but whoever is best suited for the environment. When we think of strong, we think most rugged, toughest, most able to surive encounters. However, in the evolutionary sense this isn’t true. If it were, all dinosaurs would have been like this almost mythical bad boy. Problem is things like food availabilty, ability to avoid predators, hell even resistance to sun exposure can all factor into what is really “strong”. The point is, those who are chosen are best for the environment they live in. Now lets take the 1000 bad coders to every 1 excellent one. With that in mind, most environments entered into are going to be suitable to which group? The bad ones. A lot of us have seen this. Started work at a company that the code was just dreadful but the 3 guys that have been there for years and the one dude that was hired in a week before you are all happy as a pig in… well anything. We try to change things. We try to move things forward, but in the end we get burned out and leave. Guess who replaces you? The guy who works well with the other four. In fact, this is another part of evolution is that if two groups of the same animal are split by something, say a new river, the two might in fact eventually become incompatible. Think about it, a company that has strong programmers and weak ones that becomes taken over by the weak will eventually spawn a whole new type of programmer and programming shop that it completely alien to a decent programmer and would allow itself to continue unstopped. At some point, slipery slope full on, there may end up being way more bad companies then good and the good programmers may get weeded out.

How do I know if I’m the strong?


Fact is, you probably can’t. Some people know it because they work for high end companies like Google and Microsoft. The rest of us mortals don’t. I think that is why this career has frustrated me at times actually. I can’t really be proud of what I’ve done because I’ve never been fired for bad code. I’ve never been released for writing bad programs. Hell I’ve never even been pulled into an office. You would think that means I’m doing something right except I’ve never known anyone who has. And I’ve had about 8 jobs over 7 years(All due to contract work or companies going under… Let me tell you, I’ve was on a streak of joining failing companies for a while), so I have my feel for bad code and bad coders. None of them were fired or let go because of bad code. And companies keep hiring in bad people so I’d have to believe that even if someone was let go, that person would find another job eventually. Hell, go to somewhere like Chicago, and you practically have to shoot someone to get fired because companies need people so badly.

Better than being dead.


So maybe its a good thing. Since after all I still have a job, I still get paid well, I still have some security. But in the long run, is this a good thing? Can programming as a whole grow as this situation continues? Everyday it gets easier and easier to be a programmer. I can’t imagine that’s going to change. With tools like jQuery, StackOverflow, Google, and any other way to help people with kid’s gloves, it’s hard to think that things are going in the right direction.

Last note

While writing this I couldn’t help but realize that this my be riddled with hypocrisy. After all, how do I know if I’m not one of the bad people? Sad thing is I don’t.

What about you?