dynamicreport.com News Member Login Knowledge Base Site Map
Support Support Ticket Contact Us
Products Drag & Drop Objects Drag & Drop Calendar UNIGEN Web Reporting Code View
Testimonials Downloads
Order

Knowledge Base Print printer-friendly page.

Login

UNIGEN

ddobj

News



Article #1005: Eight Programming Practices

 

Many programming techniques are recurring. The eight shortcuts below are often excluded by programmers in their coding habit, but adopting them will add structure and reduce code during development.

  1. Nested Delimiter Test
    While reading a statement from left to right begin adding 1 to the current count everytime an open delimiter is encountered, and subtract 1 from the current total for a closing delimiter. A total of 0 at the last closing delimiter will indicate a complete grouping, and so the test is useful for eliminating syntax errors when program execution is essential. However the delimiter placements are still subject to logical error.
    if ($x > 0 && ($y || (3 - $x > (21 - 4) * 3) || (8 / ($a - $b))) && $h);
  2. Boolean Toggle
    Eliminate a conditional check during a toggle assignment. The value of the toggle will alternate between 0 and 1.
    int iToggle = 0;
    loop:
    {
      // code based on toggle state
      iToggle = 1 - iToggle;
    }
  3. Bit-Based Parameters
    The bitwise OR/AND set combined parameter bit values and check for specific parameters, respectively.
    1 byte: 0x80 = 8 parameters
    2 bytes: 0x8000 = 16 parameters
    The stuffing of each parameter into the allocated data type storage occupies one bit per parameter (flag). The maximum parameters are dependent on the data type allocation in bits.
    #define PARAM_ONE 0x01;
    #define PARAM_TWO 0x02;
    #define PARAM_THREE 0x04;
    // combination of parameters: 1 value
    #define PARAM_1_3 PARAM_ONE | PARAM_THREE;
    #define PARAM_ALL PARAM_ONE | PARAM_TWO | PARAM_THREE;
    UCHAR bParam = PARAM_ONE | PARAM_TWO;
    if (bParam & PARAM_TWO)
      // parameter 2 exists
    else if (!(bParam & PARAM_TWO))
      // parameter 2 is non-existent
    if ((bParam & PARAM_1_3) == PARAM_1_3)
    // check only for parameters 1 and 3 amongst any combination of parameters
  4. Nested Code
    Dynamic Web page programming introduces an intricate occurence: code within code. This is not uncommon amongst non-Internet related coding projects, but is a constant recurring matter during Web development when using scripted languages such as a combination of ASP or PHP to output JavaScript statements for the execution of HTML output. Usually between two and three different languages to various levels of nesting may be used within one statement of the highest level script language. Programmers who are not exposed to this frequent matter may often find great confusion when composing a complex statement. One tactic is to write each of the distinct language fragments of code and combine them. At that point it is necessary to correct the string delimiters and encoding as you work deeper into the different embedded languages from left to right. The following is a generic way of remembering your choice of delimiters. The option of single quotes and quotation marks provide some degree of freedom in your choice, but which will determine your choice of delimiters further down the levels of nested languages.
    4 nested fragments of code over 3 different languages: PHP-HTML-JS-HTML

    echo "<a href = '#' onclick = 'javascript: js_call (\"<font size = \\\"1\\\">\");'>link</a>\n";

    General rule when applied to nested languages:
    Level 1 Language:
     Quotation for outer-most language output ["] (PHP)
    Level 2 Language:
     Single quote for output of single quote ['] specifying level 2 parameter (HTML)
    Level 3 Language:
     Escaped quotation [\"] for output of quotation specifying level 3 parameter (JS)
    Level 4 Language:
     Double escaped quotation for output of quotation [\\\"] specifying level 4 parameter (HTML within JS)
  5. Hungarian Notation
    The Hungarian notation refers to the naming convention of variables based on their data-type. The variable naming convention is used by some C/C++ programmers as a preference. The identifying lower-case data-type acronym begins a variable name and is followed by a brief descriptive name describing the variable's purpose. The beginning letter of a word separation is in upper-case form to improve legibility.
    (e.g. int iIndex; char cSymbol; unsigned int dwParams; double dAmount;)
  6. Condensed Conditional Values
    Code format is an art, and indentation is one piece of good legibility. The other is condensing code in sections of source code that seem suitable. In such cases a conditional variable assignment can be made using the short-form composition:
    e.g.
    SIMPLE: variable = (condition ? true value : false value);
    COMPLEX: variable = (condition1 ? true value1 : (sub-condition2 ? true sub-value2 : false sub-value2));
    COMPLEX: iAmt = (iVal >= 20 ? iVal : (iVal >= 10 ? iVal - 10 : -1));

    The question mark is equivalent to saying "if the condition to the left evaluates to true then assign the true value to the right," whereas the semicolon is equivalent to saying "if the condition evaluates to false then assign the false value to the right." This condensed format can be complex with many levels of nested short-form conditions. More complex conditions are probably better left to the expanded form if-else statements. Note that this form is derived from the C language, and occurs in many object-oriented languages including: C++, JavaScript, PHP, etc.
  7. Reduce Complexity (Big O Notation)
    Whenever possible reduce any combination of the: quantity of loops, loop iterations or nested loops for an increase in performance of program execution. Build an index during initialization, use sophisticated traversing algorithms that implement specific operations, track unnecessary conditional checks in a "map" and reference it as a basis for avoiding execution of unnecessary and time-consuming code within loop structures.
  8. Generic Structure
    Separate the general from the specific. Generalize as much of a task as possible and group the common logic and data into a separate module. Specifics should be grouped into another separate module that requires use of the general module. Re-use the generic modules (classes)! This is obviously known as object oriented programming (OOP), but such techniques can be emulated using structured languages with the implementation of external source files and data structures in C and other languages (modules rather than classes).

 

 


Back to article listing

 



 

Copyright © 2007-2008 Interaxis. All rights reserved.
Contact Webmaster