In mathematics you always keep your equals lined up directly underneath the one above. It keeps it clean and lets you know you're working on the same problem, for example:

y = 2x

y/2 = x

Programming is slightly different. We often have a lot of assignments underneath each other, and while they are not strictly the same as maths, there is a close relationship. As such, aligning the equals allows us to quickly spot the relationship.

Further, it makes your code so much more readable. Without alignment, code is like opening a CSV file in Notepad. But, if you open the CSV file in Excel, it becomes so much easier to read since the columns have meaning.

Compare these:

  person.FirstName = "Chris";                =>  person.FirstName  = "Chris";
person.Surname = "McGrath"; => person.Surname = "McGrath";
person.Age = 24; => person.Age = 24;
person.Occupation = "Software Developer"; => person.Occupation = "Software Developer";
person.HomeTown = "Brisbane"; => person.HomeTown = "Brisbane";

I question the sanity of anyone who thinks the first looks better or is easier to understand.

The Code alignment extension allows you to align by more than just the equals. As you start to see the benefits of alignment, you see that there is so much more to align by:

  // Ugly                 // An improvement        // Even better!
chris.Age = 25; => chris.Age = 25; => chris .Age = 25;
dan.Age = 23; => dan.Age = 23; => dan .Age = 23;
michael.Age = 27; => michael.Age = 27; => michael .Age = 27;
jennifer.Age = 22; => jennifer.Age = 22; => jennifer.Age = 22;

By aligning by the dot we can clearly see that we are setting the same property on each variable, and the thing that changes is the variable name.

This might seem a bit crazy now, but once you start aligning things, it's addictive.

Some more examples

  private string m_firstName = string.Empty;   =>  private string  m_firstName = string.Empty;
private string m_surname = string.Empty; => private string m_surname = string.Empty;
private int m_age = 18; => private int m_age = 18;
private Address m_address; => private Address m_address; public string FirstName { get; set; } => public string FirstName { get; set; }
public string Surname { get; set; } => public string Surname { get; set; }
public int Age { get; private set; } => public int Age { get; private set; }
private Address Address { get; set; } => private Address Address { get; set; } Assert.AreEqual("expected", person.Name); => Assert.AreEqual ("expected", person.Name);
Assert.AreEqual(21, person.Age); => Assert.AreEqual (21, person.Age);
Assert.AreNotEqual(other, person); => Assert.AreNotEqual(other, person); switch (state) => switch (state)
{ => {
case State.QLD: city = "Brisbane"; break; => case State.QLD : city = "Brisbane"; break;
case State.WA: city = "Perth"; break; => case State.WA : city = "Perth"; break;
case State.NSW: city = "Sydney"; break; => case State.NSW : city = "Sydney"; break;
default: city = "???"; break; => default : city = "???"; break;
} => }

It's surprising how few developers align their code.

Code alignment 代码对齐改进(VS2017)的更多相关文章

  1. vs code代码对齐快捷键

    vscode缩进快捷键: 选中文本: Ctrl  +  [      和   Ctrl  +  ]     实现文本的向左移动或者向右移动: vscode代码对齐快捷键: 选中文本: Shift  + ...

  2. vs快捷键代码格式化或代码对齐名字

    开发人员,换个电脑后环境要重装,vs的环境也需要重新设置. 快捷键需要重新设置,插件也需要重装,在这里备注下,换个环境就可以直接用了. 由于vs不同版本,代码对齐或者代码格式化的快捷键都不一样,所以导 ...

  3. multiple-cursors做代码对齐

    multiple-cursors做代码对齐 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839 ...

  4. Code Snippets 代码片段

    Code Snippets 代码片段       1.Title : 代码片段的标题 2.Summary : 代码片段的描述文字 3.Platform : 可以使用代码片段的平台,有IOS/OS X/ ...

  5. Effective Java提升Code Coverage代码涵盖率 - 就是爱Java

    虽然我们已经有了测试程序,但是如何得知是否已完整测试了主程序?,透过Code Coverage代码涵盖率,我们可以快速地得知,目前系统中,有多少程序中被测试过,不考虑成本跟投资效益比,涵盖率越高,代表 ...

  6. 自动生成Code First代码

    自动生成Code First代码 在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Fram ...

  7. 第五次作业2、请将该code进行代码重构,使之模块化,并易于阅读和维护;

    1.请运行下面code,指出其功能: (需附运行结果截图,并用简短文字描述其功能) 显示了人的姓名.年龄 2.请将该code进行代码重构,使之模块化,并易于阅读和维护: 3.观看视频The Exper ...

  8. Code::Blocks代码自动提示设置及常用快捷键

    Code::Blocks代码自动提示设置及常用快捷键(适用windows和linux) 1)以下需要设置的地方均在Settings->Editor...弹出的对话框中. 2)不少命令都可针对当前 ...

  9. VS中代码对齐等快捷键

    在VS2008中,选定代码后,按Ctrl+K+F组合键,可以自动进行代码对齐. 注意:要先按下Ctrl和K,再按下F,因为Ctrl+F是查找的快捷键. 也可以先按下Ctrl+K,再按下Ctrl+F. ...

随机推荐

  1. HDU - 4901 The Romantic Hero(dp)

    https://vjudge.net/problem/HDU-4901 题意 给n个数,构造两个集合,使第一个集合的异或和等于第二个集合的相与和,且要求第一个集合的元素下标都小于第二个集合的元素下标. ...

  2. datetimepicker bootstrap的时间插件显示位置问题及其他配置

    位置问题: 在bootstrap中用 datetimepicker 时默认是在输入框下面弹出的, 但是遇到输入框在屏幕最下面时,日期选择框会有一部分在屏幕下面,显示不了,因此需要能够从上面弹出. 可以 ...

  3. 阿里云服务器安装SQLServer本地无法远程访问

    新买的阿里云服务器,安装上sqlserver2012,本机连接测试没有问题,但是回到本地,使用ip远程连接报错. 尝试了网上各种办法,都是失败.最后找到原因,原来在阿里云的控制台上有设置: 首先进入安 ...

  4. js 分页问题

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. luogu P3960 列队

    传送门 因为\(Splay\)可以\(O(logn)\)维护区间,所以直接对每一行维护第一个元素到倒数第二个元素的\(Splay\),最后一列维护一个\(Splay\),每次把选出来的点删掉,然后把那 ...

  6. POJ1751 Highways【最小生成树】

    题意: 给你N个城市的坐标,城市之间存在公路,但是由于其中一些道路损坏了,需要维修,维修的费用与公路长成正比(公路是直的). 但现有M条公路是完整的,不需要维修,下面有M行,表示不需要维修的道路两端的 ...

  7. 手动创建binary log files和手动编辑binary log index file会有什么影响

    基本环境:官方社区版MySQL 5.7.19 一.了解Binary Log结构 1.1.High-Level Binary Log Structure and Contents • Binlog包括b ...

  8. windows钩子函数

    一 什么时候用到钩子?(when)Windows操作系统是建立在事件驱动的消息处理机制之上,系统各部分之间的沟通也都是通过消息的相互传递而实现的.通常情况下,应用程序只能处理当前进程的消息,如果需要对 ...

  9. python - class类(归一化设计)

    归一化设计 #继承同时具有两种含义 # 1.继承基类的方法,并且做出自己的改变或者扩展 # 2.声明某个子类兼容于某个基类,定义一个接口类,子类继承接口类,并且实现接口中定义的方法. # 实践中,继承 ...

  10. SSM框架中将时间写入数据库的格式定义

    //声明Date类接收的数据格式 @DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss") private Date addtime;