Menu madness



  • My company's app has a navigation menu that sits on the left hand side of the screen. There was a minor menu display bug and I was assigned to fix it. Shouldn't be too complicated, I thought, and got stuck in. After nearly an hour of searching, I finally found the code that actually displays the menu. To my horror, it looked like this. Note the 14 "if" statements in one method. Bear in mind that this is just one method of one of the many, many Jsp's involved!

     

     
        /*

         * (non-Javadoc)

         * @see javax.servlet.jsp.tagext.Tag#doStartTag()

         */

        @Override

        public int doStartTag() throws JspException {

            Object obj = pageContext.getAttribute(getId());

            AclEntry aclEntry = (AclEntry) pageContext.getSession().getAttribute(Constants.ACL_ENTRY_KEY);

            Principal principal = (Principal) aclEntry.getPrincipal();

            if (obj != null) {

                // Query database, do not show deleted folders

                FolderEntry fe = (FolderEntry) obj;

                try {

                    if (fe.getEntryId() > 0) {

                        Directory d = DirectoryDB.getInstance().selectEntry(fe.getEntryId(), principal);

                        if (d == null || !d.getShowName().equals(fe.getShowName())) {

                            return SKIP_BODY;

                        }

                    }

                } catch (SQLException e) {

                    e.printStackTrace();

                    return SKIP_BODY;

                }

                setFolderEntry(fe);

            } else {

     
                // Make parent

                try {

                    Class cls = Class.forName(getClassName());

                    Class[] types = new Class[]{Owner.class};

                    Constructor constructor = cls.getConstructor(types);

                    Owner owner = AclManager.getInstance().newOwner(principal);

                    FolderEntry entry = (FolderEntry) constructor.newInstance(new Object[]{owner});

                    entry.setShowName(getName());

                    entry.setDateEntry(Calendar.getInstance());

                    setFolderEntry(entry);

                    // Set tree in session

                    List<Directory> list = new ArrayList<Directory>();

                    list.add(entry);

                    pageContext.getSession().setAttribute(getTargetSessionName(), list);

                } catch (Exception e) {

                    e.printStackTrace();

                    return SKIP_BODY;

                }

            }

     
            StringBuilder builder = new StringBuilder();

            builder.append("<td width="8"><img width="8" height="25" border="0" src="");

            builder.append(((HttpServletRequest) pageContext.getRequest()).getContextPath());

            builder.append("/image/spacer.gif" alt=""/></td>");

            

            // Show dotted line image

            try {

                String[] buf = new String[getFolderEntry().getHeight()];

                if (!getFolderEntry().isRoot()) {

                    Directory p = getFolderEntry();

                    if (!isYoungestChild(getFolderEntry())) {

                        // Not youngest

                        buf[getFolderEntry().getHeight() - 1] = getIconImage(getLineIconUdr());

                    } else {

                        // Youngest

                        buf[getFolderEntry().getHeight() - 1] = getIconImage(getLineIconUr());

                    }

     
                    while (!p.getParent().isRoot()) {

                        p = p.getParent();

                        if (p.getJunior() != null && p.getJunior() instanceof FolderEntry) {

                            // Parent is not youngest

                            buf[p.getHeight() - 1] = getIconImage(getLineIconUd());

                        } else {

                            // Parent is youngest

                            buf[p.getHeight() - 1] = getIconImage(getSpacerIcon());

                        }

                    }

                }

                for (int i = 0; i < getFolderEntry().getHeight(); i++) {

                    builder.append(buf[i] != null ? buf[i] : "");

                }

     
                // Draw +/-, folder icon

                if (getFolderEntry() instanceof MenuDirectory) {

                    MenuDirectory menu = (MenuDirectory) getFolderEntry();

                    if (menu instanceof TopMenuDirectory) {

                        // Child has a menu

                        if (menu.isExpand()) {

                            builder.append(getMinusImage());

                        } else {

                            builder.append(getPlusImage());

                        }

                        builder.append(getMenuImage(menu.getImage()));

                    } else {

                        // Child is a normal folder

                        if (menu instanceof SimpleMenuDirectory) {

                            // If there are no children, do not show +/-, just a line.

                            // If root, show a + without an action.

                            builder.append(getLrImage());

                            builder.append(getMenuImage(menu.getImage()));

                        } else if (menu.isExpand()) {

                            builder.append(getMinusImage());

                            builder.append(getMenuImage(menu.getImage()));

                        } else {

                            builder.append(getPlusImage());

                            builder.append(getMenuImage(menu.getImage()));

                        }

                    }

                } else if (!getFolderEntry().hasFolderChildren(getFolderEntry().getClass().getName(), principal)) {

                    // If there are no children, do not show +/-, just a line.

                    // If root, show a + without an action.

                    if (getFolderEntry().isRoot()) {

                        builder.append(getPlusImageNoAction());

                        builder.append(getFolderImage(getFolderIconClose()));

                    } else {

                        builder.append(getLrImage());

                        builder.append(getFolderImage(getFolderIconClose()));

                    }

                } else if (getFolderEntry().isExpand()) {

                    builder.append(getMinusImage());

                    builder.append(getFolderImage(getFolderIconOpen()));

                } else {

                    builder.append(getPlusImage());

                    builder.append(getFolderImage(getFolderIconClose()));

                }

            } catch (SQLException e) {

                e.printStackTrace();

                return SKIP_BODY;

            }

     
            try {

                pageContext.getOut().print(builder.toString());

            } catch (IOException e) {

                e.printStackTrace();

            }

            return EVAL_BODY_INCLUDE;

        } 



  • At least it's indented....



  •  Not so bad code IMO.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.