Defining code regions in MPLAB X IDE

     In this post I would like to share "How to define code in regions using MPLAB X". This post has very much importance in good programming practice that's why I want to share it with you guys. I personally like this feature in MPLAB X remember this feature is only available in MPLAB X. With this feature you can define code in regions likewise you may have done in Microsoft Visual Studio. When you define certain code in regions you may easily handle the code you are interesting in. For example if you define certain interrupt setting registers code in one region and frequency setting code in other regions.

    Following figure 1 shows how an organized code looks like. Which I do in coding as general practice. In this code I organized all Debug LEDs, Spare Pins, Input buttons etc in separate regions. So whenever I want to change certain regions I quickly click on "+"  sign and made changes without looking at other regions code.
Figure 1. Organized code in regions.

     In the following figure 2. I unfold the comment and I have all the #defines for Input Buttons Pins. So this would help you to quickly identify certain regions as quickly as possible. you have seen that the <editor ... > statement.

Figure 2. unfolding

How to write Folding Comments:

     To write code in regions you need to write following statement at the top of code which you want to place it in region. Your statement which would appear above folded code write it in comment(shown in bold).

// <editor-fold defaultstate="collapsed" desc="comment">

and at the end of regional code you need to have following statement.

// </editor-fold>

your code should be in between these statements which you want to collapse.

     Now you are thinking that this method is little awful so don't be upset there is a shortcut direct method of writing these statements. Now simply type fcom and then press tab key you will quickly get these statements given in figure 3 below.
     To remember this I decoded this instruction as fcom = f (folding) com (Comment). So when ever I want to put code in regions I use folding comments.

Figure 3. Folding Comment using fcom

     Hope that this would helped you to get rid of awful codes and you would start writing awesome codes like I am doing or even better than me. Remember these statements only working in MPLAB X IDE. For more details on Folding code Click here.

     If you learned something interesting please consider donating some amount so that I continue producing such interesting free content for young engineers thank you.

All the text and graphics contained on this page belongs to owner except otherwise mentioned. Other parties' trademarks and service marks that may be referred to herein are the property of their respective owners. Reproducing or distributing text and graphics on your own site is strictly not allowed without proper linking to original content and before publishing that you should ask for permission. 

© Copyright 2014 IbrahimLabs. All rights reserved.

Popular posts from this blog

Digital AC voltmeter Schematic + Code + Proteus Simulation

Graphical LCD interfacing with PIC16F877 Code + Schematic + Proteus Simulation