\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{filoucool} \pdfinfo{ /Title (c-coding-standards.pdf) /Creator (Cheatography) /Author (filoucool) /Subject (C\# coding standards Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{10A333} \definecolor{LightBackground}{HTML}{F0F9F2} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{C\# coding standards Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{filoucool} via \textcolor{DarkBackground}{\uline{cheatography.com/157372/cs/33343/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}filoucool \\ \uline{cheatography.com/filoucool} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 25th July, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Control Flow}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avoid changing a loop variable inside a for loop block.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to update loop variables close to where the loop condition is specified.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always put a default label for switch statements} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Avoid conditions with double negatives.} \tn % Row Count 6 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Object Lifecycle}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to declare variables close to where they are used.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to initialize variables at the point of declaration.} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never shadow a name in an outer scope.} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Avoid implementing a finalizer.} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avoid empty finalizers.} \tn % Row Count 7 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always implement IDisposable if a classes uses unmanaged resources or owns disposable objects.} \tn % Row Count 9 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never call a virtual method unless the object is fully constructed.} \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Exceptions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never silently ignore exceptions.} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to use standard exceptions.} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to throw the most specific exception possible.} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always log when an exception is thrown. Log verbosely.} \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always create verbose error messages.} \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always list the explicit exceptions a method or property can throw.} \tn % Row Count 8 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{General Guidelines}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always favor simplicity. Write the simplest code that will work. KISS.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always favor readability. Name your variables and methods as clearly and as descriptively as possible.} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to keep methods under 25 lines of code (excluding vertical spacing and comments). Break down your code into small functions that are easy to understand. If a method is over 30 lines of code, it should be refactored.} \tn % Row Count 10 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to keep classes under 400 lines of code (excluding vertical spacing and comments). If a class is over 500 lines of code, it should be split out into separate classes that each do one thing.} \tn % Row Count 14 (+ 4) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to keep your code DRY, extract duplicate code into methods.} \tn % Row Count 16 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to include design-pattern names such as Bridge, Adapter, or Factory as suffix to class names where appropriate.} \tn % Row Count 19 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avoid duplicating constants. Separate into a config file.} \tn % Row Count 21 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always delete unreferenced files.} \tn % Row Count 22 (+ 1) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to have lines less than 80 characters. If the line exceeds 160 characters, it should be refactored.} \tn % Row Count 25 (+ 3) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Never have more than two nested calls on the same line.} \tn % Row Count 27 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never have methods with more than three levels of nesting.} \tn % Row Count 29 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Never have methods with more than five parameters. Try to have no more than three parameters at most being passed into a method, and generally only two.} \tn % Row Count 33 (+ 4) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{General Guidelines (cont)}} \tn % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avoid having methods with out parameters.} \tn % Row Count 1 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Avoid having methods with ref parameters.} \tn % Row Count 2 (+ 1) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avoid writing overloads for methods that you might use some day. Follow YAGNI principle.} \tn % Row Count 4 (+ 2) % Row 15 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Never programmatically click a button to execute the same action you have written in the button click event. Rather, call the same method which is called by the button click event handler.} \tn % Row Count 8 (+ 4) % Row 16 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never hardcode a path or drive name in code. Get the application path programmatically and use relative path.} \tn % Row Count 11 (+ 3) % Row 17 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Never assume that your code will run from drive "C:". You may never know, some users may run it from network or from a "Z:".} \tn % Row Count 14 (+ 3) % Row 18 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always in the application start up, do some kind of "self check" and ensure all required files and dependencies are available in the expected locations. Check for database connection in start up, if required. Give a friendly message to the user in case of any problems.} \tn % Row Count 20 (+ 6) % Row 19 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always if a wrong value found in the configuration file, application should throw an error or give a message and also should tell the user what are the correct values.} \tn % Row Count 24 (+ 4) % Row 20 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always return empty collections instead of null.} \tn % Row Count 25 (+ 1) % Row 21 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always remove dead code.} \tn % Row Count 26 (+ 1) % Row 22 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always remove unnecessary using statements.} \tn % Row Count 27 (+ 1) % Row 23 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Filenames and directory names are PascalCase, e.g. MyFile.cs.} \tn % Row Count 29 (+ 2) % Row 24 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Where possible the file name should be the same as the name of the main class in the file, e.g. MyClass.cs.} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{General Guidelines (cont)}} \tn % Row 25 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{In general, prefer one core class per file.} \tn % Row Count 1 (+ 1) % Row 26 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{A maximum of one statement per line.} \tn % Row Count 2 (+ 1) % Row 27 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A maximum of one assignment per statement.} \tn % Row Count 3 (+ 1) % Row 28 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Indentation of 2 spaces, no tabs.} \tn % Row Count 4 (+ 1) % Row 29 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Column limit: 100.} \tn % Row Count 5 (+ 1) % Row 30 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{No line break before opening brace.} \tn % Row Count 6 (+ 1) % Row 31 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{No line break between closing brace and else.} \tn % Row Count 7 (+ 1) % Row 32 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Braces used even when optional.} \tn % Row Count 8 (+ 1) % Row 33 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Space after if/for/while etc., and after commas.} \tn % Row Count 9 (+ 1) % Row 34 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{No space after an opening parenthesis or before a closing parenthesis.} \tn % Row Count 11 (+ 2) % Row 35 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{No space between a unary operator and its operand. One space between the operator and each operand of all other operators.} \tn % Row Count 14 (+ 3) % Row 36 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Where possible, group interface implementations together.} \tn % Row Count 16 (+ 2) % Row 37 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Namespace using declarations go at the top, before any namespaces. using import order is alphabetical, apart from System imports which always go first.} \tn % Row Count 20 (+ 4) % Row 38 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Modifiers occur in the following order: public protected internal private new abstract virtual override sealed static readonly extern unsafe volatile async.} \tn % Row Count 24 (+ 4) % Row 39 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{For single line read-only properties, prefer expression body properties (=\textgreater{}) when possible.} \tn % Row Count 26 (+ 2) % Row 40 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{For everything else, use the older \{ get; set; \} syntax.} \tn % Row Count 28 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Data Types}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never use magic numbers or magic strings. Use constants or enums.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to only use var when the type is very obvious.} \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never compare floating point values using ==, !=, or Equals} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to make all your variables and return values strongly typed.} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always use the decimal type for currency data. Floating point types can lead to inconsistent comparisons and rounding.} \tn % Row Count 10 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Object Oriented Programming}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always separate concerns. Separate User Interface Logic, Business Logic, Data Logic, into separate projects} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always create methods to do only 'one job'. Do not combine more than one job in a single method, even if those jobs are very small.} \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always prefer composition over inheritance.} \tn % Row Count 7 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Avoid Premature Generalization. Create abstractions only when the intent is understood.} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Avoid creating methods that function differently based on the underlying type of the object.} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always declare all data members private.} \tn % Row Count 12 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always prevent instantiation of a class if it contains only static members.} \tn % Row Count 14 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always explicitly define a protected constructor on an abstract base class.} \tn % Row Count 16 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to make all types internal by default.} \tn % Row Count 17 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Try to use using statements instead of fully qualified type names.} \tn % Row Count 19 (+ 2) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Never hide inherited members with the new keyword.} \tn % Row Count 20 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always override the GetHashCode method whenever you override the Equals method.} \tn % Row Count 22 (+ 2) % Row 12 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always override the Equals method whenever you implement the == operator, and make them do the same thing.} \tn % Row Count 25 (+ 3) % Row 13 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always implement operator overloading for the equality (==), not equal (!=), less than (\textless{}), and greater than (\textgreater{}) operators when you implement IComparable} \tn % Row Count 29 (+ 4) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always create variants of an overloaded method to be used for the same purpose and have similar behavior.} \tn % Row Count 32 (+ 3) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Object Oriented Programming (cont)}} \tn % Row 15 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Always allow properties to be set in any order.} \tn % Row Count 1 (+ 1) % Row 16 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Never create a constructor that does not yield a fully initialized object} \tn % Row Count 3 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Comments}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Try to not check in commented code. Trust source control.} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Always put comments in English.} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}