2016-05-31

About-BestPractices

Code readability is a universal subject in the world of computer programming. It's one of the first things we learn as developers. This article will detail the fifteen most important best practices when writing readable code.
Diomidis Spinellis, author of Code Quality: The Open Source Perspective, lists the most important rules for writing sparkling code. Follow them, and your code will look professional, live long, grow smoothly, and earn your colleagues’ love (rather than swearing).

Best Practice List

  1. Use Naming Conventions
  2. Create Descriptive Names: Use long descriptive names, like complementSpanLength, to help yourself, now and in the future, as well as your colleagues to understand what the code does. The only exception to this rule concerns the few key variables used within a method's body, such as a loop index, a parameter, an intermediate result, or a return value.
  3. Use Commenting Conventions
  4. Commenting & Documentation: IDE's (Integrated Development Environment) have come a long way in the past few years.
  5. Use Coding Conventions
  6. Consistent Indentation: I assume you already know that you should indent your code. However, it's also worth noting that it is a good idea to keep your indentation style consistent.
  7. Avoid Obvious Comments: Commenting your code is fantastic; however, it can be overdone or just be plain redundant.
  8. Code Grouping: More often than not, certain tasks require a few lines of code. It is a good idea to keep these tasks within separate blocks of code, with some spaces between them.
  9. Consistent Naming Scheme: PHP itself is sometimes guilty of not following consistent naming schemes. First of all, the names should have word boundaries like camelCase (First letter of each word is capitalized, except the first word) or underscores (Underscores between words, mysql_real_escape_string).
  10. Be Consistent: Do similar things in similar ways. If you're developing a routine whose functionality resembles that of an existing routine, use a similar name, the same parameter order, and a comparable structure for the code body.
  11. Don't Overdesign: Keep your design focused on today's needs. Your code can be general to accommodate future evolution, but only if that doesn't make it more complex. Don't create parameterized classes, factory methods, deep inheritance hierarchies, and arcane interfaces to solve problems that don't yet exist—you can't guess what tomorrow will bring.
  12. Split Your Code into Short, Focused Units: Every method, function, or logical code block should fit on a reasonably-sized screen window (25–50 lines long). If it's longer, split it into shorter pieces. An exception can be made for simple repetitive code sequences.
  13. Use Efficient Data Structures and Algorithms: Simple code is often more maintainable than equivalent code hand-tuned for efficiency. Fortunately, you can combine maintainability with efficiency by utilizing the data structures and algorithms provided by your programming framework. Use maps, sets, vectors, and the algorithms that work on them, and your code will be clearer, more scalable, faster, and memory-frugal.
  14. Use Framework APIs and Third-Party Libraries: Learn what functionality is available through an API in your programming framework, and also what's commonly available through mature, widely adopted third-party libraries. Libraries supported by your system's package manager are often a good bet. Use that code, resisting the temptation to reinvent the wheel.
  15. DRY Principle: DRY stands for Don't Repeat Yourself. Also known as DIE: Duplication is Evil.
  16. Avoid Deep Nesting: Too many levels of nesting can make code harder to read and follow.
  17. Limit Line Length: Our eyes are more comfortable when reading tall and narrow columns of text.
  18. Check for Errors and Respond to Them: Routines can return with an error indication, or they can raise an exception. Deal with it. Don't assume that a disk will never fill up, your configuration file will always be there, your application will run with the required permissions, memory-allocation requests will always succeed, or that a connection will never time out.
  19. Include Unit Tests: The complexity of modern software makes it expensive to deploy a system and difficult to test it as a black box. A more productive approach is to accompany every small part of your code with tests that verify its correct function. This approach simplifies debugging by allowing you to catch errors early, close to their source. Unit testing is indispensable when you program with dynamically typed languages such as Python and JavaScript, because they'll only catch at run-time any errors that that a statically typed language such as Java, C#, or C++ would catch at compile time. Unit testing also allows you to refactor the code with confidence.
  20. Keep Your Code Portable: Unless you have some compelling reason, avoid using functionality that's available only on a specific platform or framework. Don't assume that particular data types (such as integers, pointers, and time) are of a given width (for example, 32 bits), because this differs between various platforms. Store the program's messages separately from the code, and don't hard-code cultural conventions such as a decimal separator or date format.
  21. Make Your Code Buildable: A single command should build your code into a form that's ready for distribution. The command should allow you to perform fast incremental builds and run the required tests. To achieve this goal, use a build automation tool like Make, Apache Maven, or Ant. Ideally, you should set up a continuous integration system that will check, build, and test your code every time you make a change.
  22. Put Everything Under Version Control: All elements of your system—code, documentation, tool sources, build scripts, test data—should be under version control.
  23. Avoid Security Pitfalls: Modern code rarely works in isolation. Therefore it will inevitably risk becoming the target of malicious attacks. They don't have to come from the Internet; the attack vector could be data fed into your application. Depending on your programming language and application domain, you might have to worry about buffer overflows, cross-site scripting, SQL injection, and similar problems.
  24. File and Folder Organization: Technically, you could write an entire application code within a single file. But that would prove to be a nightmare to read and maintain.
  25. Consistent Temporary Names: Normally, the variables should be descriptive and contain one or more words. But, this doesn't necessarily apply to temporary variables. They can be as short as a single character.
  26. Capitalize SQL Special Words: Database interaction is a big part of most web applications. If you are writing raw SQL queries, it is a good idea to keep them readable as well.
  27. Separation of Code and Data: This is another principle that applies to almost all programming languages in all environments. In the case of web development, the "data" usually implies HTML output.
  28. Alternate Syntax Inside Templates: You may choose not to use a fancy template engine, and instead go with plain inline PHP in your template files.
  29. Object Oriented vs. Procedural: Object oriented programming can help you create well structured code. But that does not mean you need to abandon procedural programming completely. Actually creating a mix of both styles can be good.
  30. Read Open Source Code: Open Source projects are built with the input of many developers. These projects need to maintain a high level of code readability so that the team can work together as efficiently as possible.

