HAHAHAHA(HAx4)'s       




 Briefcase - comb, iComb and brace 9/11/2005

I once deemed myself as a code stylist 'cause then I was so bewitched with the aesthetical format of my code. However, in the past long while, under the pressure of hectic and erratic schedules, what I did was largely against my conviction of perfect code. In spite of all the wrongdoings, my "soft conscience" still led me to a few tools that I gleaned and tailored during my humble living. For download, please go to Ref[1] for detail.

The first two tools I call them "comb" and "iComb". When I learn the C language the first time, text books often presented code in the so called K & R style, like

if ( a != b ) {
... printf(" a!=b\n");
}

Conversely, there is another commonly used style (I believe there is an official name for it as apposed to K&R, but it slipped off my memory at this hour. That happens a lot lately on an old dog like me. ), more like

if ( a != b)
{
... printf(" a!=b\n");
}

The "comb" would tailor the code into my favorite K&R-like style. The "iComb" would turn the code into the style that is more close to the second template. As a matter of fact, I adapted these two tools from the renowned open-source tool "indent", but I changed the settings to fit my peculiarity. Needless to say, "comb" and "iComb" are also open-source. All the kudos to those made "indent" in the first place.

Speaking of "indent", I made another tool from it for the so called "brace law". I coined this phrase to refer to how braces are used on if/while/for statements even there is only one statement follows if/while/for.

e.g.
if ( a != b)
... printf(" a != b\n");

I prefer it to be as
if ( a != b) {
... printf(" a != b\n");
}

If brace law is enforced, you could use macro definition freely, and you don't need to worry about the inadvertent mistakes you or someone else may introduce when extra code is appended later on.

The usage of brace is as

brace fileName

And the line number of brace law violation will be listed out, such as

### brace rule violation, on line 117
### brace rule violation, on line 129

The brace can only take one file at a time. To go extra miles, make it handle the
whole directory, certain script may be necessary.

One way to do it is through Windows batch file. Actually, due to the limits of Windows' cmd shell, I have to make two batch files, working together to get the job done. (Maybe there is better way to do it with just one batch file)

The first batch file is pauBrace.bat, which simply provides the check the return status of brace.exe. When the file can not pass brace check smoothly, it will simply pause

[pauBrace.bat]

brace %~f1

@if %errorlevel% NEQ 0 pause

========== END OF FILE =============

The second file is dBrace.bat, which will traverse the whole directory and invoke pauBrace.bat for each file. That is the main batch file that should be called upon by user.

[dBrace.bat]

@setlocal

@cd /d %~f1

@if "prefix%~f1" == "prefix" @goto usage

@if /i "prefix%~2" == "prefix/r" for /R %%f in (.\*.c .\*.h) do
@pauBrace.bat %%f

@if not "prefix%~2" == "prefix/r" for %%f in (.\*.c .\*.h) do @pauBrace.bat %%f

@goto end

:usage

@echo usage: %~0 directoryName [/r]

:end

@endlocal

========== END OF FILE =============


I strongly believe the code style should be enforced in a certain way to confine those quirky, aberrant programmers who are full of themselves. My understanding is that the whole idea of code style enforcement is not about personal recognition or ostentation, but rather a matter of process and team work so that each and every one's contribution could be splice together seamlessly to pursue the same cause - make the whole project a successful one.

Speaking of process, I would like to wrap up by quoting words from Jack Ganssle's article (Ref[2]):

…Software artistes have missed the "process" boat. Most other industries use various sorts of defined processes to work efficiently. One way to identify an amateur organization of any sort, be they accountants, lawyers, craft shops, or software developers, is a lack of process. By contrast, an efficient company like McDonald's defines a rigorous way to do just about everything. Even a teenager, using Ray Kroc's process, can crank out Big Macs that taste exactly the same anywhere in the world...


References:
[1] HAHAHAHA's Briefcase
[2] Bailing Frantically, Jack Ganssle, Embedded Systems Programming, 09/11/03

    Posted by HAx4 at 11:38 PM

2 Comments:

  • On 10/07/2005 5:46 PM, Anonymous mas  Said

    Wow, that's a perfect tool. Could you expand it to check all our coding conventions? e.g. end each comment line with a period and never let a line exceed 80 characters?

    BTW. What about automatically correct the indentation stuff instead of giving errors? :)

     

  • On 10/08/2005 1:19 AM, Anonymous HAx4  Said

    These tools can not check name convention. Nor can they check the comments. (MS Word is a better tool to check punctuation.:-)

    However, the comb and iComb do automatically wrap your lines to 80 character boundary. They also do the indentation automatically. After you run the comb/iComb, you original file would be saved with a different ext name.

    By the way, the auto-wrap to limit line width can be achieved in many ways. Many editors could do that, or show the boundary with a solid vertical line.

    I have two other simple tools for line-width and tab elimination. See
    http://hax4.blogspot.com/2005/07/briefcase-tab.html
    http://hax4.blogspot.com/2005/06/briefcase-lindwidth.html

     

Post a Comment

<< Home  

   

 

 

Features

*) Grand Opening
*) Why HAHAHAHA
*) Stay Foolish
*) Rabbit Hole
*) Blotter
*) Notebook

Links

*) Blotter
*) Notebook
*) Rabbit Hole


*) I Love Rei
*) TEXT2PNG
*) China Blog List


*) Bertrand
*) Bruin
*) Bulldog
*) Hua
*) Mas
*) Nimrod
*) Philewar
*) Rodent
*) Samuel
*) Tom