Results 1 to 7 of 7
  1. #1
    AdamGibbons is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default Name this Anti-pattern

    Hello Java peoples,

    I'm trying to recall the name of an anti-pattern I have come across before and ideally find some solid literature on why it's very naughty and not really a good practice.

    Consider:

    Java Code:
        public static boolean isMethodInvokedInCurrentThread(final String methodName) {
    		final StackTraceElement e[] = Thread.currentThread().getStackTrace();
    		final ArrayList<String> methodNames = new ArrayList<String>();
    	    for (final StackTraceElement s : e) {
    	    	methodNames.add(s.getMethodName());
    	    }
    	    return methodNames.contains(methodName)? true : false;
        }
    
    	public NodeRef checkoutCopy(final WorkingCopy workingCopy, final boolean recordLockActivityLog){
    		final PublicationVersion     publicationVersion = publicationsManager.getPublicationVersionForWorkingCopy(workingCopy);
    		final Publication            publication        = publicationsManager.getPublicationForVersion(publicationVersion);
    		if (publication.isSerialEditing()) {
    //Very naughty
    if(!isMethodInvokedInCurrentThread("createPublication") && recordLockActivityLog) {
    				activityLogService.log(new IdfPublicationActivity(IdfActivityType.PUBLICATION_LOCKED, publication, publicationVersion));
    			}
    		}
            return checkOutCheckInService.checkout(workingCopy.getNodeRef());
        }

    As you can see from the above code the behavior of checkOutCopy() changes based on the code that is calling it. Not only that but it's looking for a name as just a static string; so if the method is refactored at a later date, this will unwittingly break this code and we'll be none the wiser.
    Perhaps this isn't an anti-pattern per se, but my gut tells me: no no no no

    Please help me educate my colleges in the dangers of coding in this fashion!

    Regards,
    Adam

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Whatever it is, it's very fragile (as you've identified), just from the whole idea of attempting to identify whether a particular method name exists in the stack trace, and deciding what to do based on that.

    Fragile is generally a Bad Thing.

  3. #3
    AdamGibbons is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    Right! Exactly! Any idea if this specific thing has a name? Is it an anti-pattern?

    I'd really like to find an article online that I can point to and say 'look read for yourself, this should not be in our code'.

    Cheers for the speedy reply!

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    No idea on anit-pattern. The only one of those I know is Ball-of-Mud, which I see all the time.

    But...a quick wiki, and I would hazard a guess at hard coding. Whatever it is, though, it's a code smell. Something is Not Quite Right.

  5. #5
    AdamGibbons is offline Member
    Join Date
    May 2011
    Posts
    3
    Rep Power
    0

    Default

    I actually meant more to do with the fact that the behavior is changing depending on the calling method (rather than the arguments passed to it); which seemed really bad to me, but yeah obviously the hard coded method name is bad too (although not as bad in my opinion.)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Well, yes.
    But I would say that the obvious symptom of that is this hardcoded method name.
    It's the symptoms that flag problems up for me.

    Hmmm, maybe this is a form of action at a distance? That is, something is happening in that method based on stuff elsewhere in the system that is not necessarily easy to identify? Using the stack trace would count in my opinion.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,038
    Rep Power
    10

    Default

    In addition to what Tolls already said, you might give the list in this article a read-through: Anti-pattern - Wikipedia, the free encyclopedia
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. strategy pattern and bridge pattern
    By jomypgeorge in forum New To Java
    Replies: 2
    Last Post: 12-13-2010, 06:13 AM
  2. Anti virus
    By gyan916 in forum Advanced Java
    Replies: 3
    Last Post: 08-03-2010, 11:22 AM
  3. Class pattern to generate following pattern:-
    By vxs in forum New To Java
    Replies: 5
    Last Post: 07-15-2010, 12:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •