Results 1 to 7 of 7
Thread: Name this Anti-pattern
- 05-09-2011, 02:34 PM #1
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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
- 05-09-2011, 03:16 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 05-09-2011, 03:24 PM #3
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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!
- 05-09-2011, 03:35 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 05-09-2011, 03:38 PM #5
Member
- Join Date
- May 2011
- Posts
- 3
- Rep Power
- 0
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.)
- 05-09-2011, 03:48 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,448
- Rep Power
- 16
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.
- 05-09-2011, 04:18 PM #7
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
-
strategy pattern and bridge pattern
By jomypgeorge in forum New To JavaReplies: 2Last Post: 12-13-2010, 05:13 AM -
Anti virus
By gyan916 in forum Advanced JavaReplies: 3Last Post: 08-03-2010, 10:22 AM -
Class pattern to generate following pattern:-
By vxs in forum New To JavaReplies: 5Last Post: 07-14-2010, 11:15 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks