0

Question is: What is target-dependent code and how does it effect portability of a software?


Motivation to ask this question

In a CS class, I'm told that the more target-dependent code software has, the more portable it will be.But it feels wrong to me, actually I think the opposite.

Justification was that target-dependent code means an implementation that is specific to a target and more target you cover means you can run software on more targets. (Target is defined as an hardware& software environment) As you can run the software on more targets, it makes the software more portable.

  • 2
    This sounds like a misunderstanding. We have no idea what your professor actually meant by that, you should probably ask them. All we could do would take a wild guess. But I agree that the statement “more target-dependend code makes software more portable” does sound rather wrong. – amon May 9 '17 at 20:36
  • You don't have to guess what my professor actually meant. First paragraph is my motivation to ask this question, second paragraph is the question. Secondly, I discussed it with him in class, so there is no misunderstanding. – ferit May 9 '17 at 20:41
  • In that case, you can please edit the question to more fully describe the professor's reasoning. What evidence or arguments did they give to support their statement? It would also be useful to see the definitions you are using in class for “portability” and “target-dependent code” – these terms do not have a clear definition and depend a bit on the context. If you or the professor are not fully fluent in English, this may also just be a translation error. – amon May 9 '17 at 20:53
  • But what I'm telling you is, it is not important what my professor's reasoning. My objective is not to understand why my professor said it. My objective is to learn the correct thing. Anyway, as you wish it a lot, I'll include his reasoning in edit. – ferit May 9 '17 at 20:57
  • 1
    You may have misheard or misundersood what he said, but don't rule out the possibility that your prof was flat wong, because that happens more often than you'd like to think it does. – Blrfl May 9 '17 at 21:45
2

Justification was that target-dependent code means an implementation that is specific to a target and more target you cover means you can run software on more targets. ... As you can run the software on more targets, it makes the software more portable.

There's a very important difference between programs that can be made to run correctly on multiple targets and programs written with portable code.

Absent a standard, the way you make code run correctly on multiple targets is to write one implementation per target or group of targets that behave the same way. This sort of development was at its worst in code developed to run on Unix systems during the 1980s and early 1990s as competing vendors tried to capture as much of the market as possible and become "the" standard. It was not unusual for a program to make extensive use of conditional compilation or execution to make it work on the raft of vendor-specific flavors of System V and BSD. The implementations may be conveniently bound together into a single file and selected based on outside factors, but that doesn't change the fact that there are multiple implementations. Programs like this are not, by definition, portable because what runs on each target is different (i.e., it's target-specific code). They are, as gnasher729 points out in his answer, ported.

Portability comes from the adoption of standards that offer a well-defined API for programs to use that handles a target's idiosyncrasies behind the scenes. For example, I can be confident that this bit of C will compile and run as expected on Windows, Linux, Solaris or OS X:

int file = open("somefile", O_RDONLY);
lseek(file, 12345, SEEK_SET);
char buffer[1024];
read(file, buffer, 1024);
/* Do something with buffer */
close(file);

This is portable code because all of the targets listed above have ISO C compilers and libraries that conform to POSIX, where the P stands for portable.

The litmus test for portability is not having to make changes for it to run on a new target. What your prof is saying boils down to "a program can be run on any target where you can beat the source code into submission." In my book, that's the polar opposite of portability and makes him dead wrong.

3

When we call software "portable" it means that it is easy to make the software run on different targets. Usually that means it is possible to make the software run on different targets with very little target dependent code. Software that is less portable needs more target dependent code to make it run on different targets.

Of course if you make the software run on ten targets, you will have more target dependent code than for two targets. But that doesn't mean the software is "portable". It means the software has been ported.

  • It's not just the amount - to me it's also about modularity. Any code that varies by target should be factored out so that adding a new target is as simple as providing new implementations for some existing interfaces. – Doval May 11 '17 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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