什么样的代码看起来才舒服?(300)

  • 主题发起人 主题发起人 smlabc
  • 开始时间 开始时间
S

smlabc

Unregistered / Unconfirmed
GUEST, unregistred user!
最近感觉不管写什么代码,看起来都很别扭,不舒服,希望大家贴点看起来舒服的代码,以下抛砖引玉贴个垃圾单元://封装了URLDownloadToFile函数unit FileDownLoadThread;interfaceuses Classes, SysUtils, Windows, ActiveX, UrlMon;const S_ABORT = HRESULT($80004004); type TFileDownLoadThread = class; TDownLoadProcessEvent = procedure(Sender:TFileDownLoadThread;Progress, ProgressMax:Cardinal) of object; TDownLoadCompleteEvent = procedure(Sender:TFileDownLoadThread) of object ; TDownLoadFailEvent = procedure(Sender:TFileDownLoadThread;Reason:LongInt) of object ; TDownLoadMonitor = class( TInterfacedObject, IBindStatusCallback ) private FShouldAbort: Boolean; FThread:TFileDownLoadThread; protected function OnStartBinding( dwReserved: DWORD; pib: IBinding ): HResult; stdcall; function GetPriority( out nPriority ): HResult; stdcall; function OnLowResource( reserved: DWORD ): HResult; stdcall; function OnProgress( ulProgress, ulProgressMax, ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult; stdcall; function OnStopBinding( hresult: HResult; szError: LPCWSTR ): HResult; stdcall; function GetBindInfo( out grfBINDF: DWORD; var bindinfo: TBindInfo ): HResult; stdcall; function OnDataAvailable( grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc; stgmed: PStgMedium ): HResult; stdcall; function OnObjectAvailable( const iid: TGUID; punk: IUnknown ): HResult; stdcall; public constructor Create(AThread:TFileDownLoadThread); property ShouldAbort: Boolean read FShouldAbort write FShouldAbort; end; TFileDownLoadThread = class( TThread ) private FSourceURL: string; FSaveFileName: string; FProgress,FProgressMax:Cardinal; FOnProcess: TDownLoadProcessEvent; FOnComplete: TDownLoadCompleteEvent; FOnFail: TDownLoadFailEvent; FMonitor: TDownLoadMonitor; protected procedure Execute; override; procedure UpdateProgress(Progress, ProgressMax, StatusCode: Cardinal; StatusText:string); procedure DoUpdateUI; public constructor Create( ASrcURL, ASaveFileName: string; AProgressEvent:TDownLoadProcessEvent = nil; ACompleteEvent:TDownLoadCompleteEvent = nil;AFailEvent:TDownLoadFailEvent=nil;CreateSuspended: Boolean=False); property SourceURL: string read FSourceURL; property SaveFileName: string read FSaveFileName; property OnProcess: TDownLoadProcessEvent read FOnProcess write FOnProcess; property OnComplete: TDownLoadCompleteEvent read FOnComplete write FOnComplete; property OnFail: TDownLoadFailEvent read FOnFail write FOnFail; end;implementationconstructor TDownLoadMonitor.Create(AThread: TFileDownLoadThread);begin inherited Create; FThread:=AThread; FShouldAbort:=False;end;function TDownLoadMonitor.GetBindInfo( out grfBINDF: DWORD; var bindinfo: TBindInfo ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.GetPriority( out nPriority ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnDataAvailable( grfBSCF, dwSize: DWORD; formatetc: PFormatEtc; stgmed: PStgMedium ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnLowResource( reserved: DWORD ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnObjectAvailable( const iid: TGUID; punk: IInterface ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnProgress( ulProgress, ulProgressMax, ulStatusCode: ULONG; szStatusText: LPCWSTR ): HResult;begin if FThread<>nil then FThread.UpdateProgress(ulProgress,ulProgressMax,ulStatusCode,''); if FShouldAbort then Result := E_ABORT else Result := S_OK;end;function TDownLoadMonitor.OnStartBinding( dwReserved: DWORD; pib: IBinding ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnStopBinding( hresult: HResult; szError: LPCWSTR ): HResult;begin Result := S_OK;end;{ TFileDownLoadThread }constructor TFileDownLoadThread.Create( ASrcURL, ASaveFileName: string;AProgressEvent:TDownLoadProcessEvent ; ACompleteEvent:TDownLoadCompleteEvent;AFailEvent:TDownLoadFailEvent; CreateSuspended: Boolean );begin if (@AProgressEvent=nil) or (@ACompleteEvent=nil) or (@AFailEvent=nil) then CreateSuspended:=True; inherited Create( CreateSuspended ); FSourceURL:=ASrcURL; FSaveFileName:=ASaveFileName; FOnProcess:=AProgressEvent; FOnComplete:=ACompleteEvent; FOnFail:=AFailEvent;end;procedure TFileDownLoadThread.DoUpdateUI;begin if Assigned(FOnProcess) then FOnProcess(Self,FProgress,FProgressMax);end;procedure TFileDownLoadThread.Execute;var DownRet:HRESULT;begin inherited; FMonitor:=TDownLoadMonitor.Create(Self); DownRet:= URLDownloadToFile( nil, PAnsiChar( FSourceURL ), PAnsiChar( FSaveFileName ), 0,FMonitor as IBindStatusCallback); if DownRet=S_OK then begin if Assigned(FOnComplete) then FOnComplete(Self); end else begin if Assigned(FOnFail) then FOnFail(Self,DownRet); end; FMonitor.Free; FMonitor:=nil;end;procedure TFileDownLoadThread.UpdateProgress(Progress, ProgressMax, StatusCode: Cardinal; StatusText: string);begin FProgress:=Progress; FProgressMax:=ProgressMax; Synchronize(DoUpdateUI); if Terminated then FMonitor.ShouldAbort:=True;end;end.
 
