Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange
209

In a language that compiles, a magic string's value is not checked at compile time. If the string must match a particular pattern, you have to run the program to guarantee it fits that pattern. If you used something such as an enum, the value is at least valid at compile-time, even if it might be the wrong value. If a magic string is being written in ...


201

The data itself is called "tramp data". It is a "code smell", indicating that one piece of code is communicating with another piece of code at a distance, through intermediaries. Increases rigidity of code, especially in the call chain. You are much more constrained in how you refactor any method in the call chain. Distributes knowledge about data/methods/...


148

Is source code generation an anti pattern? Technically, if we generate code, it is not source even if it is text that is readable by humans. Source Code is original code, generated by a human or other true intelligence, not mechanically translated and not immediately reproducible from (true) source (directly or indirectly). If something can be generated,...


127

As NickWilliams has already said: the concept the OP describes is called idempotent (noun Idempotency). It is indeed common practice, especially in high-level APIs. BUT: Rename the function. Instead of startHttpServer call it makeSureHttpServerIsRunning or ensureHttpServerIsRunning. When a function is called startHttpServer, readers expect it to start a ...


124

Even if you do need both the numeric and the string representation of a number, it's better to convert just once and also hang on to the original value, instead of converting again every time you need one or the other. The principle is, as always, that code that doesn't exist cannot have subtle defects, while code that exists often does. That may sound ...


105

The use case that exceptions were designed for is "I just encountered a situation that I cannot deal with properly at this point, because I don't have enough context to handle it, but the routine that called me (or something further up the call stack) ought to know how to handle it." The secondary use case is "I just encountered a serious error, and right ...


103

There's a detailed discussion of this on Ward's Wiki. Generally, the use of exceptions for control flow is an anti-pattern, with notable situation- and language-specific cough exceptions cough. As a quick summary for why, generally, it's an anti-pattern: Exceptions are, in essence, sophisticated GOTO statements Programming with exceptions, therefore, leads ...


101

I don't think this, in itself, is an anti-pattern. I think the problem is that you are thinking of the functions as a chain when really you should think of each one as an independent black box (NOTE: recursive methods are a notable exception to this advice.) For example, let's say I need to calculate the number of days between two calendar dates so I ...


98

Well written code should be sufficiently self-documenting that you don't need any comments explaining what the code does, because it is obvious from reading the code itself. This implies also that all functions and variables have descriptive names, although it might be needed to learn the lingo of the problem and solution domains. This does not mean that ...


89

The summit of what the other answers have grasped at, is not that "magic values" are bad, but that they ought to be: defined recognisably as constants; defined only once within their entire domain of use (if architecturally possible); defined together if they form a set of constants that are somehow related; defined at an appropriate level of generality in ...


86

This is obviously bad, right? Yea. It makes the methods non-reentrant, which is a problem if they are called on the same instance recursively or in a multi-threaded context. It means that state from one call leaks to another call (if you forget to reinitialize). It makes the code hard to understand because you have to check for the above to be sure what ...


83

There's a is a fairly large and varied set of conceptual and technical difficulties when trying to approach a relational database from an object oriented angle. These difficulties are collectively known as object-relational impedance mismatch and the related Wikipedia article is extremely informative. The article identifies quite a few, I don't see any ...


78

As a general guideline, libraries should be totally disconnected from the environment. That means that they shouldn't perform operations on standard streams, on specific files, or have any expectation about the environment or the context they are used. Of course, there are exceptions to this rule, but there must be a very good reason for it. In the case of ...


77

There is no need to have specific subclasses for every person. You're right, those should be instances instead. Goal of subclasses: to extend parent classes Subclasses are used to extend the functionality provided by the parent class. For example, you may have: A parent class Battery which can Power() something and can have a Voltage property, And a ...


75

This is sometimes unavoidable, especially if your recovery code might throw an exception. Not pretty, but sometimes there are no alternatives.


75

