What is soft coding? (Anti-pattern)
Asked Answered
G

5

17

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?

Gelsenkirchen answered 5/5, 2009 at 6:34 Comment(1)
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.Reynaud
K
26

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());
Kreit answered 5/5, 2009 at 6:38 Comment(1)
Though, you still have a string there. That should probably be in a constant or a property somewhere.Plantain
M
7

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.

Myall answered 5/5, 2009 at 8:3 Comment(0)
S
2

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):
   ...
Schaeffer answered 5/5, 2009 at 6:40 Comment(2)
"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.Piecedyed
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.Schaeffer
L
1

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.

Lactam answered 19/11, 2019 at 10:38 Comment(0)
R
-1

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.
Rottweiler answered 5/5, 2009 at 9:34 Comment(8)
That seems fine to me - it avoids cluttering your code with magic numbers, even if those numbers are constant and unchanging.Mcgruder
I think the ultimate in softcoding is more like: const int ONE = 1; const int TWO = 2; etc.Reorder
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.Bundle
..and in the news today, some earnest people didn't get a joke.Cos
If it was a joke, it was too subtle for me - yes.Bundle
@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.Abelmosk
Maybe 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.Fustic
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.Nerine

© 2022 - 2024 — McMap. All rights reserved.