Manual talk:Coding conventions

Jump to navigation Jump to search

About this board

For discussion about PHP coding conventions prior to August 2020, see Manual talk:Coding conventions/PHP/Archive.

Enforce quote styles in PHP

3
Tacsipacsi (talkcontribs)
Arlolra (talkcontribs)
Krinkle (talkcontribs)

I don't think this should be enforced in code review, PHPCS, or otherwise in CI. We have a general guideline to follow, but I don't see it helping us to require code to change back and forth between two formats on a regular basis just due to the addition or removal of a character. Local consistently and productivity are I think more important factors.

When all else is equal, e.g. writing new code, or most strings in a method or file are equally simple, then our convention says to default to single quotes. Beyond that I don't think it's worth worrying about. In my opinion, string quoting isn't the sort of thing where cognitive overhead is induced by having variants or where it adds friction during review/auditing/debugging. You are goint to encounter both variants no matter what.

In my view, the guideline mainly exists as a tie-breaker when there's uncertaintity so that nobody has to discuss which one to use if they're not sure. But when modifying existing code, or once code has been put up for review, it's imho not worth changing or discussing further.

Reply to "Enforce quote styles in PHP"

i do not understand text about vertical alignment

2
Qdinar (talkcontribs)

the width allowed for the left column constantly has to be increased as more items are added

which items are added? new columns added? what is "width allowed for the left column"? i think it is tab size. then, if you make longer strings in any column, not only left column, then tab size has to be increased, or, more tabs has to be used between columns, and, if there are elements of much different size, different count of tabs need to be used between columns.

Tgr (WMF) (talkcontribs)

The point is, you shouldn't have to change spacing in ten lines just because you replaced a name on one line. It makes for more confusing diffs. So we prefer

[
    'foo' => 0,
    'foobar' => 1,
]

over

[
    'foo'    => 0,
    'foobar' => 1,
]

(except for Puppet, apparently).

Reply to "i do not understand text about vertical alignment"
Nikerabbit (talkcontribs)

The example safe command no longer works as of version 2.0 which removed --disable and --enable command line flags: https://github.com/svg/svgo/releases/tag/v2.0.0


In addition the other config file examples do not show hot run it for a single file.

Jdforrester (WMF) (talkcontribs)
Jdforrester (WMF) (talkcontribs)

Oh, fun. I'll update the docs to say to use SVGO v1.x for now.

Reply to "svgo example broken"
אלישיב ליפא (talkcontribs)

In the section "vertical alignment" there some mistake - I think some template was misused - "Template:English\note" is in the end of the first line, instead of a note saying that some diff engines can ignore white space changes.

Krinkle (talkcontribs)

I've proposed at T253461 that we phase out our AtEase library in favour of PHP's regular error suppression methods.

Based on the feedback gathered on the task so far, I think it's preferred that we continue to generally discourage use of error suppression (regardless of whether through @, AtEase, or error_reporting).

As such, I proposed that we keep the conventions mostly as-is, including that our PHPCS rule warns against use of the @-operator. What would change is that we'd use that same PHPCS rule as our way of opt-ing to it as-needed.

Currently:

use Wikimedia\AtEase\AtEase;

AtEase::suppressWarnings();
$content = file_get_contents( $path );
AtEase::restoreWarnings();

Now:

// phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
$content = @file_get_contents( $path );
Krinkle (talkcontribs)
RobinHood70 (talkcontribs)

The latest formatting recommendation from the PSR-12 is to always use 4 spaces to indent. While I'm a fan of tabs myself, we should consider whether we want to change the recommendation on the PHP subpage to match the PSR-12.

Krinkle (talkcontribs)

No. There is no meaningful interoperable benefit from which way to use spacing. Since the early 21st century, humanity has decided to indefinitely leave this question unanswered by forever more supporting both in the abstract. This is solved through EditorConfig and PHPCS. I don't expect contributors or text editor software to benefit from this switch, and more generally we don't use the rest of PSR coding style either. I'm boldly closing this as this would very likely otherwise become a distraction and waste of time, so I'm raising the bar a little by requiring the next person to re-open this if they feel compelled to convince others to spend time on this.

Dedicated pages per programming language

1
Krinkle (talkcontribs)

I've generalized this page to be a code conventions portal. Branching off specific languages to other dedicated pages. The "do as PHP unless stated otherwise" is getting old because JavaScript, for instance, is simply very different than PHP. And although the end result in syntax may be similar at times, the reasoning behind is very different. Therefor it's better to describe it in the right context with examples that make sense.

See also the Restructure MediaWiki.org-thread on Project:Current issues.

קיפודנחש (talkcontribs)

i think it is sorely missing. User:Anomie ? or does it exist somewhere else?

peace.

Anomie (talkcontribs)

There isn't one, as far as I know. The convention generally follows mw:CC, and some bits extrapolated from mw:CC/PHP and mw:CC/JS (e.g. lowerCamelCase function names).

I wouldn't be opposed to a Manual:Coding conventions/Lua, if there's stuff that needs to go in it.

What about carriage returns between sections of code?

2
Peachey88 (Flood) (talkcontribs)

When is it good to include blank lines? I usually include a blank line between functions, classes, etc. But is it also good style to include blank lines between, say, major code sections within a function? That leads to the question of what counts as a major section of code within a function; I find it to be a pretty arbitrary/subjective decision, and ultimately having a lot of blank lines just hinders the reader from seeing very much of the code without scrolling.

This post was posted by Peachey88 (Flood), but signed as Tisane.

Peachey88 (Flood) (talkcontribs)

Pretty much as you say: they're good, but should be used sparingly; it's much easier to say "apply common sense" than to try and legislate for it.

This post was posted by Peachey88 (Flood), but signed as Happy-melon.

Deprecate private variables and methods?

4
Peachey88 (Flood) (talkcontribs)
Peachey88 (Flood) (talkcontribs)

We're actually trying to use them in new code because they help in separating abstractions and give us better better control over proper interfaces to access data. You can't force people to uses accessor methods if they still can access class variables directly. Unfortunately, due to PHP's low entry requirements, many PHP programmers know nothing about proper OOP/OOD and produce such opuses instead.

This post was posted by Peachey88 (Flood), but signed as MaxSem.

Peachey88 (Flood) (talkcontribs)

Protected seems preferable, for extensibility purposes.

This post was posted by Peachey88 (Flood), but signed as Tisane.

Peachey88 (Flood) (talkcontribs)

It depends. Public, private and protected all have their places. Some times you want something private because you /don't/ want subclasses to be able to change the implementation.

This post was posted by Peachey88 (Flood), but signed as ^demon.