Yes, your colleague is right: that is bad code. If an error can be handled locally, then it should be handled immediately. An exception should not be thrown and then handled immediately. This is much cleaner then your version (the getValueByKey() method is removed) : public String getByKey(String key) { if (valuesFromDatabase.containsKey(key)) { ...


74

Lag/Latency? I call BS on that. There should be exactly zero overhead from this practice. (Edit: It has been pointed out in the comments that this can, in fact, inhibit optimizations performed by the HotSpot VM. I don't know enough about VM implementation to confirm or deny this. I was basing my comment off of the C++ implementation of virtual functions.) ...


73

This is a good forum discussion on this very topic and good points are brought up on both sides of the argument. http://www.dbforums.com/database-concepts-design/1619660-otlt-eav-design-why-do-people-hate.html [dead link] https://web.archive.org/web/20140831134758/http://www.dbforums.com/database-concepts-design/1619660-otlt-eav-design-why-do-people-hate....


65

It's not an antipattern, it's a bad practice. The difference between an antipattern and a mere bad practice is here: anti-pattern definition. The new workplace style you show is a bad practice, vestigial or pre-OOP times, according to Uncle Bob's Clean Code. Arguments are most naturally interpreted as inputs to a function. Anything that forces you ...


64

Practical reasoning OK, I know that code is data as well. What I don't understand is, why generate source code? From this edit, I assume you are asking on a rather practical level, not theoretical Computer Science. The classical reason for generating source code in static languages like Java was that languages like that simply did not really come with ...


61

If a language inherently supports exceptions, then it is preferred to throw exceptions and the clients can catch the exception if they do not want it to result in a failure. In fact, the clients of your code expect exceptions and will run into many bugs because they will not be checking the return values. There are quite a few advantages to using exceptions ...


61

BobDalgleish has already noted that this (anti-)pattern is called "tramp data". In my experience, the most common cause of excessive tramp data is having a bunch of linked state variables that should really be encapsulated in an object or a data structure. Sometimes, it may even be necessary to nest a bunch of objects to properly organize the data. For a ...


59

Some suggestions: There is nothing wrong in having a lot of feature or bugfix branches as long as the changes done in each branch are small enough you can still handle the resulting merge conflicts in an effective manner. That should be your criterion if your way of working is ok, not some MSDN article. Whenever a branch is merged into trunk, the trunk ...


54

It is only worthwhile to call some bad coding habit an Antipattern if it is reasonably widespread. The rest we just call "rubbish code" ... If I was to suggest a name for this particular bad habit, it would be "Obsessive Abstraction Disorder" :-)


54

To answer that question, I'm going to ask you a rhetorical question about another structure that have similar property to the DOM elements that jQuery manipulates, that is the good old iterator. The question is: How many operation do you need on a simple iterator? The question can be answered easily by looking at any Iterator API in a given language. You ...


54

Someone (attributed to Richard C. Haven, a Delphi programmer) once wrote: Beginners comment nothing. Journeymen comment the obvious. myVar = myVar + 1; // add one to myVar /* This method adjusts the page margin by the appropriate device offset */ Masters comment the reason for not doing it another way /* I already tried obvious approaches A, B, and C ...


54

If all you want to do is create class X with certain arguments, subclassing is an odd way of expressing that intent, because you aren't using any of the features that classes and inheritance give you. It's not really an anti-pattern, it's just strange and a bit pointless (unless you have some other reasons for it). A more natural way of expressing this ...


50

Golden Hammer The golden hammer is a tool chosen only because it is fancy. It is neither cost-effective nor efficient at performing the intended task. source: xkcd 801 (Despite the down-votes, I stand by this answer. It might not exactly be the opposite of re-inventing the wheel semantically, but It fits every example mentioned in the question)


45

You refer to technical debt. We all accrue technical debt in the products we develop over time; refactoring is one of the very common and effective ways of reducing this technical debt, though many companies never pay down their technical debt. These companies tend to find their software extremely unstable years down the road, and the technical debt becomes ...


44

why generate code? Because programming with punch cards (or alt codes in notepad) is a pain. If it is being done for performance reasons, then that sounds like a shortcoming of the compiler. True. I don't care about performance unless I'm forced to. If it is being done to bridge two languages, then that sounds like a lack of interface library. Hmm, ...


Only top voted, non community-wiki answers of a minimum length are eligible