What makes code "cleaner"? What makes the difference between readable code and very readable code?
It can be very painful when needing to modify a piece of code in an application that you never spec'd out or wrote. But it doesn't have to be this way. By following some of these better programming tips your code can be easily read and easily modified by any developer at any time.
Refactoring is all about making code easier to understand and cheaper to modify without changing its behavior.
You should generally be looking for ways to simplify your code (e.g. removing heavily-nested case statements). As a minimum, look for the most complicated method you have and check whether it needs simplifying.
In Visual Studio, there is built-in support for Cyclomatic Complexity analysis.
Too often, developers are writing a line of code, and they forget that little bit of syntax they need to do what they want. In that case, they usually end up googling it to find the documentation and then copy and paste it into their code. That process is a pain because it wastes valuable dev time. They might also ask another dev for help.
Not to worry, AI pair programming is here to save the day!
Code duplication is a big "code smell" that harms maintainability. You should keep an eye out for repeated code and make sure you refactor it into a single place.
One of the major issues people had back in the day with ASP (before ASP.NET) was the prevalence of "Spaghetti Code". This mixed Reponse.Write() with actual code.
When moving through the different stages of testing i.e. from internal testing, through to UAT, you should suffix the application name with the appropriate stage:
It is not a good idea to have spaces in a folder or file name as they don't translate to URLs very well and can even cause technical problems.
Instead of using spaces, we recommend:
- kebab-case - using dashes between words
Other not recommended options include:
- CamelCase - using the first letter of each word in uppercase and the rest of the word in lowercase
- snake_case - using underscores between words
For further information, read Do you know how to name documents?
Try to avoid problems in if-statements without curly brackets and just one statement which is written one line below the if-statement. Use just one line for such if-statements. If you want to add more statements later on and you could forget to add the curly brackets which may cause problems later on.
Try to avoid Double-Negative Conditionals in if-statements. Double negative conditionals are difficult to read because developers have to evaluate which is the positive state of two negatives. So always try to make a single positive when you write if-statement.
Strings should be @-quoted instead of using escape character for "\". The @ symbol specifies that escape characters and line breaks should be ignored when the string is created.
- Do you refactor your code and keep methods short?
- Do you know when functions are too complicated?
- Do you use AI pair programming?
- Do you look for duplicate code?
- Do you maintain separation of concerns?
- Do you use the testing stage, in the file name?
- Do you avoid using spaces in folder and file names?
- Do you know how to avoid problems in if-statements?
- Do you avoid Double-Negative Conditionals in if-statements?
- C# Code - Do you use string literals?
- Do you add the Application Name in the SQL Server connection string?
- Do you know how to use Connection Strings?
- Do you store your secrets securely?
- Do you share your developer secrets securely?
- Do you avoid clear text email addresses in web pages?
- Do you always create suggestions when something is hard to do?
- Do you avoid casts and use the "as operator" instead?
- Do you avoid Empty code blocks?
- Do you avoid logic errors by using Else If?
- Do you avoid putting business logic into the presentation layer?
- Do you avoid "UI" in event names?
- Do you know when to use switch statement instead of if-else?
- Do you avoid validating XML documents unnecessarily?
- Do you change the connection timeout to 5 seconds?
- Do you declare member accessibility for all classes?
- Do you do your validation with Return?
- Do you expose events as events?
- Do you follow the boy scout rule?
- Do you follow naming conventions for your Boolean Property?
- Do you format "Environment.NewLine" at the end of a line?
- Do you have the time taken in the status bar?
- Do you import namespaces and shorten the references?
- Do you initialize variables outside of the try block?
- Do you know that Enum types should not be suffixed with the word "Enum"?
- Do you know how to use Connection Strings?
- Do you know what to do with a work around?
- Do you know when to use named parameters?
- Do you know where to store your application's files?
- Do you name your events properly?
- Do you pre-format your time strings before using TimeSpan.Parse()?
- Do you know not to put Exit Sub before End Sub? (VB)
- Do you put optional parameters at the end?
- Do you refer to form controls directly?
- Do you know how to format your MessageBox code?
- Do you reference websites when you implement something you found on Google?
- Do you store Application-Level Settings in your database rather than configuration files when possible?
- Do you suffix unit test classes with "Tests"?
- Do you use a helper extension method to raise events?
- Do you use a regular expression to validate an email address?
- Do you use a regular expression to validate an URL?
- Do you use Enums instead of hard coded strings?
- Do you use Environment.NewLine to make a new line in your string?
- Do you use good code over backward compatibility?
- Do you use Public/Protected Properties instead of Public/Protected Fields?
- Do you use resource file to store all the messages and globlal strings?
- Do you use resource file to store messages?
- Do you use String.Empty instead of ""?
- Do you use "using" declaration instead of use explicitly "dispose"?
- Do you warn users before starting a long process?
- DRY - Do you wrap the same logic in a method instead of writing it repeatedly whenever it's used?
- Do you know the difference between a 'smart' and a 'clever' developer?
- Use Enum Constants instead of Magic numbers?
- Do you avoid using magic string when referencing property/variable names
- Do you use null condition operators when getting values from objects
- Do you use string interpolation when formatting strings
- Do you document "TODO" tasks?
- Do you monitor your application for vulnerabilities?
- Do you keep your code consistent using .editorconfig?
- Do you know how to read source code?
- Do you check before installing 3rd party libraries?
- Do you keep a package audit log?
- Do you use package managers appropriately?
- Do you know the best package manager for Node?
- Do you use local copies to resolve race condition?
- C# Code - Do you use primary constructors to keep your code clean?
- C# Code - Do you use collection expressions to keep your code clean?
- Reading Source Code - Do you understand the importance of interfaces and abstract classes?
- Do you use "var"?
- Code - Do you handle all possible scenarios?
- Do you know how to get code line metrics?
- Do you know how to use single codebase for multiple domains with TinaCMS and Next.js?