Coding Standards & Tips: Silverlight application development
XAML
- Namespace should be Pascal case
- All XAML / Control name / Class name should be in PascalCase
- All namespace name should be in camelCase
- e.g.: xmlns:customControls="clr-namespace:Silver.Controls"
- If there is a need to specify a name for any control in the XAML, Specify it in PascalCase
- <UserControl.Resources>
<viewModel:LoginViewModel x:Key="ViewModel"/>
</UserControl.Resources>
- Suggestion: For a user control or a page DataContext can be specified in the XAML itself
- e.g.: <Grid DataContext="{StaticResource ViewModel}">
- Styles should be separated and should be placed in a ResourceDictionary
- Resource dictionary can be split into separate files to make it more readable
- e.g.: CommonStyles.xaml, HomePageStyles.xaml, NavigationBarStyles.xaml etc
C#
- Use Folders to split the application structure
- Views folder will have XAML files
- ViewModel folder will have corresponding CS files
- Styles can be placed in Assets folder
- Use a Common Library to create custom control’s methods and public variables that will be used throughout the application.
- Methods / variables that are used in more than one form can be placed in this file
- All the entities can be placed in this file, so that it can be accessed from any project
- Methods (Both Private and Public) should be in PascalCase
- Private variables in Methods and Parameters should be in camelCase
- Private variables can be in camelCase with an underscore ‘_ ‘ prefix or use “this” keyword to represent private members
- Constants (Both Private and Public) should be in PascalCase
- Public Properties, Variables should be in PascalCase
- Use setter as private to a property in case where property will only get updated from the class
- Do not use Setter if it is not required
- Use meaningful property names, variable names, and method names
- Name should give an idea about the goal of the Method / Variable
- Make code commenting a practice, Use Three slash (///) code commenting
- Use IEnumerable<ClassName> on data transfer between methods, temp storage
- Use ObservableCollection<ClassName> only when there is an update in the collection and this is to be reflected in the view, else use a List<ClassName>
- All files (Images/Text/XML) should be loaded from the server to reduce the XAP file size
- Remove all unwanted Using statements from the class
- Use LINQ to reduce code
- Use less Try Catch Statements, instead try to avoid the error by giving proper conditions
- To make code more readable, use white space between braces of conditions, statements etc
- Try to make Zero code (Logic) in the XAML.CS (if there are no animations)
- I believe not to use underscore ‘_’ between the event methods. i.e. Click event for a button control is normally represented as Button_Click, But it would be better if using OnButtonClick. Since there is a ‘On’ prefix, there is more readability and Methods of the Event can be separated easily.
Many of the things mentioned above are from Coding Standards by Brad Adams, but some of them are from my experience.
Comments
Post a Comment