高级程序员进化史

论一位程序员从初级到高级再到高管晋升过程中的代码演进之路,其中,有你的影子吗?

一位 Go 程序员的进化史

初级 Go 程序员

  1. package fac
  2. func Factorial(n int) int {
  3.     res := 1
  4.     for i := 1; i <= n; i++ {
  5.         res *= i
  6.     }
  7.     return res
  8. }

学会使用函数的 Go 程序员

  1. package fac
  2. func Factorial(n int) int {
  3.     if n == 0 {
  4.         return 1
  5.     } else {
  6.         return Factorial(n - 1) * n
  7.     }
  8. }

泛型 Go 程序员

  1. package fac
  2. func Factorial(n interface{}) interface{} {
  3.     v, valid := n.(int)
  4.     if !valid {
  5.         return 0
  6.     }
  7.     res := 1
  8.     for i := 1; i <= v; i++ {
  9.         res *= i
  10.     }
  11.     return res
  12. }

学会了多线程优化的 Go 程序员

  1. package fac
  2. import 'sync'
  3. func Factorial(n int) int {
  4.     var (
  5.         left, right = 1, 1
  6.         wg sync.WaitGroup
  7.     )
  8.     wg.Add(2)
  9.     pivot := n / 2
  10.     go func() {
  11.         for i := 1; i < pivot; i++ {
  12.             left *= i
  13.         }
  14.         wg.Done()
  15.     }()
  16.     go func() {
  17.         for i := pivot; i <= n; i++ {
  18.             right *= i
  19.         }
  20.         wg.Done()
  21.     }()
  22.     wg.Wait()
  23.     return left * right
  24. }

掌握了 Go 设计模式的程序员

  1. package fac
  2. func Factorial(n int) <-chan int {
  3.     ch := make(chan int)
  4.     go func() {
  5.         prev := 1
  6.         for i := 1; i <= n; i++ {
  7.             v := prev * i
  8.             ch <- v
  9.             prev = v
  10.         }
  11.         close(ch)
  12.     }()
  13.     return ch
  14. }

学会使用成熟的解决方案修复 Go 语言缺点的程序员

  1. package fac
  2. /**
  3.  * @see https://en.wikipedia.org/wiki/Factorial
  4.  */
  5. type IFactorial interface {
  6.     CalculateFactorial() int
  7. }
  8. // FactorialImpl implements IFactorial.
  9. var _ IFactorial = (*FactorialImpl)(nil)
  10. /**
  11.  * Used to find factorial of the n.
  12.  */
  13. type FactorialImpl struct {
  14.     /**
  15.      * The n.
  16.      */
  17.     n int
  18. }
  19. /**
  20.  * Constructor of the FactorialImpl.
  21.  *
  22.  * @param n the n.
  23.  */
  24. func NewFactorial(n int) *FactorialImpl {
  25.     return &FactorialImpl{
  26.         n: n,
  27.     }
  28. }
  29. /**
  30.  * Gets the n to use in factorial function.
  31.  *
  32.  * @return int.
  33.  */
  34. func (this *FactorialImpl) GetN() int {
  35.     return this.n
  36. }
  37. /**
  38.  * Sets the n to use in factorial function.
  39.  *
  40.  * @param n the n.
  41.  * @return void.
  42.  */
  43. func (this *FactorialImpl) SetN(n int) {
  44.     this.n = n
  45. }
  46. /**
  47.  * Returns factorial of the n.
  48.  *
  49.  * @todo remove 'if' statement. Maybe we should use a factory or somthing?
  50.  *
  51.  * @return int.
  52.  */
  53. func (this *FactorialImpl) CalculateFactorial() int {
  54.     if this.n == 0 {
  55.         return 1
  56.     }
  57.     n := this.n
  58.     this.n = this.n - 1
  59.     return this.CalculateFactorial() * n
  60. }

高级 Go 程序员

  1. package fac
  2. // Factorial returns n!.
  3. func Factorial(n int) int {
  4.     res := 1
  5.     for i := 1; i <= n; i++ {
  6.         res *= i
  7.     }
  8.     return res
  9. }

Go 语言之父 Rob Pike

  1. package fac
  2. // Factorial returns n!.
  3. func Factorial(n int) int {
  4.     res := 1
  5.     for i := 1; i <= n; i++ {
  6.         res *= i
  7.     }
  8.     return res
  9. }


一个程序员的进化史

初中/高中时初入门

  1.   10 PRINT 'HELLO WORLD'
  2.   20 END

大一

  1.   program Hello(input, output)
  2.     begin
  3.       writeln('Hello World')
  4.     end.

大四

  1.   (defun hello
  2.     (print
  3.       (cons 'Hello (list 'World))))

初入职场

  1.   #include <stdio.h>
  2.   void main(void)
  3.   {
  4.     char *message[] = {'Hello ', 'World'};
  5.     int i;
  6.     for(i = 0; i < 2; ++i)
  7.       printf('%s', message[i]);
  8.     printf('\n');
  9.   }

