r72348 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72347‎ | r72348 | r72349 >
Date:03:43, 4 September 2010
Author:reedy
Status:ok (Comments)
Tags:
Comment:
Braces and spaces
Modified paths:
  • /trunk/phase3/includes/Html.php (modified) (history)
  • /trunk/phase3/includes/WatchlistEditor.php (modified) (history)
  • /trunk/phase3/includes/WebRequest.php (modified) (history)
  • /trunk/phase3/includes/WikiMap.php (modified) (history)
  • /trunk/phase3/includes/Xml.php (modified) (history)
  • /trunk/phase3/includes/ZhClient.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Xml.php
@@ -46,8 +46,9 @@
4747 if( is_null( $attribs ) ) {
4848 return null;
4949 } elseif( is_array( $attribs ) ) {
50 - foreach( $attribs as $name => $val )
 50+ foreach( $attribs as $name => $val ) {
5151 $out .= " {$name}=\"" . Sanitizer::encodeAttribute( $val ) . '"';
 52+ }
5253 return $out;
5354 } else {
5455 throw new MWException( 'Expected attribute array, got something else in ' . __METHOD__ );
@@ -133,10 +134,12 @@
134135 if( !is_null( $all ) )
135136 $namespaces = array( $all => wfMsg( 'namespacesall' ) ) + $namespaces;
136137 foreach( $namespaces as $index => $name ) {
137 - if( $index < NS_MAIN )
 138+ if( $index < NS_MAIN ) {
138139 continue;
139 - if( $index === 0 )
 140+ }
 141+ if( $index === 0 ) {
140142 $name = wfMsg( 'blanknamespace' );
 143+ }
141144 $options[] = self::option( $name, $index, $index === $selected );
142145 }
143146
@@ -749,9 +752,15 @@
750753 protected $attributes = array();
751754
752755 public function __construct( $name = false, $id = false, $default = false ) {
753 - if ( $name ) $this->setAttribute( 'name', $name );
754 - if ( $id ) $this->setAttribute( 'id', $id );
755 - if ( $default !== false ) $this->default = $default;
 756+ if ( $name ) {
 757+ $this->setAttribute( 'name', $name );
 758+ }
 759+ if ( $id ) {
 760+ $this->setAttribute( 'id', $id );
 761+ }
 762+ if ( $default !== false ) {
 763+ $this->default = $default;
 764+ }
756765 }
757766
758767 public function setDefault( $default ) {
Index: trunk/phase3/includes/WatchlistEditor.php
@@ -100,15 +100,17 @@
101101 $titles = array();
102102 if( !is_array( $list ) ) {
103103 $list = explode( "\n", trim( $list ) );
104 - if( !is_array( $list ) )
 104+ if( !is_array( $list ) ) {
105105 return array();
 106+ }
106107 }
107108 foreach( $list as $text ) {
108109 $text = trim( $text );
109110 if( strlen( $text ) > 0 ) {
110111 $title = Title::newFromText( $text );
111 - if( $title instanceof Title && $title->isWatchable() )
 112+ if( $title instanceof Title && $title->isWatchable() ) {
112113 $titles[] = $title->getPrefixedText();
 114+ }
113115 }
114116 }
115117 return array_unique( $titles );
@@ -129,8 +131,9 @@
130132 // Do a batch existence check
131133 $batch = new LinkBatch();
132134 foreach( $titles as $title ) {
133 - if( !$title instanceof Title )
 135+ if( !$title instanceof Title ) {
134136 $title = Title::newFromText( $title );
 137+ }
135138 if( $title instanceof Title ) {
136139 $batch->addObj( $title );
137140 $batch->addObj( $title->getTalkPage() );
@@ -140,8 +143,9 @@
141144 // Print out the list
142145 $output->addHTML( "<ul>\n" );
143146 foreach( $titles as $title ) {
144 - if( !$title instanceof Title )
 147+ if( !$title instanceof Title ) {
145148 $title = Title::newFromText( $title );
 149+ }
146150 if( $title instanceof Title ) {
147151 $output->addHTML( "<li>" . $skin->link( $title )
148152 . ' (' . $skin->link( $title->getTalkPage(), $talk ) . ")</li>\n" );
@@ -221,8 +225,9 @@
222226 $cache->addBadLinkObj( $title );
223227 }
224228 // Ignore non-talk
225 - if( !$title->isTalkPage() )
 229+ if( !$title->isTalkPage() ) {
226230 $titles[$row->wl_namespace][$row->wl_title] = $row->page_is_redirect;
 231+ }
227232 }
228233 }
229234 }
@@ -270,8 +275,9 @@
271276 $dbw = wfGetDB( DB_MASTER );
272277 $rows = array();
273278 foreach( $titles as $title ) {
274 - if( !$title instanceof Title )
 279+ if( !$title instanceof Title ) {
275280 $title = Title::newFromText( $title );
 281+ }
276282 if( $title instanceof Title ) {
277283 $rows[] = array(
278284 'wl_user' => $user->getId(),
@@ -302,8 +308,9 @@
303309 private function unwatchTitles( $titles, $user ) {
304310 $dbw = wfGetDB( DB_MASTER );
305311 foreach( $titles as $title ) {
306 - if( !$title instanceof Title )
 312+ if( !$title instanceof Title ) {
307313 $title = Title::newFromText( $title );
 314+ }
308315 if( $title instanceof Title ) {
309316 $dbw->delete(
310317 'watchlist',
@@ -410,8 +417,9 @@
411418 global $wgLang;
412419
413420 $link = $skin->link( $title );
414 - if( $redirect )
 421+ if( $redirect ) {
415422 $link = '<span class="watchlistredir">' . $link . '</span>';
 423+ }
416424 $tools[] = $skin->link( $title->getTalkPage(), wfMsgHtml( 'talkpagelinktext' ) );
417425 if( $title->exists() ) {
418426 $tools[] = $skin->link(
@@ -459,8 +467,9 @@
460468 $form .= Xml::openElement( 'textarea', array( 'id' => 'titles', 'name' => 'titles',
461469 'rows' => $wgUser->getIntOption( 'rows' ), 'cols' => $wgUser->getIntOption( 'cols' ) ) );
462470 $titles = $this->getWatchlist( $user );
463 - foreach( $titles as $title )
 471+ foreach( $titles as $title ) {
464472 $form .= htmlspecialchars( $title ) . "\n";
 473+ }
465474 $form .= '</textarea>';
466475 $form .= '<p>' . Xml::submitButton( wfMsg( 'watchlistedit-raw-submit' ) ) . '</p>';
467476 $form .= '</fieldset></form>';
Index: trunk/phase3/includes/ZhClient.php
@@ -35,7 +35,7 @@
3636 $errno = $errstr = '';
3737 $this->mFP = fsockopen($this->mHost, $this->mPort, $errno, $errstr, 30);
3838 wfRestoreWarnings();
39 - if(!$this->mFP) {
 39+ if ( !$this->mFP ) {
4040 return false;
4141 }
4242 return true;
@@ -47,8 +47,9 @@
4848 * @access private
4949 */
5050 function query($request) {
51 - if(!$this->mConnected)
 51+ if ( !$this->mConnected ) {
5252 return false;
 53+ }
5354
5455 fwrite($this->mFP, $request);
5556
@@ -68,8 +69,9 @@
6970 $data .= $str;
7071 }
7172 //data should be of length $len. otherwise something is wrong
72 - if(strlen($data) != $len)
 73+ if ( strlen($data) != $len ) {
7374 return false;
 75+ }
7476 return $data;
7577 }
7678
@@ -84,8 +86,9 @@
8587 $len = strlen($text);
8688 $q = "CONV $tolang $len\n$text";
8789 $result = $this->query($q);
88 - if(!$result)
 90+ if ( !$result ) {
8991 $result = $text;
 92+ }
9093 return $result;
9194 }
9295
@@ -99,8 +102,9 @@
100103 $len = strlen($text);
101104 $q = "CONV ALL $len\n$text";
102105 $result = $this->query($q);
103 - if(!$result)
 106+ if ( !$result ) {
104107 return false;
 108+ }
105109 list($infoline, $data) = explode('|', $result, 2);
106110 $info = explode(";", $infoline);
107111 $ret = array();
@@ -122,7 +126,7 @@
123127 $len = strlen($text);
124128 $q = "SEG $len\n$text";
125129 $result = $this->query($q);
126 - if(!$result) {// fallback to character based segmentation
 130+ if ( !$result ) {// fallback to character based segmentation
127131 $result = $this->segment($text);
128132 }
129133 return $result;
Index: trunk/phase3/includes/Html.php
@@ -571,10 +571,12 @@
572572 global $wgHtml5;
573573 $attribs['name'] = $name;
574574 if ( !$wgHtml5 ) {
575 - if ( !isset( $attribs['cols'] ) )
 575+ if ( !isset( $attribs['cols'] ) ) {
576576 $attribs['cols'] = "";
577 - if ( !isset( $attribs['rows'] ) )
 577+ }
 578+ if ( !isset( $attribs['rows'] ) ) {
578579 $attribs['rows'] = "";
 580+ }
579581 }
580582 return self::element( 'textarea', $attribs, $value );
581583 }
@@ -610,7 +612,9 @@
611613 }
612614 }
613615 $html = Html::openElement( 'html', $attribs );
614 - if ( $html ) $html .= "\n";
 616+ if ( $html ) {
 617+ $html .= "\n";
 618+ }
615619 $ret .= $html;
616620 return $ret;
617621 }
@@ -623,12 +627,12 @@
624628 */
625629 public static function isXmlMimeType( $mimetype ) {
626630 switch ( $mimetype ) {
627 - case 'text/xml':
628 - case 'application/xhtml+xml':
629 - case 'application/xml':
630 - return true;
631 - default:
632 - return false;
 631+ case 'text/xml':
 632+ case 'application/xhtml+xml':
 633+ case 'application/xml':
 634+ return true;
 635+ default:
 636+ return false;
633637 }
634638 }
635639 }
Index: trunk/phase3/includes/WebRequest.php
@@ -563,15 +563,23 @@
564564 global $wgUser;
565565
566566 $limit = $this->getInt( 'limit', 0 );
567 - if( $limit < 0 ) $limit = 0;
 567+ if( $limit < 0 ) {
 568+ $limit = 0;
 569+ }
568570 if( ( $limit == 0 ) && ( $optionname != '' ) ) {
569571 $limit = (int)$wgUser->getOption( $optionname );
570572 }
571 - if( $limit <= 0 ) $limit = $deflimit;
572 - if( $limit > 5000 ) $limit = 5000; # We have *some* limits...
 573+ if( $limit <= 0 ) {
 574+ $limit = $deflimit;
 575+ }
 576+ if( $limit > 5000 ) {
 577+ $limit = 5000; # We have *some* limits...
 578+ }
573579
574580 $offset = $this->getInt( 'offset', 0 );
575 - if( $offset < 0 ) $offset = 0;
 581+ if( $offset < 0 ) {
 582+ $offset = 0;
 583+ }
576584
577585 return array( $limit, $offset );
578586 }
@@ -686,8 +694,9 @@
687695 * @return Mixed
688696 */
689697 public function getSessionData( $key ) {
690 - if( !isset( $_SESSION[$key] ) )
 698+ if( !isset( $_SESSION[$key] ) ) {
691699 return null;
 700+ }
692701 return $_SESSION[$key];
693702 }
694703
@@ -939,13 +948,15 @@
940949 global $wgTitle;
941950 $basequery = '';
942951 foreach( $this->data as $var => $val ) {
943 - if ( $var == 'title' )
 952+ if ( $var == 'title' ) {
944953 continue;
945 - if ( is_array( $val ) )
 954+ }
 955+ if ( is_array( $val ) ) {
946956 /* This will happen given a request like
947957 * http://en.wikipedia.org/w/index.php?title[]=Special:Userlogin&returnto[]=Main_Page
948958 */
949959 continue;
 960+ }
950961 $basequery .= '&' . urlencode( $var ) . '=' . urlencode( $val );
951962 }
952963 $basequery .= '&' . $query;
Index: trunk/phase3/includes/WikiMap.php
@@ -68,12 +68,14 @@
6969 global $wgUser;
7070 $sk = $wgUser->getSkin();
7171
72 - if ( !$text )
 72+ if ( !$text ) {
7373 $text = $page;
 74+ }
7475
7576 $url = self::getForeignURL( $wikiID, $page );
76 - if ( $url === false )
 77+ if ( $url === false ) {
7778 return false;
 79+ }
7880
7981 return $sk->makeExternalLink( $url, $text );
8082 }
@@ -88,8 +90,9 @@
8991 public static function getForeignURL( $wikiID, $page ) {
9092 $wiki = WikiMap::getWiki( $wikiID );
9193
92 - if ( $wiki )
 94+ if ( $wiki ) {
9395 return $wiki->getUrl( $page );
 96+ }
9497
9598 return false;
9699 }

Comments

#Comment by Simetrical (talk | contribs)   20:18, 5 September 2010

I don't see anything listed at Manual:Coding conventions on proper indentation for switch statements. I don't necessarily object to that change (although the way I did it is the way vim likes to indent it, IIRC), but if we're going to impose that as standard, could you document it as such?

#Comment by Nikerabbit (talk | contribs)   05:51, 6 September 2010

I kind of prefer the version without extra indentation. Even though I use tabsize of two, the lines tend to get very close to the 80 char width.

#Comment by 😂 (talk | contribs)   11:23, 7 September 2010

Agreed, it should be documented. I personally prefer indenting the cases in a switch, but that's just me.

#Comment by 😂 (talk | contribs)   12:22, 9 September 2010

Back to the original question: the Internet seems divided on the issue. I tried Googling around, and people seem to have mixed feelings.

  • a K&R guide says to not indent the case and default.
  • w:Indent_style - silent on the issue entirely :(
  • astyle - interestingly has an --indent-switch parameter

And of course source code around the Internet is pretty mixed. Some indent, some don't. Nobody seems to care (nearly) as much about this as opposed to where to put the curly braces.

#Comment by Platonides (talk | contribs)   17:07, 25 September 2010

I also prefer indenting the cases.

#Comment by Reedy (talk | contribs)   05:59, 7 September 2010

Personally, I don't agree. Although, not always, I think the usage of braces indicates a level of indentation to be added (same with brackets if involving multiple lines

Granted, it makes no difference to the code, performance, it only changes "readability"

Is it worth throwing it out to the maiing lists, or what?

#Comment by MaxSem (talk | contribs)   10:59, 7 September 2010

I personally believe that forcing curve braces everywhere actually makes code less readable.

#Comment by X! (talk | contribs)   11:02, 7 September 2010

I agree too. In certain cases, last of curly braces increases readability.

if( isset( $params['foo1'] ) ) $foo1 = true; if( isset( $params['foo2'] ) ) $foo2 = true; if( isset( $params['foo3'] ) ) $foo3 = true; if( isset( $params['foo4'] ) ) $foo4 = true; if( isset( $params['foo5'] ) ) $foo5 = true; if( isset( $params['foo6'] ) ) $foo6 = true; if( isset( $params['foo7'] ) ) $foo7 = true; if( isset( $params['foo8'] ) ) $foo8 = true;

#Comment by 😂 (talk | contribs)   11:21, 7 September 2010

if you're going to do all that you might as well do

for( $i = 1; $i < 9; $i++ ) {
	$var = "foo$i";
	$$var = isset( $params[$var] );
}
#Comment by X! (talk | contribs)   18:35, 7 September 2010

That was intended as an example. In a real case, you have a unique name for each element that can't be identified by for loops.

#Comment by Simetrical (talk | contribs)   17:24, 8 September 2010

That code is extremely unreadable to start with. That level of manual repetition (even if you change the names around) indicates that you're doing something wrong. I don't think curly braces decrease readability at all in real-world cases, except where the code deserves to be rewritten anyway (lots of ifs in a row).

#Comment by X! (talk | contribs)   19:52, 8 September 2010

I personally think that

if( !isset( $foo ) ) $foo = 'foo'; if( !isset( $bar ) ) $bar = 'bar'; if( !isset( $baz ) ) $baz = 'baz';

is much more cleaner than

if( !isset( $foo ) ) {

   $foo = 'foo';

}

if( !isset( $bar ) ) {

   $bar = 'bar';

}

if( !isset( $baz ) ) {

   $baz = 'baz';

}

But I guess that's just me. (For the record, I forgot the code tags in the above example. I did add newlines, but they were lost.

#Comment by Simetrical (talk | contribs)   20:03, 8 September 2010

That style is only more readable if you have several in a row. If it's just a few sprinkled here and there, I find the one-line style much less readable, because I don't expect a statement to start so far into the line.

This is wikitext, mostly, so indent with a space to preserve formatting.

#Comment by X! (talk | contribs)   20:07, 8 September 2010

Agreed, for the occasional statement, it's fine. However, for huge blocks, I prefer the former.

#Comment by Reedy (talk | contribs)   21:46, 11 October 2010

Not that I really care either way now, does this need to stay fixme?

#Comment by Simetrical (talk | contribs)   00:44, 12 October 2010

The "fixme" is that someone should add something to Manual:Coding conventions about indentation in switch statements – at least saying "we have no preference". At that point it doesn't matter much if this stays or goes, but at least it will be clear that future cleanup commits shouldn't change indentation in switch statements.

#Comment by Krinkle (talk | contribs)   22:15, 3 February 2011

closing code-tag here (entire page was wrapped in it, bug in CR)

#Comment by Krinkle (talk | contribs)   22:16, 3 February 2011

closing another one

#Comment by Reedy (talk | contribs)   22:27, 3 February 2011

Status & tagging log