12

I'm a self-taught, novice-ish coder, so I apologize if I don't nail the programmer lingo.

I'm working on a project in which I am providing data, which will be continually updated, to developers who will essentially create a tool to generate reports from queries on the data.

It seems that everyone involved thinks that they need to hard-code data values (not the schema, but the domains/values themselves) into the report-generation program.

For example, suppose we were reporting on personnel; the report would be split into categories, with a heading for each department, and then under each department heading will be subheadings of job titles, and then under each subheading will be a list of employees. The developers want to hard-code the departments and job titles. On the other hand, I would think that they could/would query out those things at runtime, sort records by them, and generate report headers dynamically based on what values are there.

Since the list of potential values will change over time (e.g., departments will be created/renamed, new job titles will be added), the code will need to be continually updated. It seems to me that we could skip the code maintenance steps and dynamically organize the reports.

Since I am not a developer, I'm wondering what I'm missing. What advantages might there be to hard-coding values into a tool like this? Is this typically how programs are designed?

  • 1
    possible duplicate of Removing hard-coded values and defensive design vs YAGNI – gnat Aug 9 '16 at 18:12
  • Are report cross-tabs, meaning values in rows should appear as columns? – Tulains Córdova Aug 9 '16 at 18:17
  • Question is not clear enough, specially paragraphs 4 and 5. – Tulains Córdova Aug 9 '16 at 18:45
  • Think I get it but please correct me: Example: they want a report of total hours spent grouped per department. Is that the idea? In that case you will need to implement aggregates, group by statements for example. Then you will be able to build the report dynamically. Other question: How do they get the data? Do they get flat data or more a relational database to build their report on? – Luc Franken Aug 9 '16 at 19:01
  • 1
    @Brendan why would you end up with three locations? Perhaps my understanding is incorrect but I'm envisioning an sql query to fetch data from a database, the the application will aggregate/group the returned values by, e.g., the department. If you're willing to have the overhead of multiple db queries, you could select distinct departments/role titles if you really want to. At no point is the data existing in more than one location - the report is being driven by the data. – kwah Aug 10 '16 at 10:03
9

Wikipedia:

Hard coding (also hard-coding or hardcoding) refers to the software development practice of embedding what may, perhaps only in retrospect, be considered an input or configuration data directly into the source code of a program or other executable object, or fixed formatting of the data, instead of obtaining that data from external sources or generating data or formatting in the program itself with the given input.

Hard-coding is considered an antipattern.

Considered an anti-pattern, hard coding requires the program's source code to be changed any time the input data or desired format changes, when it might be more convenient to the end user to change the detail by some means outside the program.

Sometimes you cannot avoid it but it should be temporary.

Hard coding is often required. Programmers may not have a dynamic user interface solution for the end user worked out but must still deliver the feature or release the program. This is usually temporary but does resolve, in a short term sense, the pressure to deliver the code. Later, softcoding is done to allow a user to pass on parameters that give the end user a way to modify the results or outcome.

  • Hardcoding of messages makes it hard to internationalize a program.
  • Hardcoding paths make it hard to adapt to another location.

The only advantage of hardcoding seems to be fast deliver of code.

  • 5
    OK, but the "only advantage" is often hugely important. Design decisions in programming are often about the trade-off between future proofing and quick delivery now, and as such, hard coding can be a perfectly acceptable choice. Sometimes not hard coding is a bad design choice. – user82096 Aug 10 '16 at 7:37
  • -1 I don't think this is a helpful answer. It essentially says 'embedding values into source code inappropriatly' is inappropriate. I think the OP wants guidance about when things may belong in source code and therefore fall outside your Wikipedia definition. – Nathan Cooper Aug 11 '16 at 22:14
  • Hard coding should be a vital part of your process and considering it an anti-pattern is outdated in the age of micro-services, with the Angular Tour of Heroes tutorial being a high profile example of a huge software house directly encoraging or even mandating as an intermediate step. What is more, when you move to dynamic data you should still retain some hard coded data as a fall-back, perhaps controlled by an environment variable or even a boolean toggle on the code itself so bugs and security issues can be properly isolated down the line. – Peter David Carter Mar 27 '18 at 18:50
23

Really? No Possible Valid Use Cases?

