Windows Phone 7的项目开始了,但公司没有相关的Coding Standards, 因此在国外网站上找了一份感觉比较靠谱的,以后就用这个了。翻译就没有必要了吧,程序员英文不好是很悲剧的,看不懂的好好学学英语吧,至少也要有cet6的阅读水平吧,不然学东西就累了。
Introduction
Believe it: majority of the programmers write "working code," but not "efficient code." As we mentioned in the beginning of this tutorial, do you want to become the "Most Valued Professional" of
your company? Writing "efficient code" is an art that you must learn and practice.
Naming Conventions andStandards
- Pascal casing: the first character of all words is upper case and the other characters are lower case.
- Camel casing: the first character of all words, except the first word, is upper case and other characters are lower case.
Use Pascal casing for class names:
public class HelloWorld
{
...
}
Use Pascal casing for method names:
public class HelloWorld
{
void SayHello(string name)
{
...
}
}
Use Camel casing for variables and method parameters:
public class HelloWorld
{
int totalCount = 0;
void SayHello(string name)
{
string fullMessage = "Hello " + name;
...
}
}
Do not use Hungarian notation to name variables. In earlier days, most programmers liked it: having the data type as a prefix for the variable name and usingm_
as
the prefix for member variables, e.g:
string m_sName;
int nAge;
However, in .NETcodingstandards,
this is not recommended. Usage of data type andM_
to represent member variables should not be done. All variables should use Camel casing. Use meaningful,
descriptive words to name variables:
- Do not use abbreviations. Use
name
,address
,salary
etc.
instead ofnam
,addr
,sal
.
- Do not use single character variable names like
i
,n
,x
,
etc. Use names likeindex
andtemp
.
One exception in this case would be variables used for iterations in loops:
for ( int i = 0; i < count; i++ )
{
...
}
If the variable is used only as a counter for iteration and is not used anywhere else in the loop, many people still like to use a single char variable (i
)
instead of inventing a different suitable name.
- Do not use underscores (_) in variable names.
- Namespace names should follow the standard pattern.
<company name>.<product name>.<top level module>.<bottom level module>
File name should match with class name. For example, for the classHelloWorld
, the file
name should behelloworld.cs(orhelloworld.vb).
Indentation and spacing: use TAB for indentation. Do not use spaces.
Comments should be in the same level as the code. Curly braces ( {} ) should be in the same level as the code outside the braces. Use one blank line to separate logical groups of code.
bool SayHello (string name)
{
string fullMessage = "Hello " + name;
DateTime currentTime = DateTime.Now;
string message = fullMessage + ", the time is : " +
currentTime.ToShortTimeString();
MessageBox.Show ( message );
if ( ... )
{
return false;
}
return true;
}
This code looks better than the code shown above:
bool SayHello ( string name )
{
string fullMessage = "Hello " + name;
DateTime currentTime = DateTime.Now;
string message = fullMessage + ", the time is : " +
currentTime.ToShortTimeString();
MessageBox.Show ( message );
if ( ... )
{
return false;
}
return true;
}
There should be one and only one single blank line between each method inside the class. The curly braces should be on a separate line and not in the same line asif
,for
,
etc.
Good:
if ( ... )
{
}
Not good:
if ( ... ) {
}
Use a single space before and after each operator and brackets.
Good:
if ( showResult == true )
{
for ( int i = 0; i < 10; i++ )
{
}
}
Not good:
if(showResult==true)
{
for(int i= 0;i<10;i++)
{
}
}
Good Programming Practices
Avoid having too-large files. If a file has more than 300-400 lines of code, you must consider refactoring the code into helper classes. Avoid writing very long methods. A method should typically
have 1-25 lines of code. If a method has more than 25 lines of code, you must consider refactoring it into separate methods. The method's name should tell what it does. Do not use misleading names. If the method name is obvious, there is no need of documentation
explaining what the method does.
Good:
void SavePhoneNumber ( string phoneNumber )
{
}
Not good:
void SaveData ( string phoneNumber )
{
}
A method should do only "one job." Do not combine more than one job in a single method, even if those jobs are very small.
Good:
SaveAddress ( address );
SendEmail ( address, email );
void SaveAddress ( string address )
{
}
void SendEmail ( string address, string email )
{
}
Not good:
SaveAddress ( address, email );
void SaveAddress ( string address, string email )
{
}
Use the C# or VB.NET specific types, rather than the alias types defined in theSystem
namespace.
Good:
int age;
string name;
object contactInfo;
Not good:
Int16 age;
String name;
Object contactInfo;
Do not hardcode numbers. Use constants instead. Do not hardcode strings. Use resource files. Avoid using many member variables. Declare local variables and pass them to methods instead of sharing
a member variable between methods. If you share a member variable between methods, it will be difficult to track which method changed the value and when. Useenum
wherever
required. Do not use numbers or strings to indicate discrete values.
Good:
enum MailType
{
Html,
PlainText,
Attachment
}
void SendMail (string message, MailType mailType)
{
switch ( mailType )
{
case MailType.Html:
break;
case MailType.PlainText:
break;
case MailType.Attachment:
break;
default:
break;
}
}
Not good:
void SendMail (string message, string mailType)
{
switch ( mailType )
{
case "Html":
break;
case "PlainText":
break;
case "Attachment":
break;
default:
break;
}
}
Do not make the member variablespublic
orprotected
.
Keep themprivate
and exposepublic
/protected
properties.
Never hardcode a path or drive name in code. Get the application path programmatically and use relative path. Never assume that your code will run from driveC:. You never know; some users may run it from a network or from aZ:.
In the application start-up, do some kind of "self check" and ensure that all required files and dependencies are available in the expected locations. Check for database connections at start-up,
if required. Give a friendly message to the user in case of any problems.
If the required configuration file is not found, the application should be able to create one with default values. If a wrong value is found in the configuration file, the application should throw
an error or give a message and also should tell the user what the correct values are.
Error messages should help the user to solve the problem. Never give error messages like "Error in Application," "There is an error," etc. Instead, give specific messages like "Failed to update
database. Please make sure the login ID and password are correct."
When displaying error messages, in addition to telling what is wrong, the message should also tell what the user should do to solve the problem. Instead of a message like "Failed to update the
database," suggest what the user should do: "Failed to update database. Please make sure the login ID and password are correct."
Show short and friendly messages to the user, but log the actual error with all possible information. This will help a lot in diagnosing problems.
Comments
Do not write comments for every line of code and every variable declared. Write comments wherever required. Good, readable code will require very few comments. If all variables and method names
are meaningful, that will make the code very readable and it will not need much commenting. Fewer lines of comments will make the code more elegant. However, if the code is not clean/readable and there are fewer comments, that is worse. If you have to use
some complex or weird logic for any reason, document it very well with sufficient comments. If you initialize a numeric variable to a special number other than 0, -1, etc., document the reason for choosing that value. The bottom line is: write clean, readable
code in such a way that it doesn't need any comments to understand it. Do a spell check on comments and also make sure that proper grammar and punctuation are used.
Exception Handling
Never do a "catch exception and do nothing." If you hide an exception, you will never know if the exception happened or not. In the case of exceptions, give a friendly message to the user, but
log the actual error with all possible details about the error, including the time it occurred, the method and class name, etc. Always catch only the specific exception, not generic exceptions.
Good:
void ReadFromFile ( string fileName )
{
try
{
}
catch (FileIOException ex)
{
throw;
}
}
Not good:
void ReadFromFile ( string fileName )
{
try
{
}
catch (Exception ex)
{
return "";
}
}
There's no need to catch the general exception in all your methods. Leave it open and let the application crash. This will help you find most of the errors during the development cycle. You can
have an application level (thread level) error handler where you can handle all general exceptions. In the case of an "unexpected general error," this error handler should catch the exception and should log the error, in addition to giving a friendly message
to the user before closing the application or allowing the user to "ignore and proceed."
Do not writetry
-catch
in
all your methods. Use it only if there is a possibility that a specific exception may occur. For example, if you are writing into a file, handle onlyFileIOException
.
Do not write very largetry
-catch
blocks.
If required, write a separatetry
-catch
for
each task you perform and enclose only the specific piece of code inside thetry
-catch
.
This will help you find which piece of code generated the exception and you can give specific error messages to the user.
You may write your own custom exception classes, if required, in your application. Do not derive your custom exceptions from the base classSystemException
.
Instead, inherit fromApplicationException
.
相关推荐
Windows+Phone+7+开发教程汇总,文本形式的,大家可以看下
《Windows+Phone+7高级编程》随书源代码,非常的详细,如果你想学Windows+Phone+7开发,但是只有电子书没有源码的话可以下来看看,如果买书了的话就没必要浪费积分下载了,这个资源有点大,我只能分两次上传了。
深入浅出Windows+Phone7开发 适合初学者
Windows+Phone+7+完美开发征程源代码
《Windows+Phone+7高级编程》随书源代码这是最后三章的源代码,大家可以下来参考下,本身这个太大了,一次没办法上传完,请谅解···
1、Windows Phone数据库开发指南 2、Windows Mobile上使用WINCE自带数据库 3、XML on Windows Mobile C# 4、关于如何在ppc模拟器中读取本地xml文件的办法 5、设置 Windows 窗体 DataGridView 控件中的数据格式 6、...
C# 做的计算器,Windows phone 7用的。包含进制转换。
关于window mobile开发介绍。常用设备以及相关任务开发编译和发布
这是一本通过实例来讲解Windows Phone 8技术要点和开发技巧的书。全书通过100多个独立的实例,全面展示了Windows Phone 8应用开发的各个技术要点,汇集了最常用和最实用的技巧,每个实例都是一个知识点。 这里是...
Windows Phone 7 高级编程 Windows Phone 用户体验设计准则 Windows+Phone7完美开发征程 Windows-Phone-7-UI设计及人机交互指南 wp7 官方中文教程
windows phone 开发 使用C#语言 初始入门参考可用
本书旨在分享强大的Microsoft编程工具(诸如Silverlight、XNA和Visual Studio 2010 Express Edition一类)的使用心得,介绍了如何使用现有的C#和.NET编程技能来开发一些Windows Phone 7应用程序。本书作者团队探讨了...
Windows Phone Blog Menu是一个Silverlight导航控件,看起来像Windows Phone 7。 控件中的tiles链接到web站点在你的博客中使用这个控件可以表达你对WP7的喜爱。 Windows Phone Blog Menu is a Silverlight ...
Android+VS+iPhone 安卓,苹果开发的较量,你说谁赢!!
Windows+Phone+7程序设计01 绝对全,本人以人格担保 Windows+Phone+7程序设计02 有两个切记 两个文件 本人上传资源大小有限 分开上传
Apple+iPhone拆解分析 关于iphone开发的相关知识讲解
windows phone 7 code
微信跳一跳 mac + iphone 图文教程,微信跳一跳这个小游戏最近很火,元旦的时候就发现了有人用python写的脚本可以刷跳一跳的分数。