Jump to content

Indent styles


Hall of Famer

K&R or Allman?  

6 members have voted

  1. 1. K&R or Allman?

    • K&R
      2
    • Allman
      0
    • neither, I use a less common indent style and feel comfortable about it.
      4


Recommended Posts

As far as I know, two common indent styles exist in the programming world: K&R and Allman. Well not to be disrespectful for other indent styles, I am just talking about the two most widely used ones. For me, I've been coding with K&R styles throughout my life, and find it more comfortable personally. I do, however, realize that most PHP frameworks use Allman indenting style, good examples are Zend Framework, Symfony and Codeignitor. CakePHP is perhaps the only framework stick to K&R intent style.

 

I was wondering though, how many of you here are advocates for K&R or Allman style. And why? I was told before that it was just a matter of preference, like the color we choose for our shirts. But is it really so? Are there any other important reasons behind this preference? Is it just a coincidence that CakePHP is the only popular PHP framework using K&R? 

Link to comment
Share on other sites

But, I use tabs for indenting.

 

ewww.

 

I use tabs as well, not really sure why, but I like that I can change the tab size to be 2 "spaces" vs having to do 4 spaces all the time without much room for manipulating. Just my preference though.

Link to comment
Share on other sites

I kind of have my own style I guess.  From reading through the wiki-list it probably resembles 1TBS the most.  I put the opening brace after the expression (eg if ($blah){).  Having it on it's own line annoys me to no end.  I use tabs for an indent rather than spaces.  I rarely have any issues with this, but I can understand why spaces would be preferred in a large multi-coder environment.

 

If I end up needing to split something across multiple lines, I usually do something like this:

$a = sprintf("Welcome %s, your color is %s and your symbol is %s\r\n",
$name,
$color,
$symbole
);

or 

if (
    $a == $b 
    || ($c == $b && $x==$y
){
blah();
}

 

 

I generally don't care what style someone uses, so long as it is readable and not a big wild mess like some people post to the forums here.

 

Link to comment
Share on other sites

and not a big wild mess like some people post to the forums here.

 

class HTMLPurifier
{

/** Version of HTML Purifier */
public $version = '4.3.0';

/** Constant with version of HTML Purifier */
const VERSION = '4.3.0';

/** Global configuration object */
public $config;

/** Array of extra HTMLPurifier_Filter objects to run on HTML, for backwards compatibility */
private $filters = array();

/** Single instance of HTML Purifier */
private static $instance;

protected $strategy, $generator;

/**
* Resultant HTMLPurifier_Context of last run purification. Is an array
* of contexts if the last called method was purifyArray().
*/
public $context;

/**
* Initializes the purifier.
* @param $config Optional HTMLPurifier_Config object for all instances of
*                the purifier, if omitted, a default configuration is
*                supplied (which can be overridden on a per-use basis).
*                The parameter can also be any type that
*                HTMLPurifier_Config::create() supports.
*/
public function __construct($config = null) {

$this->config = HTMLPurifier_Config::create($config);

$this->strategy     = new HTMLPurifier_Strategy_Core();

}

/**
* Adds a filter to process the output. First come first serve
* @param $filter HTMLPurifier_Filter object
*/
public function addFilter($filter) {
trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom', E_USER_WARNING);
$this->filters[] = $filter;
}

/**
* Filters an HTML snippet/document to be XSS-free and standards-compliant.
*
* @param $html String of HTML to purify
* @param $config HTMLPurifier_Config object for this operation, if omitted,
*                defaults to the config object specified during this
*                object's construction. The parameter can also be any type
*                that HTMLPurifier_Config::create() supports.
* @return Purified HTML
*/
public function purify($html, $config = null) {

// :TODO: make the config merge in, instead of replace
$config = $config ? HTMLPurifier_Config::create($config) : $this->config;

// implementation is partially environment dependant, partially
// configuration dependant
$lexer = HTMLPurifier_Lexer::create($config);

$context = new HTMLPurifier_Context();

// setup HTML generator
$this->generator = new HTMLPurifier_Generator($config, $context);
$context->register('Generator', $this->generator);

// set up global context variables
if ($config->get('Core.CollectErrors')) {
// may get moved out if other facilities use it
$language_factory = HTMLPurifier_LanguageFactory::instance();
$language = $language_factory->create($config, $context);
$context->register('Locale', $language);

$error_collector = new HTMLPurifier_ErrorCollector($context);
$context->register('ErrorCollector', $error_collector);
}

// setup id_accumulator context, necessary due to the fact that
// AttrValidator can be called from many places
$id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
$context->register('IDAccumulator', $id_accumulator);

$html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context);

// setup filters
$filter_flags = $config->getBatch('Filter');
$custom_filters = $filter_flags['Custom'];
unset($filter_flags['Custom']);
$filters = array();
foreach ($filter_flags as $filter => $flag) {
if (!$flag) continue;
if (strpos($filter, '.') !== false) continue;
$class = "HTMLPurifier_Filter_$filter";
$filters[] = new $class;
}
foreach ($custom_filters as $filter) {
// maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat
$filters[] = $filter;
}
$filters = array_merge($filters, $this->filters);
// maybe prepare(), but later

for ($i = 0, $filter_size = count($filters); $i < $filter_size; $i++) {
$html = $filters[$i]->preFilter($html, $config, $context);
}

// purified HTML
$html =
$this->generator->generateFromTokens(
// list of tokens
$this->strategy->execute(
// list of un-purified tokens
$lexer->tokenizeHTML(
// un-purified HTML
$html, $config, $context
),
$config, $context
)
);

for ($i = $filter_size - 1; $i >= 0; $i--) {
$html = $filters[$i]->postFilter($html, $config, $context);
}

$html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context);
$this->context =& $context;
return $html;
}

/**
* Filters an array of HTML snippets
* @param $config Optional HTMLPurifier_Config object for this operation.
*                See HTMLPurifier::purify() for more details.
* @return Array of purified HTML
*/
public function purifyArray($array_of_html, $config = null) {
$context_array = array();
foreach ($array_of_html as $key => $html) {
$array_of_html[$key] = $this->purify($html, $config);
$context_array[$key] = $this->context;
}
$this->context = $context_array;
return $array_of_html;
}

/**
* Singleton for enforcing just one HTML Purifier in your system
* @param $prototype Optional prototype HTMLPurifier instance to
*                   overload singleton with, or HTMLPurifier_Config
*                   instance to configure the generated version with.
*/
public static function instance($prototype = null) {
if (!self::$instance || $prototype) {
if ($prototype instanceof HTMLPurifier) {
self::$instance = $prototype;
} elseif ($prototype) {
self::$instance = new HTMLPurifier($prototype);
} else {
self::$instance = new HTMLPurifier();
}
}
return self::$instance;
}

/**
* @note Backwards compatibility, see instance()
*/
public static function getInstance($prototype = null) {
return HTMLPurifier::instance($prototype);
}

}

 

What's wrong with that?

Link to comment
Share on other sites

Something very similar to K&R Variant: 1TBS, but with a few differences. For instance, having the opening bracket on the same line as the statement, for all statements. Even though it's function/class definitions, or whatever. Makes things more consistent, if you ask me. :)

Tabs all the way, set to 4 characters wide.

Link to comment
Share on other sites

a big wild mess like some people post to the forums here.

I swear to god this is what some of my coworkers do. They all have something else in common too. I should find out what editor they use. I spent 30 minutes once cleaning up some indenting (yes, I know a real IDE can do it for me, shut up.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.