code-readability

What is the best way to document data reader method calls?

When working with namespaces such as System.Data.Odbc or System.Data.OracleClient the various data reader methods generally require an integer corresponding to a column be provided to the functions (e.g. OracleDataReader.GetInt32). My question is this, what is the best way to work with these functions so that the code is fairly self-do...

Is Code For Computers or for People?

Ultimately, code compiles down (eventually) into instructions for a CPU. Code, however, (in my humble opinion) is for human beings to read, update, and interact with. This leads me to the following observation: Code that is unreadable by other engineers, even if it's functional, is bad code. With that in mind, what can this programmer...

Studies on optimal code width?

If you enable the "View Right Margin" in your IDE of choice, it is likely that it will default to 80 characters. I tend to change it to 120 for no reason other than it was the standard at a company I was with a few years back, and no other company has told me to do it differently. My question is, are there any studies that actually sho...

Gracefully avoiding NullPointerException in Java

Consider this line: if (object.getAttribute("someAttr").equals("true")) { // .... Obviously this line is a potential bug, the attribute might be null and we will get a NullPointerException. So we need to refactor it to one of two choices: First option: if ("true".equals(object.getAttribute("someAttr"))) { // .... Second option: S...

Most elegant way to deal with singles/plurals?

Say you're making a blog software, and want to show the number of comments an entry got. You might do it this way: [Entry title] [Content........] [ <?php print($numComments;) ?> Comments] Which might result in: [Entry title] [Content........] 5 Comments But if an entry had only 1 comment, I want the line to say 'Comment' rather th...

General programming - else or else if for clarity

In a situation where a variable could have two different values, and you do something if its one, something differnent if its the other, would you just do: if(myVariable == FIRST_POSSIBLE_VALUE) { ... } else { ... } or would you do: if(myVariable == FIRST_POSSIBLE_VALUE) { ... } else if (myVariable == SECOND_POSSIBLE_VALUE) { ... } ...

Why use base16 numbers in code when base10 numbers are more readable?

I have seen many people uses base16 numbers in code where a base10 number is more readable. Here is a c# code. byte[] b = new byte[0x1000]; For me the below is more readable, byte[] b = new byte[4096]; Is there any good reason for using base16 numbers or is it a matter of preference? ...

A better way to test the value of an Option?

I often find myself with an Option[T] for some type T and wish to test the value of the option against some value. For example: val opt = Some("oxbow") if (opt.isDefined && opt.get == "lakes") //do something The following code is equivalent and removes the requirement to test the existence of the value of the option if (opt.map(_...

Pattern to catch exception from sections of code (while not making eyes bleed)

I have a section of code that looks like this: try { classVar = functionCall(input, sEnum.First); classVar = functionCall(input, sEnum.Second); classVar = functionCall(input, sEnum.Third); } catch (Exception ex) { Debug.Assert(false, ex.ToString()); } However my exception dosent show which specific call it came from. The sta...

Patterns for declaring functions for greater readability

In C++ functions needed to be declared before they were called. This could be worked around with function signatures but for the most part this is no longer required in newer programming languages, C#, Python, ETC. However, while reading other peoples, code and when having to structure functions in a class, I find that I miss the consis...

Including commented Class declaration in implementation file

Hi All, Everyone knows the advantages of a more readable code. So in order to make my code more readable what i do normally is include the commented class declaration in the implementation file of that class. This way i need not have to browse through various include directories to go to the definition. So, Is this a good practice or ju...

"Number line" style of comparisons

I recently read in Code Complete that the recommended way of handling expressions that involve numbers is to order them like a number line. The book has 2 examples: if ( (MIN_ELEMENTS <= i) && (i <= MAX_ELEMENTS) ) if ( (i < MIN_ELEMENTS) || (MAX_ELEMENTS < i ) ) With the first example showing that i is between the min and max elem...

How can this 6 line method be refactored to be more readable?

Hi everyone, I'm trying to clean up this ridonkulously ugly method here, that's crying out for refactoring, but I'm not sure what kind of structure would do this best (i.e. a case statement, or simply a carefully formatted if then statements) At first glance, it looks like it would be an ideal place for a case statement with a few well...

Would VS2008 c++ compiler optimize the following if statement?

if (false == x) { ...} as opposed to: if (!x) { ... } and if (false == f1()) { ...} as opposed to: if (!f1()) { ... } I think the if(false == ... version is more readable. Do you agree, or have another trick you can propose? Will it be just as fast? Thanks. This is why I do not like !x: if (25 == a->function1(12345, 6789) &&...

Do I need to have meaningful names for loop control variables?

Code Complete book suggested, it is a good practice to use meaningful names for loop control variables. For example: for(int month=0; month < MONTHS_PER_YEAR; month++){ // processing } Instead of single letters for example for(int i=0; i < MONTHS_PER_YEAR; i++){ // processing } I follow this practice and use meanin...

where to put & in php?

i wonder where i should put the & properly. $b =& $a; or $b = &$a; ...

Is there any tool to standardize format of C++ code?

Hello, all :) I'm looking for a tool that works on Windows to reformat some C++ code in my codebase. Essentially, I've got some code I wrote a while ago that I'd like to use, but it doesn't match the style I'm using in a more recent project. What's the best way to reformat C++ code in a standard manner? Billy3 ...

How can I hide tracing code in Visual Studio IDE C# ?

As I'm starting to put more tracing in my code, i'm realizing it adds a lot of clutter. I know Visual Studio allows you to hide and reveal code, however, i'd like to be able group code into "tracing" code and then hide it and reveal at will as i'm reading the code. I suppose it could do this either per file or per class or per function. ...

Best ways to format LINQ queries.

Before you ignore / vote-to-close this question, I consider this a valid question to ask because code clarity is an important topic of discussion, it's essential to writing maintainable code and I would greatly appreciate answers from those who have come across this before. I've recently run into this problem, LINQ queries can get prett...

What C# language features help you to reduce lines of code and improve readability?

I came across a C# language feature today courtesy of ReSharper, the ?? operator. This helped make the code even more concise than my initial attempt. See below for iteration in improving lines/length/readability of code. A first attempt could be something like.. if (usersEmail == null) userName = firstName; else userName = usersEm...