﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-love in C++, live on MFC-随笔分类-C++ Coding Standards</title><link>http://www.cppblog.com/flyingxu/category/1323.html</link><description>to get ready...</description><language>zh-cn</language><lastBuildDate>Sun, 25 May 2008 19:10:21 GMT</lastBuildDate><pubDate>Sun, 25 May 2008 19:10:21 GMT</pubDate><ttl>60</ttl><item><title>Make header files self-sufficient的一个例子</title><link>http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Fri, 23 Jun 2006 12:56:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/8908.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/8908.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/8908.html</trackback:ping><description><![CDATA[C＋＋编程规范中第23条中说：<br />If one header file won't work unless the file that includes it also includes another header, that's gauche and puts unnecessary burden on that header file's users.<br />呵呵，英语有点拗口，后面举的例子也都是模板的例子。我倒是遇到过一个实际的例子，简化后如下：<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">a.h</span></font><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff"><font face="Courier New">class</font></span><font face="Courier New"><span style="COLOR: #000000"> A <br />{<br /> </span><span style="COLOR: #0000ff">int</span></font><span style="COLOR: #000000"><font face="Courier New"> a;<br />};</font></span></div> <br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">b.h<br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">注意：b.h并没有include a.h</span><span style="COLOR: #008000"><br /></span><span style="COLOR: #0000ff"><font face="Courier New">class</font></span><span style="COLOR: #000000"><font face="Courier New"> B <br />{<br /> A a;<br />};</font> </span></div>用的时候怎么用呢？比如在main.cpp中想用class B<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #008000">//</span><span style="COLOR: #008000">main.cpp</span></font><span style="COLOR: #008000"><br /></span><font face="Courier New"><span style="COLOR: #000000">#include </span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">a.h</span><span style="COLOR: #000000">"</span></font><span style="COLOR: #000000"><br /><font face="Courier New">#include </font></span><font face="Courier New"><span style="COLOR: #000000">"</span><span style="COLOR: #000000">b.h</span><span style="COLOR: #000000">"</span></font><span style="COLOR: #000000"><br /></span><font face="Courier New"><span style="COLOR: #008000">//</span><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/dot.gif" /></span></font><span style="COLOR: #008000"><br /></span><font face="Courier New"><span style="COLOR: #000000">B b;<br /></span><span style="COLOR: #008000">//</span></font><font face="Courier New"><span style="COLOR: #008000"><img src="http://www.cppblog.com/images/dot.gif" /><br /></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">do something on b</span></font></div>可以看到，main为了使用B，还得包含a.h，这就是上面一段英语说的：<br />如果一个头文件（b.h）不能工作，除非包含它（b.h）的文件（也就是main.cpp）也包含另一个头文件（a.h）...<br /><br />hehe，当我第一次遇到这种情况时，想了半天也想不明白为什么我明明包含了b.h还是不能用B啊<br /><br />那么，正确的写法应该是什么呢？<br />在类B的作者在设计B的时候，就应该想到用户只需要#include "b.h"就可以使用这个class B。所以，在b.h文件中，应该写明#include "a.h"，而不是让用户在main.cpp中去include "a.h"<br /><br />:)<img src ="http://www.cppblog.com/flyingxu/aggbug/8908.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-06-23 20:56 <a href="http://www.cppblog.com/flyingxu/archive/2006/06/23/8908.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>用返回类型BOOL替代函数返回类型void</title><link>http://www.cppblog.com/flyingxu/archive/2006/05/17/7305.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Wed, 17 May 2006 08:12:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/05/17/7305.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/7305.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/05/17/7305.html#Feedback</comments><slash:comments>10</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/7305.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/7305.html</trackback:ping><description><![CDATA[在写函数的时候,很多函数只是设置某个变量然后做一些其他的相应的处理,比如:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000"> CInstruction::SetName(</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"> CString</span><span style="COLOR: #000000">&amp;</span></font><font face="Courier New"><span style="COLOR: #000000"> strName)<br />{<br />    m_strName </span><span style="COLOR: #000000">=</span></font><font face="Courier New"><span style="COLOR: #000000"> strName;    <br /></span></font><span style="COLOR: #008000"><br />            //some other operations<br /></span><font face="Courier New"><span style="COLOR: #000000">    </span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(</span><span style="COLOR: #000000">!</span></font><font face="Courier New"><span style="COLOR: #000000">IsRed())<br />    {<br />        //...        <br /></span></font><span style="COLOR: #000000"><font face="Courier New">    }<br />}</font></span></div>这样的函数,返回类型为<font face="Courier New">void</font>顺理成章.<br /><br />但是,随着程序继续往下写,需要处理的情况越来越复杂,这个时候,发现SetName这个函数,返回void已经不够用了,因为可以需要知道SetName这个函数调用有没有成功,因为可能传入的strName不一定是合法的,如果调用SetName函数没有成功,可能要继续调用别的函数,这个时候,可能就要讲返回类型改为BOOL.<br /><br />而后来继续想,一个函数的返回值,应该更加充分的利用,调用一个函数时,就该充分的利用它的返回值;而在写一个函数时,也应该注意到这一点.如果实在没有什么值好返回,就返回TRUE<br /><br />所以,对于函数,如果在设计时准备把返回类型定为void,其实可以改为BOOL<br />1)最初可能没有用,但情况会越来越复杂<br />2)充分利用函数的返回值<img src ="http://www.cppblog.com/flyingxu/aggbug/7305.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-05-17 16:12 <a href="http://www.cppblog.com/flyingxu/archive/2006/05/17/7305.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>尽量用代码来替代资源中的属性</title><link>http://www.cppblog.com/flyingxu/archive/2006/05/17/7299.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Wed, 17 May 2006 06:17:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/05/17/7299.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/7299.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/05/17/7299.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/7299.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/7299.html</trackback:ping><description><![CDATA[资源中的属性,是指写MFC程序时VC IDE的resource编辑器中可以设定的各种属性,比如<font face="Courier New">WS_EX_TRANSPARENT</font>或者<font face="Courier New">LVS_REPORT</font>.<br />而这些属性,都是可以用代码在程序初始化后加上去的,比如在<font face="Courier New">OnInitDialog()</font> 或者<font face="Courier New">OnCreate()</font>函数中.<br /><br />从方便性来说,肯定是在资源中设置这些属性更加方便,但是..<br />我遇到一种情况,在我的程序中,有两个rc文件,分别是中文和英文,这个时候,就发生了在英文资源中设置了属性修正了一个bug但是在中文版的资源中忘记设置,bug依然存在.而如果是在在代码中加上类似的代码:<br /><div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><font face="Courier New"><span style="COLOR: #000000">m_CrossRefTab.ModifyStyleEx(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,WS_EX_TRANSPARENT);</span></font></div>资源文件不用改变,而且修正了所有的问题.<br /><br />所以,我觉得,应该尽量用几行代码来替代在资源中的设定属性<img src ="http://www.cppblog.com/flyingxu/aggbug/7299.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-05-17 14:17 <a href="http://www.cppblog.com/flyingxu/archive/2006/05/17/7299.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>code review 把代码投影在墙上</title><link>http://www.cppblog.com/flyingxu/archive/2006/04/09/5206.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Sun, 09 Apr 2006 13:37:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/04/09/5206.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/5206.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/04/09/5206.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/5206.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/5206.html</trackback:ping><description><![CDATA[
		<a class="" title="" href="http://www.cublog.cn/u/4222/" target="_blank">锋锋</a>说：<br />把代码投影在墙上，几个人坐下来一起评论也是有效的方法<br /><br />呵呵，我觉得很cool啊，可惜现在我不是老板，等我哪天成老板了，就这么做<img src ="http://www.cppblog.com/flyingxu/aggbug/5206.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-04-09 21:37 <a href="http://www.cppblog.com/flyingxu/archive/2006/04/09/5206.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Is Hungarian notation obsolete?</title><link>http://www.cppblog.com/flyingxu/archive/2006/04/05/5043.html</link><dc:creator>flyingxu</dc:creator><author>flyingxu</author><pubDate>Wed, 05 Apr 2006 11:45:00 GMT</pubDate><guid>http://www.cppblog.com/flyingxu/archive/2006/04/05/5043.html</guid><wfw:comment>http://www.cppblog.com/flyingxu/comments/5043.html</wfw:comment><comments>http://www.cppblog.com/flyingxu/archive/2006/04/05/5043.html#Feedback</comments><slash:comments>5</slash:comments><wfw:commentRss>http://www.cppblog.com/flyingxu/comments/commentRss/5043.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/flyingxu/services/trackbacks/5043.html</trackback:ping><description><![CDATA[
		<p>书上说匈牙利命名法已经过时了，我不这样认为。<br /><br />有人认为现在编译器已经可以很好的检测出类型的不匹配，或者IDE中可以很快的看到类型，所以在c中可能需要，在C＋＋（强类型语言）中就不需要了。<br /><em>C++ made it harder to do that without wicked casting and the compiler catches most of those kind of errors.  So, I agree with the previous poster that it's now redundant.<br />Also, modern IDEs allow you to hover the cursor over a variable and show you the variable's definition.</em><br /><br />不过我觉得代码不是写给编译器看的，而是写给人看的，这里就有self-documenting和readability的问题。<br />很明显，如果你看到nIndex 或者strFile或者wndNext，就可以很快知道分别是int CString CWnd类型，而不用回头去看变量定义，这样，看代码时就会很快。<br />而且，对于MFC程序员来说，更重要一些，因为MFC里面的变量都是用匈牙利命名法的。<br /><em>If you're programming C++/MFC you're better sticking to <b style="COLOR: black; BACKGROUND-COLOR: #ffff66">hungarian</b> for consistency with the class library &amp; Win32 API declarations.<br /></em><a class="" title="微软的约定" href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs600/html/hunganotat.asp" target="_blank">微软的约定</a>，就是标准了 <img height="19" src="http://www.cppblog.com/Emoticons/72_72.gif" width="19" border="0" /><br /><br />不过，书上提到在泛型编程中不需要，现在体会还不深，可能是对的。<br /><br />今天(2006 04 13碰巧看到codeproject的一个vote),结果如下<br /></p>
		<table cellspacing="0" cellpadding="2">
				<tbody>
						<tr bgcolor="#ff9900">
								<td valign="center">
										<b>Option</b>
								</td>
								<td valign="center" width="10%">
										<b>Votes</b>
								</td>
								<td valign="center" align="middle" width="10%">
										<b>%</b>
								</td>
						</tr>
						<tr bgcolor="#fafafa">
								<td valign="center" width="100%">Pascal Cased</td>
								<td valign="center" align="right">171</td>
								<td valign="center" align="right">
										<b>10.6</b>
								</td>
						</tr>
						<tr bgcolor="#fafafa">
								<td valign="center" width="100%">camel Cased</td>
								<td valign="center" align="right">702</td>
								<td valign="center" align="right">
										<b>43.4</b>
								</td>
						</tr>
						<tr bgcolor="#fafafa">
								<td valign="center" width="100%">Fixed letter prefix (eg lLocal)</td>
								<td valign="center" align="right">81</td>
								<td valign="center" align="right">
										<b>5.0</b>
								</td>
						</tr>
						<tr bgcolor="#fafafa">
								<td valign="center" width="100%">Hungarian prefix (eg strLocal)</td>
								<td valign="center" align="right">481</td>
								<td valign="center" align="right">
										<b>29.7</b>
								</td>
						</tr>
						<tr bgcolor="#fafafa">
								<td valign="center" width="100%">Scope prefix (eg l_Local)</td>
								<td valign="center" align="right">36</td>
								<td valign="center" align="right">
										<b>2.2</b>
								</td>
						</tr>
						<tr bgcolor="#fafafa">
								<td valign="center" width="100%">Scope and Hungarian prefix (eg l_strLocal)</td>
								<td valign="center" align="right">125</td>
								<td valign="center" align="right">
										<b>7.7</b>
								</td>
						</tr>
						<tr bgcolor="#85bc4c">
								<td valign="center">
										<b>Responses</b>
								</td>
								<td valign="center" align="right">
										<b>1618</b>
								</td>
								<td valign="center" align="right">
										<script>
												<!--
