I found the Wikipedia entry on the soft coding anti-pattern terse and confusing. So what is soft coding? In what settings is it a bad practice (anti-pattern)? Also, when could it be considered beneficial, and if so, how should it be implemented?
-
This does not apply to Wikipedia's article on softcoding but a useful little trick sometimes is to switch your language from "English" to "Simple English" on Wikipedia. – Travis May 05 '09 at 17:26
5 Answers
Short answer: Going to extremes to avoid Hard Coding and ending up with some monster convoluted abstraction layer to maintain that is worse than if the hard coded values had been there from the start. i.e. over engineering.
Like:
SpecialFileClass file = new SpecialFileClass( 200 ); // hard coded
SpecialFileClass file = new SpecialFileClass( DBConfig.Start().GetConnection().LookupValue("MaxBufferSizeOfSpecialFile").GetValue());

- 44,326
- 9
- 65
- 80
-
13Though, you still have a string there. That should probably be in a constant or a property somewhere. – Kobi May 05 '09 at 07:01
The main point of the Daily WTF article on soft coding is that because of premature optimization and fear a system that is very well defined and there is no duplicated knowledge is altered and becomes more complex without any need.
The main thing that you should keep in mind is if your changes actually improve your system and avoid to lightly label something as anti-pattern and avoid it by all means. Configuring your system and avoiding hardcoding is a simple cure for duplicated knowledge in your system (see point 11 : "DRY Don't Repeat Yourself" in The Pragmatic Programmer Quick Reference Guide) This is the driving need behind the suggestion of avoiding hardcoding. I.e. there should be ideally only one place in you system (that would be code or configuration) that should be altered if you have to change something as simple as an error message.

- 1,836
- 3
- 18
- 29
Ola, a good example of a real project that has the concept of softcoding built in to it is the Django project. Their settings.py file abstracts certain data settings so that you can make the changes there instead of embedding them within your code. You can also add values to that file if necessary and use them where necessary.
http://docs.djangoproject.com/en/dev/topics/settings/
Example:
This could be a snippet from the settings.py file:
num_rows = 20
Then within one of your files you could access that value:
from django.conf import settings
...
for x in xrange(settings.num_rows):
...

- 29,188
- 15
- 39
- 42
-
"num_rows" here might be a little misleading, and a poor example, as the settings file is (should be) used for global and/or application specific settings like "default_comment_count", "template_directory". 'num_rows' looks a bit generic. – anonymous coward Jun 02 '09 at 13:56
-
Whereas this is an example and the actual variable really should not matter, 'num_rows' actually fits within the settings file. Lets say you have a couple pages with data tables in them. A simple way to manage the number of rows that show up within those tables is with a variable in the settings file. – johannix Jun 02 '09 at 16:19
Soft-coding: it is process of inserting values from external source into computer program. like insert values through keyboard, command line interface. Soft-coding considered as good programming practice because developers can easily modify programs. Hard-coding. Assign values to program during writing source code and make executable file of program.Now, it is very difficult process to change or modify the program source code values. like in block-chain technology, genesis block is hard-code that cannot changed or modified.

- 358
- 3
- 9
The ultimate in softcoding:
const float pi = 3.1415; // Don't want to hardcode this everywhere in case we ever need to ship to Indiana.

- 44,500
- 61
- 101
- 156

- 173,980
- 10
- 155
- 350
-
2That seems fine to me - it avoids cluttering your code with magic numbers, even if those numbers are constant and unchanging. – pauljwilliams May 05 '09 at 09:36
-
5I think the ultimate in softcoding is more like: const int ONE = 1; const int TWO = 2; etc. – tfinniga May 05 '09 at 17:33
-
6Since pi = 3.1415926536, using pi = 3.1415 instead of 3.1416 strikes me as sloppy - in Indiana and elsewhere. Unless one count in the 5th place doesn't matter. I was recently working with a team where the difference between a 16-digit approximation to pi and a 32-digit approximation was causing discrepancies between two 'supposed to be identical' sets of results. The discrepancies were only accounted for once the difference was recognized. Rare - in the extreme; but it happened. – Jonathan Leffler May 06 '09 at 06:26
-
6
-
-
@Jonathan Leffler: Actually, defining constants for *byte* constant of 1, 2, 3, etc. can be useful, since at least in vb there is, for whatever reason, no suffix to designate that a numeric constant should be of type byte. – supercat Aug 02 '11 at 19:52
-
3Maybe I'm missing the joke, but this isn't soft-coding at all. The value is hard-coded, albeit defined as a named constant. Soft-coding would be to load PI from a config file, database, web service, etc. – Misko Oct 19 '12 at 21:42
-
Agree with @CodeMonkey1. Defining *constants* is not softcoding. The value is still in the code. Softcoding is putting the values in a configuration file or DB or whatever so that it is not in the code. Defining constants is a great practice for readability and avoiding errors. It appears that it is difficult for quite a few people to grasp the actual concept of softcoding vs hardcoding. – ThunderGr Dec 06 '13 at 15:24