Archive

Posts Tagged ‘cost/benefit’

To if-else-if or if-if, that is the question

August 21st, 2009 Derek-Jones 4 comments

I am currently measuring if-statements, occurring in visible source, that might be mapped to an equivalent switch-statement. The most obvious usage to look for is a sequence of if-else-if statements that all involve the same expression being tested against an integer constant, as in

if (x == 1)
   stmt_1;
else
   if (x == 2)
      stmt_2;
   else
      if (x == 3)
         stmt_3;

Another possible sequence is:

if (x == 1)
   stmt_1;
if (x == 2)
   stmt_2;
if (x == 3)
   stmt_3;

provided all but the last conditionally executed arms do not change the value of the common control variable (e.g., x).

I started to wonder about what would cause a developer to chose one of these forms over the other. Perhaps the if-if form would be used when it was obvious that the common conditional variable was not modified in the conditionally executed arm. This would imply that there would be more statements in the arms of if-else-if sequences than if-if sequences. The following plot of percentage occurrence (over all detected if-else-if/if-if forms) of line number difference between pars of associated if-statements (e.g., when the controlling expression occurs on line x and the following if-statement controlling expression occurs on line x+2 the distance is 2) shows that this is not the case:

Lines between if-statement controlling expressions

Just over a quarter of the arms contain a single statement (or to be exact the code is contained on a single line); this suggests that when using the if-else-if form most developers put the else and if on the same line. At the next distance along the percentage of if-else-if forms is twice as great as the if-if, probably because of else and if appearing on separate lines (as in the introductory example) in one case and less frequently a comment/blank line in the other. Next along, why the big increase in if-if forms? A comment + blank line, or perhaps no comment or blank line but the use of curly brackets (this is too off the track of where I am supposed to be going to investigate).

This morning I realized why the original plot did not look right, one of the data sets was a way off adding to 100%. An updated version has been uploaded.

It turns out that a single statement (or at least a single line) is more common in the if-else-if form, the opposite of what I had expected. At slightly larger distances there are still differences that can be attributed to else and if appearing on separate lines, curly brackets and a comment/blank line, but the effect is not as large as seen in the original, less accurate, plot.

I have a feeling that I ought to say something about the if-else-if form being preferred to the if-if form. One of the forms will have its behavior changed if the common control variable is modified in one of its arms. But is this an intended or unintended behavior? What is the typical characteristic usage of a common control variable, e.g., do they tend to be accessed but not modified in a given function definition? At the moment I see no obvious cost or benefit strongly favoring one usage over the other, so I will remain silent on the issue.

Recommendations for teachers of programming

December 29th, 2008 Derek-Jones No comments

The software developers I deal with usually have at least 3 or more years professional experience in industry (I have not taught a beginners programming class in over 25 years) and these recommendations are based on what I tell these people. I like to think that they are applicable to teaching the3ose with a lot less experience.

First. Application domain expertise generally has greater value than programming expertise. That is, an investment of time in learning the application domain will yield a greater return on investment that learning more programming techniques.

