BozoSort

yet another CS blog

Browsing Posts published in December, 2009

Proper, consistent indentation of source code is a crucial component to keep from going insane when staring at lines of text for hours on end. Most people adhere to a single favored style, be it K&R, ANSI, GNU, or whatever*, which for the most part is acceptable because they aren’t all that different from one another, and most modern IDE’s are capable of understanding different coding styles and adapting auto indentation settings to the wants of the programmer. However, there is also a tool included (or at least available for) *nix systems, called indent which can actually manipulate C/C++ source code to modify it’s indentation style.

Take for example this extremely lazy solution to projecteuler.net problem #3 It has many levels of indentation, comments, and is nice and short, which will help emphasize the major differences between coding styles in my examples. My personal favorite coding style follows the ANSI standard, which is good for distinguishing code blocks, but takes up a lot more vertical space than other styles.

//* Euler Three */
#include <iostream>
using std::cout;
using std::endl;

#include <cmath>
using std::sqrt;

bool isPrime(unsigned long long);

int main()
{
  unsigned long long NUM = 600851475143; // as provided by projecteuler   problem #3
  for (unsigned long long i = NUM / 2; i >= 2; i--)
    {
    // The following for-loop is going to do something relavent
    for (unsigned long long t = i; t >= 2; t--)
    {
      if ((t * i) == NUM)
      {
        if (isPrime(t))
        {
            cout << "PFF: " << t << endl;
         }
       }
     }
   }
  return 0;
}

bool isPrime(unsigned long long n)
{
  bool prime = true;
  for (unsigned long long i = 3; i <= sqrt(n); i += 2)
  {
    if (n % i == 0)
    {
      prime = false;
      break;
    }
  }
  if ((n % 2 != 0 && prime && n > 2) || n == 2)
  {
    return true;
  }
  else
  {
    return false;
   }
}



Now, let’s use indent to change to the more common K&R style:
$ indent -kr Three.cpp


The resulting source file:

/* Euler Three */

#include <iostream>
using std::cout;
using std::endl;

#include <cmath>
using std::sqrt;

bool isPrime(unsigned long long);

int main()
{

    unsigned long long NUM = 600851475143;	// as provided by projecteuler problem #3
    for (unsigned long long i = NUM / 2; i >= 2; i--) {
	// The following for-loop is going to do something relavent
	for (unsigned long long t = i; t >= 2; t--) {
	    if ((t * i) == NUM) {
		if (isPrime(t)) {
		    cout << "PFF: " << t << endl;
		}
	    }
	}
}
return 0;
}

/*
	The follwing function was copied and pasted from somewhere on the internets
	and should probably have some sort of credit to the author or something
*/
bool isPrime(unsigned long long n)
{
    bool prime = true;

    for (unsigned long long i = 3; i <= sqrt(n); i += 2) {
	if (n % i == 0) {
	    prime = false;
	    break;
	}
    }
    if ((n % 2 != 0 && prime && n > 2) || n == 2) {
	return true;
    } else {
	return false;
    }
}

The style is changed, and all I had to do was invoke a command with a single switch and the file name as the parameter. There are other switches available, of course, including:
-bad (force blank lines after declarations)
-bap (force blank lines after function bodies)
-bl (format according to the Pascal standard)
-bf (forces braces on if statements)
-bls (put braces on the line after struct declaration lines)
-kr (format to the Kernighan & Ritchie coding style)
-orig (format to Berkeley coding style)
-st (send to standard output )
-bbo (break after boolean operators)
-bc (break after commas)
… and a bazillion more options, all available in the man pages :p

* for more reading on indentation styles, check out the wiki entry

Wordpress 2.8.5 and 2.8.6 as packaged with Ubuntu are broken. Using the admin pages gives the error:
Warning: constant() [function.constant]: Couldn't find constant WP_CORE_UPDATE in /usr/share/wordpress/wp-admin/includes/update.php on line 33

A bug report addressing* this problem can be found here.

I have a simple/ghetto workaround for this issue. Open:
/usr/share/wordpress/wp-admin/includes/update.php
and comment out the two lines:
if ( !constant('WP_CORE_UPDATE') )
return false;

!! You will need to edit this file with root permissions. If you are running a full desktop, you can do:
sudo gedit /usr/share/wordpress/wp-admin/includes/update.php &

If you are running a headless server as I am, you can open the file for editing using vim, nano, emacs, etc. ie:
sudo nano /usr/share/wordpress/wp-admin/includes/update.php

The error will no longer appear, and the blog will continue to function 100%. There are absolutely better ways to deal with this issue, but that is up for the Ubuntu package maintainers to .. maintain.

The following screen-shot shows exactly where to add comment notation:

update.php

* “addressing” might be too strong of a word considering the progress being made on this issue. Two versions of wordpress to go through the package maintainers and it’s still broken? In fact, the report is still considered “New”.

UPDATE 12-24-2009
Turns out the Wordpress 2.9-1 update still has this issue…

Wordpress Update 2.9-1 still broken

WordPress Appliance - Powered by TurnKey Linux