D(["mb","�</td></tr>\n</table></blockquote>\n<h2>Most popular new articles 3 Apr 2006 - 10 Apr 2006</h2>\n<ul>\n<li><a href\u003d\"http://www.codeproject.com/article.asp?tag\u003d38800032665938819\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">Symbolic Differentiation</a> - Hatem Mostafa\n<li><a href\u003d\"http://www.codeproject.com/article.asp?tag\u003d38800032662838819\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">Simple AJAX implementation for ASP.NET Web applications</a> - Alexander Turlov\n<li><a href\u003d\"http://www.codeproject.com/article.asp?tag\u003d38800032686138819\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">Hooking the kernel directly</a> - Anton Bassov\n<li><a href\u003d\"http://www.codeproject.com/article.asp?tag\u003d38800032666838819\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">The beginner\'s guide to using enum Flags</a> - v2.0\n<li><a href\u003d\"http://www.codeproject.com/article.asp?tag\u003d38800032687038819\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">Option Pricing using the Binomial Tree Model in C#</a> - AndrewPeters\n</li></li></li></li></li></ul>\n<h2><a href\u003d\"http://www.codeproject.com/info/latest.asp\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">Latest Additions</a></h2>\n<p>73 articles overall. 39 new, 34 updated, 6 moved. 43 articles were edited, 30 unedited.<br>Article types listed: New, Updated, edited, unedited\n<br>Article categories listed: General, MFC/C++, C#, <a href\u003d\"http://ASP.NET\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">ASP.NET</a>, .NET, <a href\u003d\"http://VB.NET\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">VB.NET</a>\n<h3><i>Articles posted in the last week</i></h3>\n<h3><a href\u003d\"http://www.codeproject.com/vb/net/\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">VB.NET</a></h3>\n<ul>\n<li><a href\u003d\"http://www.codeproject.com/article.asp?tag\u003d38800032665038819\" target\u003d\"_blank\" onclick\u003d\"return top.js.OpenExtLink(window,event,this)\">",1]
);

//-->
										</script>
�</td>
						</tr>
				</tbody>
		</table>
		<br />Hungarian Notation排第二.<br />cp上面有两个链接<br />Conversations: Hungarian wartHogs (<a href="http://www.cuj.com/documents/s=7989/cujcexp1911hyslop/hyslop.htm">http://www.cuj.com/documents/s=7989/cujcexp1911hyslop/hyslop.htm</a>)<br />号称这篇文章就已经明白的说HN过时了(作者也是<em>c++ coding stardard</em>的作者).<br />如果不用HN,那么应该用什么样的命名规则呢?<br />Naming Guidelines(<a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconNamingGuidelines.asp">http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconNamingGuidelines.asp</a>)<br />.Net下的推荐,也许别的地方也可以用.<img src ="http://www.cppblog.com/flyingxu/aggbug/5043.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/flyingxu/" target="_blank">flyingxu</a> 2006-04-05 19:45 <a href="http://www.cppblog.com/flyingxu/archive/2006/04/05/5043.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>