中级专家

  1.   #include <iostream.h>
  2.   #include <string.h>
  3.   class string
  4.   {
  5.   private:
  6.     int size;
  7.     char *ptr;
  8.   string() : size(0), ptr(new char[1]) { ptr[0] = 0; }
  9.     string(const string &s) : size(s.size)
  10.     {
  11.       ptr = new char[size + 1];
  12.       strcpy(ptr, s.ptr);
  13.     }
  14.     ~string()
  15.     {
  16.       delete [] ptr;
  17.     }
  18.     friend ostream &operator <<(ostream &, const string &);
  19.     string &operator=(const char *);
  20.   };
  21.   ostream &operator<<(ostream &stream, const string &s)
  22.   {
  23.     return(stream << s.ptr);
  24.   }
  25.   string &string::operator=(const char *chrs)
  26.   {
  27.     if (this != &chrs)
  28.     {
  29.       delete [] ptr;
  30.      size = strlen(chrs);
  31.       ptr = new char[size + 1];
  32.       strcpy(ptr, chrs);
  33.     }
  34.     return(*this);
  35.   }
  36.   int main()
  37.   {
  38.     string str;
  39.     str = 'Hello World';
  40.     cout << str << endl;
  41.     return(0);
  42.   }

主程序员

  1.  [
  2.   uuid(2573F8F4-CFEE-101A-9A9F-00AA00342820)
  3.   ]
  4.   library LHello
  5.   {
  6.       // bring in the master library
  7.       importlib('actimp.tlb');
  8.       importlib('actexp.tlb');
  9.       // bring in my interfaces
  10.       #include 'pshlo.idl'
  11.       [
  12.       uuid(2573F8F5-CFEE-101A-9A9F-00AA00342820)
  13.       ]
  14.       cotype THello
  15.    {
  16.    interface IHello;
  17.    interface IPersistFile;
  18.    };
  19.   };
  20.   [
  21.   exe,
  22.   uuid(2573F890-CFEE-101A-9A9F-00AA00342820)
  23.   ]
  24.   module CHelloLib
  25.   {
  26.       // some code related header files
  27.       importheader(<windows.h>);
  28.       importheader(<ole2.h>);
  29.       importheader(<except.hxx>);
  30.       importheader('pshlo.h');
  31.       importheader('shlo.hxx');
  32.       importheader('mycls.hxx');
  33.       // needed typelibs
  34.       importlib('actimp.tlb');
  35.       importlib('actexp.tlb');
  36.       importlib('thlo.tlb');
  37.       [
  38.       uuid(2573F891-CFEE-101A-9A9F-00AA00342820),
  39.       aggregatable
  40.       ]
  41.       coclass CHello
  42.    {
  43.    cotype THello;
  44.    };
  45.   };
  46.   #include 'ipfix.hxx'
  47.   extern HANDLE hEvent;
  48.   class CHello : public CHelloBase
  49.   {
  50.   public:
  51.       IPFIX(CLSID_CHello);
  52.       CHello(IUnknown *pUnk);
  53.       ~CHello();
  54.       HRESULT  __stdcall PrintSz(LPWSTR pwszString);
  55.   private:
  56.       static int cObjRef;
  57.   };
  58.   #include <windows.h>
  59.   #include <ole2.h>
  60.   #include <stdio.h>
  61.   #include <stdlib.h>
  62.   #include 'thlo.h'
  63.   #include 'pshlo.h'
  64.   #include 'shlo.hxx'
  65.   #include 'mycls.hxx'
  66.   int CHello::cObjRef = 0;
  67.   CHello::CHello(IUnknown *pUnk) : CHelloBase(pUnk)
  68.   {
  69.       cObjRef++;
  70.       return;
  71.   }
  72.   HRESULT  __stdcall  CHello::PrintSz(LPWSTR pwszString)
  73.   {
  74.       printf('%ws
  75. ', pwszString);
  76.       return(ResultFromScode(S_OK));
  77.   }
  78.   CHello::~CHello(void)
  79.   {
  80.   // when the object count goes to zero, stop the server
  81.   cObjRef--;
  82.   if( cObjRef == 0 )
  83.       PulseEvent(hEvent);
  84.   return;
  85.   }
  86.   #include <windows.h>
  87.   #include <ole2.h>
  88.   #include 'pshlo.h'
  89.   #include 'shlo.hxx'
  90.   #include 'mycls.hxx'
  91.   HANDLE hEvent;
  92.    int _cdecl main(
  93.   int argc,
  94.   char * argv[]
  95.   ) {
  96.   ULONG ulRef;
  97.   DWORD dwRegistration;
  98.   CHelloCF *pCF = new CHelloCF();
  99.   hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  100.   // Initialize the OLE libraries
  101.   CoInitializeEx(NULL, COINIT_MULTITHREADED);
  102.   CoRegisterClassObject(CLSID_CHello, pCF, CLSCTX_LOCAL_SERVER,
  103.       REGCLS_MULTIPLEUSE, &dwRegistration);
  104.   // wait on an event to stop
  105.   WaitForSingleObject(hEvent, INFINITE);
  106.   // revoke and release the class object
  107.   CoRevokeClassObject(dwRegistration);
  108.   ulRef = pCF->Release();
  109.   // Tell OLE we are going away.
  110.   CoUninitialize();
  111.   return(0); }
  112.   extern CLSID CLSID_CHello;
  113.   extern UUID LIBID_CHelloLib;
  114.   CLSID CLSID_CHello = { /* 2573F891-CFEE-101A-9A9F-00AA00342820 */
  115.       0x2573F891,
  116.       0xCFEE,
  117.       0x101A,
  118.       { 0x9A, 0x9F, 0x00, 0xAA, 0x00, 0x34, 0x28, 0x20 }
  119.   };
  120.   UUID LIBID_CHelloLib = { /* 2573F890-CFEE-101A-9A9F-00AA00342820 */
  121.       0x2573F890,
  122.       0xCFEE,
  123.       0x101A,
  124.       { 0x9A, 0x9F, 0x00, 0xAA, 0x00, 0x34, 0x28, 0x20 }
  125.   };
  126.   #include <windows.h>
  127.   #include <ole2.h>
  128.   #include <stdlib.h>
  129.   #include <string.h>
  130.   #include <stdio.h>
  131.   #include 'pshlo.h'
  132.   #include 'shlo.hxx'
  133.   #include 'clsid.h'
  134.   int _cdecl main(
  135.   int argc,
  136.   char * argv[]
  137.   ) {
  138.   HRESULT  hRslt;
  139.   IHello        *pHello;
  140.   ULONG  ulCnt;
  141.   IMoniker * pmk;
  142.   WCHAR  wcsT[_MAX_PATH];
  143.   WCHAR  wcsPath[2 * _MAX_PATH];
  144.   // get object path
  145.   wcsPath[0] = '\0';
  146.   wcsT[0] = '\0';
  147.   if( argc > 1) {
  148.       mbstowcs(wcsPath, argv[1], strlen(argv[1]) + 1);
  149.       wcsupr(wcsPath);
  150.       }
  151.   else {
  152.       fprintf(stderr, 'Object path must be specified\n');
  153.       return(1);
  154.       }
  155.   // get print string
  156.   if(argc > 2)
  157.       mbstowcs(wcsT, argv[2], strlen(argv[2]) + 1);
  158.   else
  159.       wcscpy(wcsT, L'Hello World');
  160.   printf('Linking to object %ws\n', wcsPath);
  161.   printf('Text String %ws\n', wcsT);
  162.   // Initialize the OLE libraries
  163.   hRslt = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  164.   if(SUCCEEDED(hRslt)) {
  165.       hRslt = CreateFileMoniker(wcsPath, &pmk);
  166.       if(SUCCEEDED(hRslt))
  167.    hRslt = BindMoniker(pmk, 0, IID_IHello, (void **)&pHello);
  168.       if(SUCCEEDED(hRslt)) {
  169.    // print a string out
  170.    pHello->PrintSz(wcsT);
  171.    Sleep(2000);
  172.    ulCnt = pHello->Release();
  173.    }
  174.       else
  175.    printf('Failure to connect, status: %lx', hRslt);
  176.       // Tell OLE we are going away.
  177.       CoUninitialize();
  178.       }
  179.   return(0);
  180.   }

初级黑客

  1.   #!/usr/local/bin/perl
  2.   $msg='Hello, world.\n';
  3.   if ($#ARGV >= 0) {
  4.     while(defined($arg=shift(@ARGV))) {
  5.       $outfilename = $arg;
  6.       open(FILE, '>' . $outfilename) || die 'Can't write $arg: $!\n';
  7.       print (FILE $msg);
  8.       close(FILE) || die 'Can't close $arg: $!\n';
  9.     }
  10.   } else {
  11.     print ($msg);
  12.   }
  13.   1;

中级黑客

  1.   #include <stdio.h>
  2.   #define S 'Hello, World\n'
  3.   main(){exit(printf(S) == strlen(S) ? 0 : 1);}

资深黑客

  1.   % cc -o a.out ~/src/misc/hw/hw.c
  2.   % a.out

大师级黑客

  % echo 'Hello, world.'

初级经理

  1.   10 PRINT 'HELLO WORLD'
  2.   20 END

中级经理

  1.   mail -s 'Hello, world.' bob@b12
  2.   Bob, could you please write me a program that prints 'Hello, world.'?
  3.   I need it by tomorrow.
  4.   ^D

高级经理

  1.   % zmail jim
  2.   I need a 'Hello, world.' program by this afternoon.

高管

  1.   % letter
  2.   letter: Command not found.
  3.   % mail
  4.   To: ^X ^F ^C
  5.   % help mail
  6.   help: Command not found.
  7.   % damn!
  8.   !: Event unrecognized
  9.   % logout

原文地址:

https://github.com/SuperPaintman/the-evolution-of-a-go-programmer

https://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html

60+专家,13个技术领域,CSDN 《IT 人才成长路线图》重磅来袭!

直接扫码或微信搜索「CSDN」公众号,后台回复关键词「路线图」,即可获取完整路线图!

(0)

相关推荐