coding-style

Define enums within a method in C#?

半腔热情 提交于 2019-12-10 00:40:23
问题 I have mainly a C++ background and I am learning C#. So, I need some help with C# idioms and style. I am trying to write, in C#, a small text-file parsing method in which I need a simple state variable with three states. In C++ I would declare an enum like this for the state variable: enum { stHeader, stBody, stFooter} state = stBody; ...and then use it in my parsing loop like this: if (state == stHeader && input == ".endheader") { state = stBody; } In C# I realize that it is not possible to

Is there a code style enforcer?

时光总嘲笑我的痴心妄想 提交于 2019-12-09 17:53:51
问题 I'm working with websites written in PHP, along with many other programmers, and sometimes I have to deal with really awful code. Indentation totally messed up, curly braces in the wrong places, terrible whitespace use, it really hurts my eyes and actually makes my work harder and take longer. Is there a tool where you can specify your coding style and then it applies it to files? I'm sure there must be such functions in different IDEs, but I need an independent tool as I code with simple

What is the best practice for writing sql queries inside c# code

雨燕双飞 提交于 2019-12-09 17:17:41
问题 On my current project I'm using SQL CE. Since it doesn't have support for stored procedures I have to write sql queries inside repository. Option 1: StringBuilder query = new StringBuilder(); query.Append("SELECT"); query.Append(" c.CUSTOMER_ID,"); query.Append(" COALESCE (c.FIRST_NAME, '') + ' ' + COALESCE (c.LAST_NAME, '') AS FULL_NAME"); query.Append(" ct.NAME as CUSTOMER_TYPE"); query.Append("FROM "); query.Append(" CT_CUSTOMER_TYPE AS ct INNER JOIN CUSTOMER AS c ON ct.CUSTOMER_TYPE_ID =

Pimp my LINQ: a learning exercise based upon another post

雨燕双飞 提交于 2019-12-09 16:39:40
问题 I decided to try out LINQ for the first time to try and solve this question. The results of my first foray into the wonderful world of LINQ looked like this: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<string> list = new List<string>() { "fred-064528-NEEDED1", "xxxx", "frederic-84728957-NEEDED2", "sam-028-NEEDED3", "-----", "another-test" }; var result = from s in

Should I test if equal to 1 or not equal to 0?

旧城冷巷雨未停 提交于 2019-12-09 15:53:55
问题 I was coding here the other day, writing a couple of if statements with integers that are always either 0 or 1 (practically acting as bool s). I asked myself: When testing for positive result, which is better; testing for int == 1 or int != 0 ? For example, given an int n , if I want to test if it's true , should I use n == 1 or n != 0 ? Is there any difference at all in regards to speed, processing power, etc? Please ignore the fact that the int may being more/less than 1 / 0 , it is

Python bracket convention [closed]

血红的双手。 提交于 2019-12-09 13:11:17
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . What do you think is the convention that is mostly used when writing dictionary literals in the code? I'll write one possible

Using AStyle in Vim

让人想犯罪 __ 提交于 2019-12-09 11:26:28
问题 I am trying to get AStyle working with Vim so that I can use the "=" key to re-indent various sections of code. For example, I'd like to be able to type my usual =iB to indent the current block of code using AStyle rather than the built in indenter. I tried just setting equalprg=astyle in my vimrc, but the problem is that astyle only receives the selected block but thinks that it's receiving a whole file. Therefore, the indentation is completely off when I try to only indent a nested class. I

Is using assert() for production not favored over if..else.. blocks?

依然范特西╮ 提交于 2019-12-09 11:17:36
问题 I am finding that using assert(...) makes my code shorter and easier to read, as opposed to lengthy if..else.. blocks. However, are there good technical reasons not to use assert(...) in shipping code, when it does the same thing as testing a return value while using less code? 回答1: Having read this article I will share my beliefs about assert : Yes it's fine to use assert when something absolutely should meet the condition you are asserting. Many languages allow you to raise custom errors

What to do when project coding standards conflicts with unit test code coverage?

吃可爱长大的小学妹 提交于 2019-12-09 11:10:39
问题 I'm working on a personal project for the learning experience, and also at the same time to implement a decent body of code. Part of this education, and making it a decent body of code, is unit testing. I have recently dived into PHPUnit and its code-coverage tools. I have encountered a situation with a particular implementation where the coding standard used causes code-coverage to be lost. In this particular instance breaking the coding standard used causes a jump from 88% to 94% in code

Should I commit cosmetic changes?

青春壹個敷衍的年華 提交于 2019-12-09 07:33:23
问题 There are minor coding style changes that I often want to commit to the source control, but now the change log is full of those changes that do not affect code functionality. What should I do next time I have to fix minor things like: Remove and sort usings (in .NET, imports in python, includes in c++) Correct indentation, spacing and line breaks 回答1: If you are changing the code file, I don't really see why you wouldn't want to commit and share those changes. If you don't you run the risk