A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::open($save_path, $name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 132

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 290

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::read($session_id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 164

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::write($session_id, $session_data) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 233

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::destroy($session_id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 313

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: 8192

Message: Return type of CI_Session_files_driver::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

Filename: drivers/Session_files_driver.php

Line Number: 354

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 296

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Session ini settings cannot be changed after headers have already been sent

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Session save handler cannot be changed after headers have already been sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Session cannot be started after headers have already been sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home2/umernaze/public_html/coderanks/application/controllers/Article.php
Line: 7
Function: __construct

File: /home2/umernaze/public_html/coderanks/index.php
Line: 317
Function: require_once

Logo
Code Ranks ×

C# Best Practices

22/06/2019  .   3 minutes, 32 seconds to read  .   Posted by Admin
#c# #best-practices
Naming Conventions
  1. Pascal Case
  2. Camel Case
Pascal Case

The first characters of all words are Upper Case and other characters are lower case.

Example
CaseHistory

Camel Case

The first letter is in lowercase and the first letter of every subsequent concatenated word is in caps.

OR

The first character of all words, except the first word, is Upper Case and other characters are lower case.

Example
businessCase

Naming Conventions and Standards

Use Pascal casing for Class names.

Example
public class BusinessCase  
{  
    //.........  
}  
 Use Pascal casing for Method names.

Example
void AutoComplete(string name)  
{  
    // ......  
}  
Use Camel casing for variables and method parameters.

Example
int totalCount = 0;  
void AutoComplete(string name)  
{  
   string fullMessage = "Hello " + name;  
}  
Use Meaningful, descriptive words to name variables. Do not use abbreviations.

Example

Good

  1. string address;  
  2. int salary;  

Bad 

  1. string nam;  
  2. string addr;  
  3. int sal;  
Avoid using single-character variable names like i, n, s, etc. Expect in the loops. Use names like index, temp.

Example
Avoid using underscores (_) for local variable names.

Use 'is' keyword for boolean functions
private bool _isFinished ();

The prefix for the UI elements should be appropriate so that you can identify them from the rest of the variables. A brief list is given below.

Example

Control Prefix
Label lbl
TextBox txt
DataGrid dtg
Button btn
ImageButton imb

Use Pascal Case for file names.

Indentation and Spacing

Use TAB for indentation. Do not use SPACES. Define the Tab size as 4.

Comments should be on the same level as the code (use the same level of indentation).

Example

Good
// Format a message and display  
string myMessage = " C# best practices ";  
DateTime currentTime = DateTime.Now;  
string infoMessage = myMessage + ", the time is : " + currentTime.ToShortTimeString();  
MessageBox.Show( infoMessage );  

Bad

// Format a message and display  
string myMessage = " C# best practices ";
DateTime currentTime = DateTime.Now;  
string infoMessage = myMessage + ", the time is : " + currentTime.ToShortTimeString();  
MessageBox.Show ( infoMessage ); 

The code should be properly formatted.

separate logical groups of code Using one blank line.

Example

Good

bool ShowMessage(string name) {  
    string fullName = "Hello " + name;  
    DateTime currentTime = DateTime.Now;  
    string message = fullName + ", the time is : " + currentTime.ToShortTimeString();  
    MessageBox.Show(message);  
    if (...) {  
        // Write your code here  
        // ...  
        return false;  
    }  
    return true;  
}  
Bad
bool ShowMessage(string name) {  
        string fullName = "Hello " + name;  
        DateTime currentTime = DateTime.Now;  
        string message = fullName + ", the time is : " + currentTime.ToShortTimeString();  
        MessageBox.Show(message);  
        if (...) {  // code here  
                    // ...   return false;  
        }  
        return true; 
}
 
Format the code using curly braces and separate the method with new line.

Example

Good
if ( ... )  
{  
   // write your code here  
} 
Bad
if ( ... ) {  
// Write your code here }  
Always use a single space before and after operators and braces.

Example

Good
if(showResult)  
{  
   for ( int j = 0; j < 10; j++ )  
   {  
       //  
    }  
}  
Bad
if(showResult==true)  
{  
    for(int j= 0;j<10;j++)  
    {  
       //  
    }  
}  
Superb Programming Practices

Method name should clarify the meaning. Do not use misleading names. If the method name is clear then there is no need of documentation.

Example

Good
void SaveStudentDetails (string studentDetails )  
{  
   // Save the student details.  
}  
Bad
// This method will save the student details.  
void SaveDetails (string student )  
{  
   // Save the student details.  
}  
A method should do only one job at a time. Do not use it for more than one job.

Example

Good
//Save student details  
SaveStudentDetails();  
//Send email to user that student details is added successfully.  
SendEmail();  
Bad
//Save student details  
Public void SaveStudentDetails()  
{  
   // First task  
   //Save student details  
   //Second task  
   // Send emails  
}  
Make sure string comparisons convert string to uppercase or lowercase for comparison.

Example

Good
If(UserName.ToLower()=="tom")  
{  
   // write yor logic here  
}  

Bad

If(UseName=="TOm")  
{  
   //  
}  
Comments
  • Do not write comments for every line of code and every variable declared.
  • Use // or /// for comments. Avoid using /* … */
  • Write comments where is it required. But they should be well readable and limited. If all variables and method names are perfectly meaningful, then there is no need for any comments.
  • If you initialize a numeric variable to a special number other than 0, -1, etc, document the reason for choosing that value.
  • Make sure to perform spelling check on comments and make sure proper grammar and punctuation is used.
Enum

Always use a singular noun for defining enum.

Example
Enum Mailtype  
{  
   Subject,  
   Body,  
   PlaneText  
}  
Interface

Always use the letter "I" as a prefix with the name of an interface. After the letter I, use PascalCase.

Example
public interface IMultiply  
{  
   int Multiplication();  
}