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. CentOS6.x下,tomcat - web项目部署

    1. 安装tomcat tomcat安装方法:http://www.cnblogs.com/vurtne-lu/p/6478440.html 2. 配置tomcat 修改server.xml文件 &l ...

  2. 数据库中DQL、DML、DDL、DCL的概念与区别

    SQL(Structure Query Language)语言是数据库的核心语言. SQL语言共分为四大类:数据定义语言DDL,数据操纵语言DML,数据查询语言DQL,数据控制语言DCL. 1. 数据 ...

  3. 一个简单的"RPC框架"代码分析

    0,服务接口定义---Echo.java /* * 定义了服务器提供的服务类型 */ public interface Echo { public String echo(String string) ...

  4. 编写优秀jQuery插件技巧

    1. 把你的代码全部放在闭包里面 这是我用的最多的一条.但是有时候在闭包外面的方法会不能调用. 不过你的插件的代码只为你自己的插件服务,所以不存在这个问题,你可以把所有的代码都放在闭包里面. 而方法可 ...

  5. buildroot构建项目(三)--- u-boot 2017.11 适配开发板修改 1

    当前虽然编译成功了,但是对于我们自己的目标板并不太适用.还得做一系列得修改. 一.lds 文件分析 u-boot 中最重要得链接文件即是,u-boot.lds.我们可以查看我们编译出来得 u-boot ...

  6. 20155313 2016-2017-2 《Java程序设计》第五周学习总结

    20155313 2016-2017-2 <Java程序设计>第五周学习总结 教材内容学习 第八章 异常处理 程序中总有些意想不到的状况所引发的错误,Java中的错误也以对象方式呈现为ja ...

  7. C++ 中 #ifndef, #define, #endif 宏定义

    目的:为了保证包含的内容只被程序(include) 和编译了一次.判断预处理器常量是否已被定义. 预编译将所有头文件(#include"XXX.h")用头文件中的内容来替换,头文件 ...

  8. 【SVN】svn使用方法

    下载安装TortoiseSVN 下载地方 安装成功后 TortoiseSVN清除凭证 右击空白处-TortoiseSVN-Settings打开Settings窗口后做如下操作: svn在idea中的使 ...

  9. TIdHTTP get参数带中文解决方法--请求报文

    Post 看起来稍微复杂先,暂不讨论.post 目前按照一般方法有中文名也可以. 拼接时:pointname=九记餐厅&begintime=2017-03-01 00:00:00& 有 ...

  10. bootstrap-table前端修改数据

    使用bootstrap-table显示数据,后台传回数据以后,可能需要对其做调整,如需要前端为数据添加单位 调整数据代码 $("#"+tableId).bootstrapTable ...