Implications:

  • Minimize the amount of programming knowledge that your students need to know. For instance, there is little to be gained by teaching them the umpteen possible combination of looping constructs that can be written; in practice one or two account for 90% of all usage in industry. Teach the common cases and keep hammering on them until people use them without thought.
  • Criticize students for their failure to understand the application domain not coding constructs they use. For instance, use of loops when other techniques are available is not inefficient coding it is a failure to understand the application domain, anyway (if the application domain involves strings they should think string manipulation not looping over individual characters, if a domain uses vectors use vector arithmetic not loops over individual values, if a domain uses sets think sets manipulation), why are you even thinking about coding efficiency?
  • Set problems that enable students to build on previously learned programming skills to solve different application domain problems.
  • Second. Computer time is cheap, people time is expensive.

    Implications:

  • Code optimization is invariably an inefficient redistribution of time from computers to people. Writing efficient code is an advanced topic best left until after graduation and a year or two in industry. If performance is an issue concentrate on the algorithms.
  • Programming by fitting together chunks of code having a well understood (to the author) behavior can be a fast way of getting programs written (once students have a repertoire of the necessary chunks) and the fact that the code is not the most computer efficient is irrelevant.
  • Developers trying to optimize every bit of code they write is the single biggest cause of poor coding habits that I encounter and these habits tend to be so ingrained that it requires a huge effort to change them. Don’t warp peoples minds when they are at their most impressionable at the start learning to program, just because you were abused is not a reason for you to abuse others.
  • Third. Most software is written to solve an immediate problem, used a few times, and then forgotten about (I know of software projects costing in the millions where the code was never used; my own contribution to never used code is somewhat smaller, but still much larger than I would have liked). The point is that writing software involves a cost/benefit trade-off, how much should I invest in making code maintainable given the risk that the benefits from this investment may not be recouped.

    Implications:

  • If a program is only run a few time it does not really matter how fast it runs, ie, optimization at the code level is pointless and in many cases the algorithm used will not make a lot of difference.
  • A few brief comments specifying input/output, any assumptions made and revision dates (a few seconds typing can save an hour later trying to figure out which is the ‘latest’ version). Anything more is bureaucratic dead weight, usually inserted to keep management happy, rarely correct and a general time sink. Don’t put people off commenting because of the memories they have of the pointless comments their instructor made them write.
  • Fourth. Don’t fool yourself that you are teaching programming; you probably simply give a lecture or two and expect students to pick up the details from one of the assigned course books.

    Implications:

  • Students pick up lots of folklore that gets them through the course; work with this not against it. You are the chief wizard, give them a few powerful principles spells that can be applied to solve the problems they are likely to encounter. Understanding takes more time than is available, accept this fact.
  • Fifth. Coding style does exist and can have an effect on the cost of writing and maintaining code as well as how well a compiler can optimize it. These costs and benefits vary with the context in which the code was created and used. A good programmer will adopt a coding style appropriate to the context, just like an actor plays a role with the appropriate characterization (eg, comedy, thriller, suspense). It takes more experience than most undergraduate have to be able to properly discern what style is being used, let alone be capable of changing their own style.

    Implications:

  • Encouraging students to adopt some coding style is at best a waste of time and often causes people to thoughtlessly adopt inappropriate practices and becoming inflexible in their habits (to the point of ignoring further learning opportunities). Let students experiment and find a method of writing that works for them no matter how much it offends your sense of taste (they probably think the same about your dress sense as you do about their coding style and both issues are equally irrelevant).
  • Warn students that they are likely to encounter people proclaiming the one true way to writing great code and recommend that they ask these self proclaimed gurus for proof (ie, experimental studies published in peer reviewed journals) that using the proffered techniques will result in the claimed outcome.
  • FireStats icon Powered by FireStatscanada cheap propecia

    canada viagra

    buy propecia online pharmacy

    canada levitra

    once a day viagra

    cheap propecia 5mg

    buy daily cialis

    cheapest propecia prescription

    buy propecia no prescription

    generic cialis

    buy viagra mexico

    cialis discount

    cialis okay for women

    buy cialis once daily

    levitra vs cialis

    cheapest online propecia

    buy propecia on line

    cialis cost

    map of france with cialis

    cialis generic

    cialis canadian

    5 mg daily cialis

    buy now propecia

    generic levitra cialis

    buy cialis without a prescription

    cheapest price propecia cheap

    buying generic propecia

    online propecia sales

    bio viagra herbal

    cheapest cialis

    cheap levitra online

    discount brand name cialis

    healthcare canadian pharmacy

    generic viagra made in india

    50 mg cialis

    buy propecia uk

    cialis soft pills

    cialis by women

    buy real cialis

    levitra sale

    levitra cheapest

    cialis generic 100 mg

    best prices for propecia

    buy branded viagra

    cialis india pharmacy

    canadian online pharmacy cialis

    buy cheapest cialis

    cialis online

    buy cheap online propecia

    buy cialis for daily use

    best cialis price

    brand name cialis

    canadian healthcare viagra sales

    original levitra

    cialis medication

    best price for propecia

    best propecia prices

    levitra

    buy viagra online canada

    buying generic cialis mexico rx

    lowest price for levitra

    generic propecia 5mg

    cialis strenght mg

    can i order viagra from the chemist

    express viagra delivery

    buy levitra in europe

    cialis by mail

    cheapest propecia prescription

    best way to use cialis

    buy generic levitra online

    buy propecia international pharmacy

    how to get cialis in canada

    levitra 10mg

    cheap viagra from uk

    generic propecia india

    cialis woman

    cialis brand only

    generic viagra canadian

    buy cheap propecia

    cialis 100 mg

    buying viagra in canada

    canada generic propecia

    buy now online propecia

    ordering viagra overnight delivery

    buying viagra without prescription

    cialis cost

    levitra vs cialis

    levitra vs viagra

    buying cialis soft tabs 100 mg

    online generic cialis 100 mg

    buy cialis without prescription

    cialis alternative

    cialis tablets

    buying generic propecia

    cialis 30 mg

    bestellen levitra online

    cialis and ketoconazole

    levitra mail order

    best levitra prices

    cialis 30 mg

    canadian pharmacy cialis pfizer

    discount propecia propecia

    buy xenical propecia

    online levitra tablet

    cialis endurance

    cialis iop

    cialis cost

    cialis buy

    generic cialis canada

    buying online propecia

    buy discount viagra

    for sale levitra

    cialis express delivery

    cialis fast delivery

    order propecia

    cialis canada

    best viagra

    cialis and canada custom

    cheapest propecia sale uk

    buy levitra vardenafil

    herbal alternative to cialis

    ordering viagra overnight delivery

    cheap propecia order online

    buying viagra in canada

    discount brand name cialis

    cialis and diarrhea

    generic cialis india discount

    get levitra

    can i order viagra from the chemist

    buying real viagra without prescription

    hydrochlorothiazide cialis

    levitra canadian pharmacy

    generic propecia alternative

    cialis 50 mg dose

    buy cheap propecia online

    lowest propecia 1 mg

    buy viagra online cheap us

    order propecia propecia

    get levitra online

    lowest price on non generic levitra

    cheap fast levitra

    cost levitra low

    buying cialis without a prescription

    cheap propecia online

    levitra tablet

    cheap propecia online prescription

    cialis soft canada

    cheap viagra canada or india

    cialis india pharmacy

    generic levitra canada

    buy propecia where

    buy propecia online from usa pharmacy

    canada viagra pharmacies scam

    buying cialis in canada

    buy cialis next day delivery

    buy cialis cannada

    i need to buy propecia

    cheap levitra tablets

    canadian viagra india

    cialis levitra sales

    best online generic levitra

    buy viagra on line

    cialis iop

    baldness male propecia

    online pharmacy propecia renova

    low price propecia

    effect of cialis on women

    cialis fast delivery usa

    buy cialis on line

    generic viagra canadian

    cialis refractory

    how strong is 5 mg of cialis

    cialis alternative

    cialis cheap

    cialis 5 mg

    express viagra delivery

    combine cialis and levitra

    cheap viagra from uk

    buy cheap online propecia

    levitra online overnight delivery

    genuine cialis pills

    cheap propecia online

    cialis no rx

    cialis for women

    levitra online

    chip cialis

    buy cialis 5 mg

    levitra tabs

    herbal viagra

    buying online propecia

    cialis overnight

    cialis tablets vs viagra

    cheap levitra uk

    best price for generic cialis

    cheapest propecia pharmacy online

    cialis discounts

    online order viagra overnight delivery

    cialis 100 mg

    buy levitra online viagra

    buy cialis online without prescription

    buy propecia online

    bestellen levitra online

    cialis angioplasty

    levitra info

    cialis arterial fibrillation

    buy viagra online canada

    canadian pharmacy viagra legal

    generic viagra made in usa

    cialis dose

    generic levitra online

    how to get cialis

    cialis price

    cialis daily dosage pharmacy

    can i order viagra from the chemist

    online pharmacy levitra

    best price for propecia online

    cialis once daily

    buy cialis usa

    levitra in india

    brand viagra without prescription buy

    cialis philippines

    buy propecia online cheap pharmacy

    cialis discount

    buy generic viagra india rx

    canadian healthcare

    herbal viagra

    best levitra price

    buy viagra pills

    generic cialis india

    levitra viagra cialis

    cialis samples in canada

    cialis no prescription

    best price for generic cialis

    canadian women viagra

    buy viagra china

    how to buy cialis in canada

    buy online prescription propecia

    buy propecia pills

    buying cialis

    canadian viagra generic

    buy propecia canada

    canadian pharmacy

    online order propecia

    levitra order prescription

    cialis germany

    buy cheap levitra online

    buy levitra online viagra

    cialis vs levitra

    buy propecia online prescription

    how you get pfizer viagra