写的好像很不错啊,怎么能说这个是垃圾呢
 
你这个看起来就比较舒服啊~~
 
看起来很舒服阿。人不舒服?
 
舒服吗 我看是罗嗦。 这样的代码 一向是我打击的对象。
 
unit FileDownLoadThread;interfaceuses Classes, SysUtils, Windows, ActiveX, UrlMon;const S_ABORT = HRESULT($80004004);type{Code Comment Here....and descriptions} TFileDownLoadThread = class;{Code Comment Here....and descriptions} TDownLoadProcessEvent = procedure(Sender: TFileDownLoadThread;Progress, ProgressMax: Cardinal) of object; TDownLoadCompleteEvent = procedure(Sender: TFileDownLoadThread) of object ; TDownLoadFailEvent = procedure(Sender:TFileDownLoadThread; Reason: LongInt) of object ;{Code Comment Here....and descriptions} TDownLoadMonitor = class(TInterfacedObject, IBindStatusCallback) private FShouldAbort: Boolean; FThread:TFileDownLoadThread; protected function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG; szStatusText: LPCWSTR): HResult; stdcall; function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc; stgmed: PStgMedium ): HResult; stdcall; function GetPriority( out nPriority ): HResult; stdcall; function OnLowResource(reserved: DWORD ): HResult; stdcall; function OnStartBinding(dwReserved: DWORD; pib: IBinding ): HResult; stdcall; function OnStopBinding(hresult: HResult; szError: LPCWSTR ): HResult; stdcall; function OnObjectAvailable(const iid: TGUID; punk: IUnknown ): HResult; stdcall; function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo ): HResult; stdcall; public constructor Create(AThread:TFileDownLoadThread); property ShouldAbort: Boolean read FShouldAbort write FShouldAbort; end;{Code Comment Here....and descriptions} TFileDownLoadThread = class( TThread ) private FSourceURL: string; FSaveFileName: string; FProgress,FProgressMax:Cardinal; FOnProcess: TDownLoadProcessEvent; FOnComplete: TDownLoadCompleteEvent; FOnFail: TDownLoadFailEvent; FMonitor: TDownLoadMonitor; protected procedure Execute; override; procedure UpdateProgress(Progress, ProgressMax, StatusCode: Cardinal; StatusText:string); procedure DoUpdateUI; public constructor Create(ASrcURL, ASaveFileName: string; AProgressEvent:TDownLoadProcessEvent = nil; ACompleteEvent:TDownLoadCompleteEvent = nil; AFailEvent:TDownLoadFailEvent=nil; CreateSuspended: Boolean=False); property SourceURL: string read FSourceURL; property SaveFileName: string read FSaveFileName; property OnProcess: TDownLoadProcessEvent read FOnProcess write FOnProcess; property OnComplete: TDownLoadCompleteEvent read FOnComplete write FOnComplete; property OnFail: TDownLoadFailEvent read FOnFail write FOnFail; end;implementationconstructor TDownLoadMonitor.Create(AThread:TFileDownLoadThread);begin inherited Create; FThread:=AThread; FShouldAbort:=False;end;{Code Comment Here....and descriptions}{Code Comment Here....and descriptions}{Code Comment Here....and descriptions}function TDownLoadMonitor.GetBindInfo(out grfBINDF:DWORD; var bindinfo:TBindInfo):HResult;begin Result := S_OK;end;function TDownLoadMonitor.GetPriority(out nPriority ): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnDataAvailable(grfBSCF,dwSize:DWORD; formatetc:PFormatEtc;stgmed:PStgMedium ):HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnLowResource(reserved: DWORD): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnObjectAvailable(const iid: TGUID; punk: IInterface): HResult;begin Result := S_OK;end;{Code Comment Here....and descriptions}function TDownLoadMonitor.OnProgress(ulProgress,ulProgressMax, ulStatusCode:ULONG;szStatusText:LPCWSTR):HResult;begin if FThread<>nil then FThread.UpdateProgress(ulProgress,ulProgressMax,ulStatusCode,''); if FShouldAbort then Result := E_ABORT else Result := S_OK;end;{Code Comment Here....and descriptions}function TDownLoadMonitor.OnStartBinding(dwReserved: DWORD;pib: IBinding): HResult;begin Result := S_OK;end;function TDownLoadMonitor.OnStopBinding(hresult:HResult;szError:LPCWSTR): HResult;begin Result := S_OK;end;{ TFileDownLoadThread }constructor TFileDownLoadThread.Create(ASrcURL, ASaveFileName: string; AProgressEvent: TDownLoadProcessEvent; ACompleteEvent: TDownLoadCompleteEvent; AFailEvent: TDownLoadFailEvent; CreateSuspended:Boolean);begin if (@AProgressEvent=nil) or (@ACompleteEvent=nil) or (@AFailEvent=nil) then CreateSuspended:=True; inherited Create( CreateSuspended ); FSourceURL:=ASrcURL; FSaveFileName:=ASaveFileName; FOnProcess:=AProgressEvent; FOnComplete:=ACompleteEvent; FOnFail:=AFailEvent;end;{Code Comment Here....and descriptions}procedure TFileDownLoadThread.DoUpdateUI;begin if Assigned(FOnProcess) then FOnProcess(Self,FProgress,FProgressMax);end;procedure TFileDownLoadThread.Execute;var DownRet:HRESULT;begin inherited; FMonitor:=TDownLoadMonitor.Create(Self); DownRet:= URLDownloadToFile(nil,PAnsiChar(FSourceURL), PAnsiChar(FSaveFileName),0,FMonitor as IBindStatusCallback); if DownRet=S_OK then begin if Assigned(FOnComplete) then FOnComplete(Self); end else begin if Assigned(FOnFail) then FOnFail(Self,DownRet); end; FMonitor.Free; FMonitor:=nil;end;{Code Comment Here....and descriptions}procedure TFileDownLoadThread.UpdateProgress(Progress, ProgressMax, StatusCode: Cardinal; StatusText: string);begin FProgress:=Progress; FProgressMax:=ProgressMax; Synchronize(DoUpdateUI); if Terminated then FMonitor.ShouldAbort:=True;end;end.简单排了一下版.
 
