views:

559

answers:

4

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?

+10  A: 

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());
Chad Grant
Though, you still have a string there. That should probably be in a constant or a property somewhere.
Kobi
But if it's camelCase it gets it from the Registry instead of the DB, duh!
Chad Grant
+4  A: 

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):
   ...
"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
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.
+3  A: 

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.

Yorgos Pagles
+3  A: 

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.
MSalters
That seems fine to me - it avoids cluttering your code with magic numbers, even if those numbers are constant and unchanging.
Visage
+1 for Indiana dig.
Carl Manaster
I think the ultimate in softcoding is more like:const int ONE = 1;const int TWO = 2;etc.
tfinniga
Since 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
..and in the news today, some earnest people didn't get a joke.
Roger Nolan
If it was a joke, it was too subtle for me - yes.
Jonathan Leffler