Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Programming style
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Vertical alignment === Some programmers consider it valuable to align similar elements vertically (as tabular, in columns), citing that it can make typo-generated bugs more obvious. For example, unaligned: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $replacement = array('foo', 'bar', 'baz', 'quux'); $value = 0; $anothervalue = 1; $yetanothervalue = 2; </syntaxhighlight> aligned: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $replacement = array('foo', 'bar', 'baz', 'quux'); $value = 0; $anothervalue = 1; $yetanothervalue = 2; </syntaxhighlight> Unlike the unaligned code, the aligned code implies that the search and replace values are related since they have corresponding elements. As there is one more value for search than replacement, if this is a bug, it is more likely to be spotted via visual inspection. Cited disadvantages of vertical alignment include: * Dependencies across lines which leads to maintenance load. For example, if a long column value is added that requires a wider column, then all lines of the table must be modified (to maintain the tabular form) which is a larger change which leads to more effort to review and to understand the change at a later date * Brittleness: if a programmer does not correctly format the table when making a change, the result is a visual mess that is harder to read than unaligned code. Simple refactoring operations, such as renaming, can break the formatting. * More effort to maintain which may discourage a programmer from making a beneficial change, such as improving the name of an identifier, because doing so would require significant formatting effort * Requirement to use a fixed-width fonts; not proportional fonts Maintaining alignment can be alleviated by a tool that provides support (i.e. for [[elastic tabstop]]s), although that creates a reliance on such tools. As an example, simple refactoring operations to rename "$replacement" to "$r" and "$anothervalue" to "$a" results in: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $r = array('foo', 'bar', 'baz', 'quux'); $value = 0; $a = 1; $yetanothervalue = 2; </syntaxhighlight> With unaligned formatting, these changes do not have such a dramatic, inconsistent or undesirable effect: <syntaxhighlight lang="php"> $search = array('a', 'b', 'c', 'd', 'e'); $r = array('foo', 'bar', 'baz', 'quux'); $value = 0; $a = 1; $yetanothervalue = 2; </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)