Oh my god. It's full of code!

Visual Force Blows

EDIT/FORWARD: I got called out in a posting at http://www.tehnrd.com/if-visualforce-sucks-this-blows/comment-page-1/#comment-353 that pretty much line for line tore my post apart. So it is with great humility that I retract a majority of my negative statements about the following languages. I still do not like them, however I will admit they are both valid and well constructed and any difficulties I have are due to my own failings as a programmer and human being. This post is kept here now for historical and reference purposes, the content has not been modified. Also, I really do not wish any physical to harm to any of the developers of Apex/Visual force, it was more a hyperbole/figure of speech.

Okay, so you have probably never heard of this, but I am having to work with a language called Visual Force, yet another proprietary more than likely soon to be extinct language introduced by the over engineering code monkeys at Salesforce.

It is a language that is supposed to allow you to create custom content for their CRM platform which allows you to do anything from create new custom field types, to build whole websites for your customers or whatever. Sounds great in theory, however in reality it makes you want to stab yourself. A lot.

Problem is, it uses what is called MVC (Model View Controller, or some shit like that) which divides your logic code, from your design code (oh my god, they invented the idea of functions that can be called, how fucking astounding). Seems like a solid plan, cause your design team and your programming team get to work separately and not step all over each others code. Problem is, I am the design team, and the coding team. That means I get to deal with the whole project from the ground up and deal with all the over engineered bs.

For example, I am trying to create a simple dynamic menu system. The objects for the menu are pulled from Salesforce (which is basically just a fancy database) and looped over to create the links. The objects are simple (objects are just Salesforce’s name for tables). Seems easy right? Just query for the objects, loop over them and create links out of the data on said objects. Hell in Cold Fusion I could do this in 6 lines (minus formatting)

<cfquery name=”getlinks” datasource=”whatever”>
Select location,name from links
</cfquery>

<cfloop query=”getlinks”>
<a href=”#getlinks.location#”>#getlinks.name#</a>
</cfloop>

