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.
Many a times programmers makes a common mistake by assuming size of pointer and long same.If in some expression sizeof(long) is used, it may give different result on 32-bit and 64-bit OS version. Like if we talk about Microsoft Visual Studio running on 64-bit OS version the pointer size would be 8 byte and size of long comes out to be 4 byte. Program written with such assumption would give false result or may even get crash.So, one has to be very cautious while using data type size across the platform.
2) Don't use specific system constant.
System specific constant should not be used as they are not portable, we are some time not aware of them also. Like system constant "NULL" is specific to windows and will give compilation error on other platform. Instead of "NULL" one can use "0".
3) System file/folder path notation may vary on different platform.
When working with file path one need to be cautious for example "c:\\TestFile.txt" will work on Windows but give error on Linux.For this one i recommend to use forward slash "c://TestFile.txt" , it would work well on both windows and Linux.
4) Avoid using system specific models/libraries.
Don't use system specific models/libraries like Event handling model, Threading libraries, File Creation libraries etc.. . As they are not compatible across platform. Write a wrapper around such models and within wrapper use generic portable libraries. For example, Windows even handling model is totally different from Linux. Windows have special mode for handling events, like we may not find timed wait for multiple object on other platform.
5) Always write default statement in switch case.
Many latest compiler gives compilation error if default is not specified.
6) Always specify return type for functions.
Many latest compiler gives compilation error if return type is not specified.
7) Always specify type with static variables.
Variables declared with static keyword must contain data type with it, some old compiler take int as default type but modern compiler will generate compilation error for it.
8) Always take care of scope of variable.
Like some compiler support variable scope limited to for() while some compiler dont.
For example:-
Don't prefer writing code as below (Non-portable code).
{
for(int i ; ;)
{
//do some thing
}
for(int i ; ;)
{
//do some thing
}
}
Prefer writing code as below (Portable code)
{
for(int i ; ;)
{
//do some thing
}
for(int j ; ;)
{
//do some thing
}
}
9) Don't use C++ commenting style in C code.
Don't use // commenting style in c code, as compile other then microsoft visual studio may generate error for it. Prefer using /* */ commenting style.
10) Take care of include depth for header files and also for file code size.
Microsoft visual studio compiler generated error like "internal compiler error" if include depth is too large or file size exceeds certain limit. Always take care of file size and include depth.
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. I will cover this topic separately bye for now.
Hope you enjoyed this post ! <<plug removed>>
Keep Rocking
-Tajendra