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. Hive记录-Hive on Spark环境部署

    1.hive执行引擎 Hive默认使用MapReduce作为执行引擎,即Hive on mr.实际上,Hive还可以使用Tez和Spark作为其执行引擎,分别为Hive on Tez和Hive on ...

  2. Codeforces 590D Top Secret Task

    D. Top Secret Task time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  3. VMware虚拟机Mac OS X无法调整扩展硬盘大小的解决方案(转)

    使用VMware虚拟机搭建的MacOSX,在10.10以上可能会出现无法扩充磁盘大小的问题. 因为很多朋友在初次安装MacOSX的时候都默认选择40G的磁盘大小,结果用了没两天之后就发现磁盘不够用了. ...

  4. 有关Java内存溢出及内存消耗的小知识

    内存溢出原理: 我们知道,Java程序本身是不能直接在计算机上运行的,它需要依赖于硬件基础之上的操作系统和JVM(Java虚拟机). Java程序启动时JVM都会分配一个初始内存和最大内存给这个应用程 ...

  5. Mongodb 副本集

    mongodb主从模式就是一个 单副本的应用:没有很好的扩展性和容错性: 副本集的多个副本保证了容错性:主服务器负责整个副本集的读写,副本集定时同步数据:主节点挂掉:副本集会自动选举一个主的服务器: ...

  6. hadoop HA 配置 + zookeeper 服务注册

    环境测试 6台机器 centos6.7 x64 master ( namenode/cluster ) 10.10.100.101 namenode1 10.10.100.105 namenode2 ...

  7. luogu P3674 小清新人渣的本愿

    传送门 毒瘤lxl 本质是莫队,关键是怎么处理询问 这里需要开两个bitset(记为\(b1,b2\)),分别存\(x\)和\(n-x\)是否出现 对于询问1,即\(x-y=z\),由于\(y=x-z ...

  8. 前端mock数据的几种方式

    方式 备注 本地php服务架设 直接输出json 使用在线mock服务 如easyMock.apizza.Rap1\2 , 可以远程协作  本地node服务:koa+mongodb    本地node ...

  9. 【逆向工具】IDA使用5-( string、图形化与视图的切换、图形化显示反汇编地址、自动注释、标签使用)

    分析petya病毒时新学会的技巧. IDA技巧1 : string 提取文件中的字符串内容,如果看到一些文件字符串可以定位到关键的函数中. view -> open subview -> ...

  10. 【逆向工具】逆向工具101editor使用-游戏快速通关

    [渡者游戏简介] 船夫小江将运送客人的,羊.狐狸.草等物品过河,如果留下动物被其它种类吃掉任务就失败了.你需要帮助他做出正确的顺序选择.Ferryman是一款根据经典谜题改编的解谜游戏. 一.查看文件 ...