Mar 2, 2010

How to achieve code portability ? Basic 10 quick tips.

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 constants.
            System specific constant should not be used as they are not portable, we are some time not aware of them also. 

3) System file/folder path notation may vary on different platform.
           When working with file path one need to be cautious for example "\\testfolder\\TestFile.txt" will work on Windows but give error on Linux.For this one i recommend to use forward slash "/tesfolder/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 !

Keep Rocking


point no 3 - "c://TestFile.txt" will work under linux.

Could you please provide us working code snippet for linux?

You should use forward slashes in your filenames, even on operating systems that use
backslashes (DOS, Windows, OS/2, etc.).

Remember, the backslash ("\") is used in string literals to create special characters: "\n" is a
newline, "\b" is a backspace, and " " is a tab, "\a" is an "alert", "\v" is a vertical-tab, etc.
Therefore the file name "\version\next\alpha\beta est.dat" is interpreted as a bunch of very funny
characters. To be safe, use "/version/next/alpha/beta/test.dat" instead, even on systems that use a
"\" as the directory separator. This is because the library routines on these operating systems
handle "/" and "\" interchangeably.
Of course you could use "\\version\\next\\alpha\\beta\ est.dat", but that might hurt you (there's a
non-zero chance you'll forget one of the "\"s, a rather subtle bug since most people don't notice
it) and it can't help you (there's no benefit for using "\\" over "/"). Besides "/" is more portable
since it works on all flavors of Unix, Plan 9, Inferno, all Windows, OS/2, etc., but "\\" works
only on a subset of that list. So "\\" costs you something and gains you nothing: use "/" instead.

Post a Comment