Code portability basically refers to making source code able to compile on different platform without making any changes in source code.
While coding its very important to keep portability in mind.The best way to introduce code portability is while coding.Keeping certain things into account we can achieve code portability with lesser effort, which we will discuss in this post.There are certain tools too which detect portability problems on source code, its a post processing of code and requires extra effort.
Non-portable code introduces problems like maintenance of different versions, reduces readability, reduces understanding of code etc...
Efforts needs to make legacy or old source code portable, can really make you feel lost in this big programming ocean. So, the best policy is to keep portability into account while writing code, it saves lots of time and efforts on rework. Big question now is - "How to write portable code?".Our source code should be compatible with different environment like different processor, different OS, different version of libraries etc... In this post we would focus on basic tips need to be kept in mind while writing code.
1) Don't assume data type size to be constant across platform, as it may change with platform.
2) Don't use specific system constant.
3) System file/folder path notation may vary on different platform.
4) Avoid using system specific models/libraries.
5) Always write default statement in switch case.
6) Always specify return type for functions.
7) Always specify type with static variables.
8) Always take care of scope of variable.
9) Don't use C++ commenting style in C code.
10) Take care of include depth for header files and also for file code size.
I have tried to cover 10 basic tips for code portability for beginners though there are several other areas too, where we need to focus on advanced portability issues, for e.g. dealing with classes, virtual functions, exception handling, compiler directives, run-time identification.
Hope you enjoyed this post !