Code alignment 代码对齐改进(VS2017)

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.

作者:【唐】三三

出处:https://www.cnblogs.com/tangge/p/8116699.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   【唐】三三  阅读(1714)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-12-26 (4)WebApi 跨域问题
2016-12-26 (2)WebAPI的增删改查
2016-12-26 (3)WebApi客户端调用
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示