While I agree that hard-coding is generally an anti-pattern or at least a very bad code smell, there are plenty of cases where it makes sense:

  • simplicity (YAGNI),
  • the input is actually constant and will never change (ie it represents a natural or business constant or an approximation of one. e.g. 0, PI, ...),
  • embedded software (memory and allocation constraints come to mind),
  • secure software (these values are not the be available and/or easy to decode or reverse-engineer, e.g. cryptographic tokens and salts),
  • generated code (your preprocessor or generator is configurable, but spits out code with hard-coded values),
  • and probably a few more.

Still an Anti-Pattern? So is Over-Engineering! It's about your Software's Life Expectancy!!

Not that I'm saying there are all great reasons, and generally I'd balk at hard-coded values. But some can easily get a pass for valid reasons.

And don't oversee the first one regarding simplicity/YAGNI either by thinking it's trivial: there's probably no reason to implement a crazy parser and value checker for a simple script that does one job for a narrow use case very well.

It's difficult to find the balance. Sometimes you don't foresee that a software will get to grow and last longer than the simple script it started as. Oftentimes though, it's the other way around: we over-engineer things, and a project gets shelved faster than you can read the Pragmatic Programmer. You wasted time and effort on things than an early prototype did not need.

That's the mean things with Anti-Patterns: they're present in both extremes of the spectrum, and their appearance depends on the sensitivity of the person reviewing your code.

  • That's funny, because I piloted this myself, and it was much easier and faster and cleaner for me to handle the values dynamically. I did it in Python, whereas I believe the end product will be coded in Java--if this makes a difference. It felt over-engineered when I hard-code in the values, because each in-coming column had to be tracked in multiple places. – Tom Aug 9 '16 at 21:02
  • @Tom: You're saying it was easier and faster to implement (or even reuse) a configuration lookup library than to use an hard-coded value? Great for you. Also, I don't see how your last sentence fits the definition of over-engineering. It would feel obviously messy, and obviously if it's hard-coded and duplicated it's even worse (which was not the point of your question question, I probably misunderstood, but it seemed to me like you meant the value was not hard-coded in place every time, but in a single point in the program). – haylem Aug 9 '16 at 21:16
  • Anyways, I'm just pointing out cases where it'd be valid. I'm also pointing out that it'd be controversial in my last sentence. You can't please everybody and teams have people with varying skill levels. – haylem Aug 9 '16 at 21:18
  • Like I said, I'm a self-taught hack, so I probably mis-used the term over-engineered. I just meant that it took far fewer lines if code when values weren't hard-coded. My explanation is probably lacking due to a lack of a background in this field and my insufficient terminology--e.g., I have no idea what a configuration lookup library is. – Tom Aug 9 '16 at 22:18
  • 1
    You can have more complex things that are still sensible to hardcode. One that comes to mind that I wrote a few years back was all possible permutations of a set of values. I needed to find a random valid direction, picking a random permutation and then taking the first valid result was by far the most efficient solution and since it was in an O(N^3) loop efficiency mattered. – Loren Pechtel Aug 11 '16 at 20:20
5

There are times it's OK to hard-code values. For example, there are some numbers like 0, or one or various n^2-1 values for bitmasks that need to be certain values for algorithmic purposes. Allowing such values to the configurable has no value and only opens up the possibility of issues. In other words, if changing a value would only break things, it should probably be hardcoded.

In the example you gave, I don't see where hard-coding would be useful. Everything you mention would/should already be in the database including headings. Even things that drive the presentation (such as sort order) can be added if they aren't there.

  • Thanks. Sort order was the one concern I had. However, in our case it doesn't matter, and I didn't even consider that it could be added as another table in the database. – Tom Aug 9 '16 at 20:59
  • 1
    I should note that managing all of this in the DB is one option. You could also use configuration files or other solutions but hardcoding appears to be a poor choice. The DB option is often used because it's easy to create an interface to allow the options to be managed by users. There are also tools like this which are specifically designed for this purpose. – JimmyJames Aug 10 '16 at 14:29
-1

Implementing a robust solution that allows for values that might otherwise have been hard-coded to instead be configurable by the end users demands robust validation of those values. Did they put in an empty string? Did they put in something non-numeric where it should have been a number? Are they doing SQL injection? Etc.

Hard-coding avoids a lot of these risks.

Which isn't to say that hard-coding is always, or even often, a good idea. This is just one of the factors to take into account.

protected by gnat Aug 11 '16 at 19:00

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.