=========== 以下代码风格一看就是出自代码民工之手 ======================procedure TFileDownLoadThread.Execute;var DownRet:HRESULT;begin inherited; FMonitor:=TDownLoadMonitor.Create(Self); DownRet:= URLDownloadToFile( nil, PAnsiChar( FSourceURL ), PAnsiChar( FSaveFileName ), 0,FMonitor as IBindStatusCallback); if DownRet=S_OK then begin if Assigned(FOnComplete) then FOnComplete(Self); end else begin if Assigned(FOnFail) then FOnFail(Self,DownRet); end; FMonitor.Free; FMonitor:=nil;end;========== 以下是本姑娘改的, 够"大家闺秀"了吧~~~~~~ =================procedure TFileDownLoadThread.Execute;var DownRet: HRESULT;begin inherited; FMonitor := TDownLoadMonitor.Create(Self); DownRet := URLDownloadToFile(nil, PAnsiChar(FSourceURL), PAnsiChar(FSaveFileName), 0, FMonitor as IBindStatusCallback); if DownRet = S_OK then if Assigned(FOnComplete) then FOnComplete(Self) else if Assigned(FOnFail) then FOnFail(Self, DownRet); FMonitor.Free; FMonitor := nil;end;以前我也有这样的感觉, 后来我琢磨了一些高手的风格, 象这样一变, 读代码就清爽多了.begin 下面的字对着begin后面, 这样漂亮些.代码风格可以看出一个人的人格.自己找时间写个对齐的小软件吧.
 
