Rather Complicated Map Usage



  • When I finally figured out what a co-worker's code was supposed to do (by adding a bunch of my own comments describing lines and blocks), it was a forehead-slapping moment. Now although we write Java code, we're constrained to a proprietary framework and libraries. The code doesn't directly speak SQL -- we are forced to use a proprietary library which does that on our behalf. (Trust me, I'm doing you a favor by not explaining the proprietary stuff further.) Here's (essentially) what is code did:

    MapList brands = ProprietaryClass.findObjects( {pseudocode: select id,name from brands} ); // MapList == java.util.List of Map objects

    MapList brandsConnected = ProprietaryClass.findObjects(
    {pseudocode: select id,name from brand_mappings, brands where brand_mappings.brand_id=brands.id AND brand_mappings.object_id=1234}
    );
    Map brandsConnectedMap = new HashMap();
    for(int i=0; i < brandsConnected .size(); i++) {
    Map m = (Map) brandsConnected .get(i);
    brandsConnectedMap .put( m.get("name"), m.get("id") ); // mapping name -> id
    }

    /* If you're following now, brandsConnected is most definitely a subset of the elements in brands. */

    // -snipped code-
    // much later...

    for(int i=0;i<brands.size();++i){
    Map brand=(Map)brands.get(i);
    String brandName=(String)brand.get("name");
    String brandId=(String)brand.get("id");
    if(brandId.equals(brandsConnectedMap .get(brandName))) {
    %><%=brand.get("name")%> <%
    }
    }

    I think the way I've abbreviated it has even made it more clear for you, dear reader. Just imagine how many data structures, flow-control and looping constructs could be avoided if this were done just a bit simpler.



  • @Kozz said:

    When I finally figured out what a co-worker's code was supposed to do (by adding a bunch of my own comments describing lines and blocks), it was a forehead-slapping moment. Now although we write Java code, we're constrained to a proprietary framework and libraries. The code doesn't directly speak SQL -- we are forced to use a proprietary library which does that on our behalf. (Trust me, I'm doing you a favor by not explaining the proprietary stuff further.) Here's (essentially) what is code did:

    MapList brands = ProprietaryClass.findObjects( {pseudocode: select id,name from brands} ); // MapList == java.util.List of Map objects
    
    MapList brandsConnected = ProprietaryClass.findObjects( 
      {pseudocode: select id,name from brand_mappings, brands where brand_mappings.brand_id=brands.id AND brand_mappings.object_id=1234}
      );
    Map brandsConnectedMap = new HashMap();
    for(int i=0; i  id
    }
    
    /* If you're following now, brandsConnected is most definitely a subset of the elements in brands. */
    
    // -snipped code-
    // much later...
    
    for(int i=0;i  

    I think the way I've abbreviated it has even made it more clear for you, dear reader. Just imagine how many data structures, flow-control and looping constructs could be avoided if this were done just a bit simpler.


    Still missing a bit of code though



  • When I finally figured out what a co-worker's code was supposed to do (by adding a bunch of my own comments describing lines and blocks), it was a forehead-slapping moment. Now although we write Java code, we're constrained to a proprietary framework and libraries. The code doesn't directly speak SQL -- we are forced to use a proprietary library which does that on our behalf. (Trust me, I'm doing you a favor by not explaining the proprietary stuff further.) Here's (essentially) what is code did:

    MapList brands = ProprietaryClass.findObjects( {pseudocode: select id,name from brands} ); // MapList == java.util.List of Map objects
    MapList brandsConnected = ProprietaryClass.findObjects(
      {pseudocode: select id,name from brand_mappings, brands where brand_mappings.brand_id=brands.id AND brand_mappings.object_id=1234}
      );

    Map brandsConnectedMap = new HashMap();

    for(int i=0; i < brandsConnected .size(); i++) {

      Map m = (Map) brandsConnected.get(i);

      brandsConnectedMap.put( m.get("name"), m.get("id") ); // mapping name -> id

    }

    /* If you're following now, brandsConnected is most definitely a subset of the elements in brands. */


    // -snipped code- much later...



    for(int i=0;i<brands.size();++i){>

      Map brand=(Map)brands.get(i);

      String brandName=(String)brand.get("name");

      String brandId=(String)brand.get("id");

      if(brandId.equals(profileBrandMap.get(brandName))) {

        System.out.println(brand.get("name"));

      }

    }

    I think the way I've abbreviated it has even made it more
    clear for you, dear reader. Just imagine how many data structures, flow-control
    and looping constructs could be avoided if this were done just a bit simpler.

    Update: The new WTF is that the form timed out while I was trying to fix the formatting that the forum's WYSIWYG editor ate. Ack. Thank cthulu I pasted it into notepad first.


Log in to reply