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
-Tajendra



58 comments:

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.

Repairing of hp laptop are very well done by us as we have experts that are well versed with the technology used in hp laptop to build them. Get in touch with us for swift & skilled help in repairing your hp laptop system. hp service center in Delhi. Call us: 7217871051
Hp Service Center in Delhi

Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7217871051
Hp Service Center in Ghaziabad

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7217871051
Lenovo Service Center in Ghaziabad

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 7217871051
HP Service Center in Uttam Nagar

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 7217871051
Lenovo Service Center in Neta ji subhash Place

Dell Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Dell Service Center in Rajouri Garden

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Hp Service Center in Rajouri Garden

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324
Lenovo Service Center in Rajouri Garden

Dell Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Dell Service Center in Lajpat-Nagar

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 9891868324
Lenovo Service Center in Nehru-Place

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 9891868324
Lenovo Service Center in Pitampura

Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 9891868324
Hp Service Center in Pitampura

Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324.
Hp Service Center in Mangolpuri

Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 9891868324
Lenovo Service Center in Mangolpuri

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Hp Service in Center in Okhla

Lenovo Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Lenovo Service Center in Greater Noida

Hp Laptop Service Center offer fixing for your PC needs, we are experts in PC chip level fix, call us: 9891868324.
Hp Service in Center in Greater Noida



Hp Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 7042640351.
Hp Service Center in Chattarpur



Lenovo Laptop Service Center offer fixing for your PC needs, we are specialists in PC chip level fix, call us: 7042640351.
Lenovo Service Center in Chattarpur


Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838122281.
Hp Service Center in BKC-Mumbai

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838122281.
Dell Service Center in Mumbai

Hp Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838446546.
Hp Service Center in Mumbai

Lenovo Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838446546.
Lenovo Service Center in Mumbai

Dell Laptop Service Center offer repairing for your laptop needs, we are experts in laptop chip level repair, call us: 7838446546.
Dell Service Center in Mumbai

The school maintains the authors' obligation to greatness in all fields, with accentuation on its maxim Higher Stronger Brighter. High ethics are an essential of a decent individual. Contact on us: 8130269997.
Nursery Admissions 2021-2022
Best Schools in Delhi
GD GOENKA Rohini

We are 100% trustworthy Dell laptop Service Center in Udyog Vihar. The Dell Laptop Service Center provides repair services for various types of Dell Laptop, computer and office-related devices. Our Dell laptop service center Gurgaon technician will do a more thorough inspection immediately & fix quickly. We are best Dell Laptop Repairing Center and a quality service provider for Dell laptops. We can fix all major issues related to board replacement, software installation, screen replacement etc. Dell Service Center Udyog Vihar. Dell Laptop Repairing Service Center Udyog Vihar phase 1, phase 2, phase 3,Gurgaon. Udyog Vihar Gurgaon is the production hub of Gurgaon. Our Expertise is helped you choose better vendors from the dell laptop repair industry. Always focusing service and target. Our target is to satisfy customers by more efficient and genuine services. We are not targeting money, so we updated dell laptop spare parts price list to the website for customers willing to know prices and judge prices by their self-comparing by other vendors.

dell service center in udyog vihar Gurugram , dell service center in udyog vihar gurgaon, dell service center in udyog vihar, dell Laptop service center in udyog vihar Gurugram , dell Laptop service center in udyog vihar gurgaon, dell Laptop service center in udyog vihar, dell service center udyog vihar Gurugram , dell service center udyog vihar gurgaon, dell service center udyog vihar, dell Laptop service center udyog vihar Gurugram , dell Laptop service center udyog vihar gurgaon, dell Laptop service center udyog vihar.

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Outdoor Advertising Companies in Delhi

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Outdoor advertising

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Hoardings in Delhi

The Company eyes the vision of getting one of the India's notable and rumored Ad Agency by satisfying our customer's requirements and giving our significant customers the best quality outcomes utilizing our ability and rich encounters of publicizing area. We have practical experience in vital and inventive Brand Promotion arrangements in OOH.
Hoardings in Delhi
Rural wall painting advertising
Digital wall painting
Transit Media Service

Arising as a broadly perceived promoting organization in all features of publicizing and marking arrangements centering in outside, on the web and electronic media spaces. Contact on us: 9868946230.
Hoardings in Delhi
Digital wall painting
Auto Rickshaw advertising

One thing we make sure that we provide tobacco-free paan and currently we have a paan franchise in INDIA so people who want to eat paan or purchase paan just type of google paan near me and you find your nearest mast banarasi paan shops.
Karva Chauth Suhag Thali
Paan Café
Flavour Paan
Paan Franchise

Four Square Media Services is engineered in Delhi NCR. The Company eyes the vision of getting one of the India's striking and assumed Outdoor Advertising Companies in Delhi by fulfilling our client's basics and giving our colossal clients the best quality results using our ability and rich experiences of advancing. Contact on us: 9868946230.
Hoardings in Delhi
Digital wall painting
Wall painting advertising
Outdoor advertising

Four Square Media Services is engineered in Delhi NCR. The Company eyes the vision of getting one of the India's striking and assumed Outdoor Advertising Companies in Delhi by fulfilling our client's basics and giving our colossal clients the best quality results using our ability and rich experiences of advancing. Contact on us: 9868946230.
Digital wall painting
Auto Rickshaw advertising
Advertising wall painting
Auto rickshaw ad in delhi
Rural wall painting advertising

Dell is a well-known laptop & computer brand of Dell Technologies which is a reputed company based on IT situated in the USA.

Dell Service Center in Sushant Lok Phase 1, 2

Bs system solution in Delhi provides the best solution for laptop repair. Our experienced engineers take complete care and responsibility for your device and provide you a high-quality solution. The Lenovo Laptop Technical support is available for all range of customers. IF YOU ARE INTERESTED YOU MAY CALL -9891868324, 9643406897, and 9643409808
laptop repairing center in Delhi
best laptop service center in Delhi

Bs system solution in Delhi provides the best solution for laptop repair. Our experienced engineers take complete care and responsibility for your device and provide you a high-quality solution. The Lenovo Laptop Technical support is available for all range of customers. IF YOU ARE INTERESTED YOU MAY CALL -9891868324, 9643406897, and 9643409808
laptop service in Delhi
laptop reparing center in Mumbai

Bs system solution in Delhi provides the best solution for laptop repair. Our experienced engineers take complete care and responsibility for your device and provide you a high-quality solution. The Lenovo Laptop Technical support is available for a range of customers. IF YOU ARE INTERESTED YOU MAY CALL -9891868324, 9643406897, and 9643409808.
multi brand laptop repairing in Mumbai
Best laptop repairing center in Mumbai

Laptop Service Center Mumbai provides laptop repair home service in all over Mumbai. if you are facing any issue with your laptop, then you may call us and you also visit our nearest service station.
There is list of Authorised Laptop Service Center -
Dell Authorised Service Center in Mumbai
Lenovo Authorised Service Center in Mumbai
Hp Authorised Service Center in Mumbai
Asus Authorised Service Center in MumbaiAcer Authorised Service Center in Mumbai
Acer Authorised Service Center in Mumbai

This comment has been removed by the author.

Post a Comment