多谢冰力不足,不过这个不是重点,重点是觉得看起来不舒服啊!难道年纪大了不适合当代码工人了?
 
TO: 关门放狗多谢批评,不过貌似真要封装,非这么罗嗦不可.
 
这个垃圾还是不错的垃圾哦.
 
========== 以下是本姑娘改的, 够"大家闺秀"了吧~~~~~~ =================procedure TFileDownLoadThread.Execute;var DownRet: HRESULT;begin inherited; FMonitor := TDownLoadMonitor.Create(Self); DownRet := URLDownloadToFile(nil, PAnsiChar(FSourceURL), PAnsiChar(FSaveFileName), 0, FMonitor as IBindStatusCallback); if DownRet = S_OK then if Assigned(FOnComplete) then FOnComplete(Self) else if Assigned(FOnFail) then FOnFail(Self, DownRet); FMonitor.Free; FMonitor := nil;end;本来蛮好的,一改倒改错了
 
为什么看起来不舒服?不可能是一边写一边不舒服,肯定是写完了,或者打开原来写的东西猛一看,不爽!想想猛一看和细一看的区别是什么?猛一看,满眼代码,晕,而且心理上有一点点潜在的畏惧感。细一看,看进去,看明白了,就舒服了。所以要想猛一看也能爽,必须加注释,注释完了,让代码有种抑扬顿挫的美感,而且让人有安全感,这样猛一看也很爽。
 
还是轻舞肥羊兄弟眼尖
 
其实我觉得还是习惯问题,就象那个所谓“代码民工”写的代码,我们公司基本上都是这个风格,大家都看的很习惯,也感觉很舒服。至于被改过的,我看的不太习惯,如: 1、为什么声明的变量和var要放在一行??如果有两个变量怎么声明?和第一个变量一列?我记得Delphi源代码中应该是另起一行吧,而且很多语言声明时都是另起一行的。 2、if语句后面加begin、end问题,首先,我觉得被改过后的程序有问题了,第二,加begin、end和不加的,那个更容易被看懂?我觉得还是易读比较重要,并不是每个人都是你这样的高手。 3、一个语句写成多行的问题,无论是专家的推荐还是Delphi源代码中,一个长行都是被分成多行的,有高手还说:一行不能超过80个字符,但我个人的习惯还是放在一行中,这样比较容易看懂,所以我们公司开发软件的,显示分辨率都是1280*1024的,这样可以在一行中显示更多的字符,尤其是写sql语句时,一行写的多一点,在某些场合感觉层次更分明、更易懂。
 
再补充一句,如果某些人不习惯排版,可以装一个排版软件,如cnpack,写完代码后,执行一条命令,排的绝对是工工整整,而且还可以自定义风格。
 
自己写的代码,至少保持前后风格的统一而你的这些代码从格式上就不统一我一直觉得好的程序员是有洁癖的
 
delphi 真的完了,有问题,没人答,废问题,一堆人堆着!
 
========== 以下是本姑娘改的, 够"大家闺秀"了吧~~~~~~ =================--------->不建议你去掉人家的BEGIN END. 没什么意思,并且,少了BEGIN END,很容易搞错逻辑关系.还有,也不利于将来改代码(假如还要加逻辑,你就肯定会乱的...)一句话,你这样改,得不尝失
 
已经写得非常不错了,大约有3年以上经验了,缺点还是有的 ===> 注释 ,唯一缺的就是注释
 
后退
顶部