2016-05-25

About-VisualStudio2015-Extensions

VS2015 Update 2

It is better to have the latest visual studio with its companion extension for web development

VS2015 Extensions

Web Extension Pack The easiest way to set up Visual Studio for the ultimate web development experience.
Web Essentials Web Essentials is very well-named, it's essential, there is a whole host of features that Web Essential adds to Visual Studio, from Browser Link to its amazing addition to CSS intellisense and zen coding and more.
Bootstrap Snippet Pack The same thing as jQuery snippet, but this time it's for Bootstrap, it makes your life a lot easier and you don't have to look for examples on the web anymore, just type "<bs-Something" and you're good to go.
CssCop I'm not much of a CSS guru and I mostly rely on CssCop to point out my CSS mistakes, CssCop can check your Css based on set of rules and make sure you are following the best practices and avoid nasty mistakes.
jQuery Code Snippets This one is pretty self explanatory, it extends the Visual Studio jQuery snippets, and you don't need to spend your time typing $.ajax and what comes after it, it makes your jQuery coding speed a lot faster.
Node.js Tools for Visual Studio-NTVS NTVS is a free, open source plugin that turns Visual Studio into a Node.js IDE. It is designed, developed, and supported by Microsoft and the community.
ASP.NET MVC Boilerplate Are you sick and tired of adding must use packages and tools like elmah, glimpse, etc. to your project every time you want to start a project? Well, ASP.NET MVC Boilerplate addresses this issue, it is a template with all the packages and components that we use in every project, but it is not in default Visual Studio template.
SideWaffle Template Pack Another amazing extension, it adds tons of templates to the Visual Studio template, from templates for creating jQuery plugin to Angularjs Project template, it also makes creating custom templates a whole lot easier.
JSLint.NET JsLint is a code quality and validation tool for JavaScript, but this particular extension is a wrapper for Douglas Crockford's JSLint that bring this amazing tool to .NET.
JavaScript Map Parser Extension for Visual Studio that shows a map for JavaScript code in a separate panel.
Copy As HTML Copy selected code in HTML format while preserving syntax highlighting, indentation, background colour and font. Options: Max Height, Title Block, Alternating Lines, Line Numbers, Wrap Lines, Un-indent, Background Colour, Class Names. Converts RTF, outputted by VS, into HTML.
Image Optimizer Uses industry standard tools to optimize any JPEG, PNG and Gifs - including animated Gifs. Can do both lossy and lossless optimization.
Developer Assistant Searching for code sample couldn't be any easier with this extension, you can see and use sample code right from your code as you are typing.
Ref12 Ref12 is a pretty simple but necessary extension, it gives you the ability to navigate to the actual source code of the .NET Framework rather than just meta data.
Code Cracker for C# In the past, only big companies like JetBrain (Creator of Resharper) could build a code analysis feature and tooling for Visual Studio, because doing so was not an easy task and required a lot of work and money, but with release of Roslyn compiler and set of API that it provides for querying and working with compiler, it is easier to build a code analysis tool for C# and VB.
Refactoring Essentials Another code analyzer for C# and VB.NET is Refactoring Essential, it is available as both extension and Nuget package that you can use on project by project bases. https://www.nuget.org/packages/RefactoringEssentials/ Install-Package RefactoringEssentials
ResXManager Manage localization of all ResX-Based resources in one place. Shows all resources of a solution and let's you edit the strings and their localizations in a well-arranged data grid.
Visual Studio Spell Checker This extension spellchecks your comments, strings, and plain text, it's pretty non-intrusive and doesn't get in the way of your actual code and doesn't mix itself with error highlighting.
Productivity Power Tools A lot of native Visual Studio capability that we see today was originally available as an addition in this extension and eventually got integrated in Visual Studio, but this extension still has a lot of great features.
Snippet Designer Typing repetitive stuff pretty much bores me to death, I often create snippets for these kind of codes, snippet designer helps you do it easily and painlessly.
Chutzpah Test Adapter for the Test Explorer Chutzpah adapter for the Visual Studio Unit Test Explorer. Chutzpah is an open source JavaScript test runner which enables you to run JavaScript unit tests from the command line and from inside of Visual Studio.
Chutzpah Test Runner Context Menu Extension Chutzpah is an open source JavaScript test runner which helps you integrate JavaScript unit testing into your website. It enables you to run JavaScript unit tests from the command line and from inside of Visual Studio.
CodeMaid CodeMaid is an open source Visual Studio extension to cleanup and simplify our C#, C++, F#, VB, PHP, PowerShell, R, JSON, XAML, XML, ASP, HTML, CSS, LESS, SCSS, JavaScript and TypeScript coding.
VSColorOutput Color output for build and debug windows.

2016-05-19

CSharp_BitCountAlgorithm


Bitcount algorithm in C#

In this article, we will learn:

  • Sparsh bitcount algorithm
  • Iterated bitcount algorithm
  • Precomputed bitcount algorithm
Bit counting is useful when using compact data structures in memory with bits. In this article, we will discuss various ways of counting total no of bits.

Sparsh bitcount algorithm:

This is a simple and fast algorithm that walks through all the bits that are set to one. It is static. It does not rely on saving state.

Output:

Sparsh_bitcount

Iterated bitcount algorithm:

This bit count is slow,simple and reliable.

Output:

Sparsh_bitcount

Precomputed bitcount algorithm:

This program demonstrates the use of a precomputed bitcount lookup table. The InitializeBitcounts method uses a logical method to precompute the bits in the table based on how the binary representation changes.

Output:

Sparsh_bitcount

SPECIAL THANKS TO CSHARPSTAR 

HTMLCode

HTMLCode Content