Look at that. Short and sweet. Grab the data, loop over it, create links. Now lets look at the Apex/Visual Force approach (apex is what they call the underlying language that powers visual force, imagine Java and C# getting wasted and having a kid. Apex is the result of their drunken grope fest. Its ugly, and embarrassing and mentally retarded.).

Now to be fair, I don’t even have a working sample yet, even though I’ve been working on this system for 2 days now. The following is the implementation in apex, of the above idea of getting data and looping to build links.

public with sharing class WebsiteMenu
{

public class menuItem
{
public List<menuItem> subMenu { get; set; }
public String Label { get; set; }
public String Action { get; set; }

public menuItem(String newLabel, String newAction) {
subMenu = new List<menuItem>();
Label = newLabel;
Action = newAction;
}

}

public List<menuItem> getMenuItems() {
Map<Id,menuItem> itemMap = new Map<Id,menuItem>();

List<menuItem> mainMenu = new List<menuItem>();

// Query all menu items.
Map<id,WebSite_Data__c> menuMap = new Map<id,WebSite_Data__c>(
[select id,Sub_Menu__c,Page_Title__c,Action__c from WebSite_Data__c]);

// Build the menu; all top-level items first.
for(WebSite_Data__c item:menuMap.values()) {
if(item.Sub_Menu__c==null) {
itemMap.put(item.id,new menuItem(item.Page_Title__c,item.Action__c));
mainMenu.add(itemMap.get(item.id));
}
}

// Remove those items from future iterations.
menuMap.keySet().removeAll(itemMap.keyset());

// Recursively add items to the menu.
while(!menuMap.isEmpty()) {
for(WebSite_Data__c item:menuMap.values()) {
if(itemMap.containsKey(item.Sub_Menu__c)) {
itemMap.put(item.id,new menuItem(item.Page_Title__c,item.Action__c));
itemMap.get(item.Sub_Menu__c).subMenu.add(itemMap.get(item.id));
}
}
// And remove them from future iterations.
menuMap.keySet().removeAll(itemMap.keySet());
}

// Return the constructed menu.
return mainMenu;
}
}

——————————————————————————————————

<!-- custom component 'menuItem' -->
<apex:component selfClosing="true">
  <apex:parameter type="theController.menuItem" name="item" required="true" />
  <apex:outputLink value="{!item.Action}">{!item.Label}
  </apex:outputLink>
  <apex:repeat value="{!item.subMenu}" var="subItem">
    <c:menuItem item="{!subItem}" />
  </apex:repeat>
</apex:component>

------------------------------------------------------------------------------
<!-- the main page -->
<apex:page controller="theController">
  <apex:repeat value="{!menuItems}" var="menuItem">
    <c:menuItem item="{!menuItem}" />
  </apex:repeat>
</apex:page>

Look at all that shit. I have to write my initial template page, then right the back end apex code
to create the menu, the write a component that invokes that code, and then write a function that
invokes the component and put that back in the original page. How much fucking overkill bullshit is that.

And guess what, that above code won’t even display the menu anywhere. I still have to nest that bottom piece of
code in whatever template I want it to show up on. And even after all that, this thing still doesn’t even work.
Don’t even get me started on testing classes either for the above code. I’ll save that rant for another day.

If it was possible to hate a language to death, they Apex and Visualforce have died in a fire long ago. Oh yeah, and for those reading hopeful
to get some help with this stuff, I’ll post again when I get functional code.

11 Responses

  1. Heh, heh. Wow! So, “to be fair”, if I understand the CF code, that will produce a list of hyperlinks, right? And, if I can follow your “crappy” Apex/Visualforce you are defining a menu with sub-menu items, right? Ignoring the number of lines of code for a minute, I respectfully submit for your consideration the following bits of Apex/Visualforce:

    public class WebsiteMenu {
    public List menuItems {
    get {
    if (menuItems == null) {
    menuItems = [Select Id, Page_Title__c, Action__c (Select Id, Page_Title__c, Action__c From Sub_Menus) From WebSite_Data__c];
    }
    return menuItems;
    } set; }

    }

    {!items.Page_Title__c}

    {!subs.Page_Title__c}

    Making assumptions about how your data is stored that may not be correct, but this should illustrate an “other” way to do that code.

    Cheers!

    January 11, 2010 at 9:21 pm

  2. Well, rats, my html tags disappeared.

    January 11, 2010 at 9:22 pm

  3. I left the sample code over here: http://devangeljournal.blogspot.com/2010/01/simple-object-iteration-for-visualforce.html

    January 11, 2010 at 9:32 pm

  4. Hey thanks a bunch for the reply, I’ve give that a shot right away!

    January 11, 2010 at 9:32 pm

  5. Okay, so I got you’re code working, and that’s really cool. The only issue is that instead of one large list of all links, I need to only find menu items of a certain sub_menu type. Check out
    http://www.fpitesters.com/
    That menu on the top is what I am trying to recreate. I guess you could make that into a parent->child relationship, but all 4 menus are really their own separate categories. So I either need to make one call to the controller and get all the objects, then filter them down from there (based on the sub_menu field) or make 4 separate calls each of which fetches only the desired type of link.

    January 11, 2010 at 9:59 pm

  6. Ok, I actually wrote some code to test this out. Mine is a simple sample. But, check it out. It may be, or may not be, what you are looking to do.

    Cheers

    January 11, 2010 at 11:55 pm

  7. Man, I wish I could edit my comments. I forgot to mention that I updated my blog post that had the “pseudo code” on it.
    :-)

    January 11, 2010 at 11:56 pm

  8. Thanks for all the help man, soon I will have a working version of what I need.

    January 12, 2010 at 3:54 pm

  9. Apexophile

    Two words come to mind when reading this post: Ignorance and Irony. Not sure which applies here.

    January 26, 2010 at 7:26 am

  10. Wow way to back up your arguments there. I mean yeah you really showed me. Not like I haven’t apologized or prefaced the article or anything.

    Ignorance – sure, I guess I am ignorant of the language, and some of the mentality behind it. Doesn’t change the fact that to me, and many others it is extremely difficult, frustrating, and limiting.

    Irony – How so? Ironic that I hate it yet use it? That’s not ironic, that’s business.

    Next time you come to insult me, bring a real argument, or get the fuck off my blog.

    January 26, 2010 at 3:03 pm

  11. Andrew Chinn

    Hey, I understand your frustration. I have been using Visualforce and Apex in SF for about 6 months now. It is very frustrating as it has a big learning curve as the most efficient ways to do things. I say stick in there and hopefully it will work out better for you.

    Cheers!

    October 30, 2011 at 3:10 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 202 other followers