coding-style

Granularity of Paradigm Mixing

When using a multi-paradigm language such as Python, C++, D, or Ruby, how much do you mix paradigms within a single application? Within a single module? Do you believe that mixing the functional, procedural and OO paradigms at a fine granularity leads to clearer, more concise code because you're using the right tool for every subproble...

What is the best way to do input validation in C++ with cin?

My brother recently started learning C++. He told me a problem he encountered while trying to validate input in a simple program. He had a text menu where the user entered an integer choice, if they entered an invalid choice, they would be asked to enter it again (do while loop). However, if the user entered a string instead of an int, t...

"Interfaces" in Python: Yea or Nay?

So I'm starting a project using Python after spending a significant amount of time in static land. I've seen some projects that make "interfaces" which are really just classes without any implementations. Before, I'd scoff at the idea and ignore that section of those projects. But now, I'm beginning to warm up to the idea. Just so we...

Do you code variables in your language?

I am just working on a project where the library has an object with the property color, however being British I always use colour when writing variables and properties. I also just found some legacy code where the British developer used color in a variable name. Is American English the default for development now? ...

Are clusters discouraged in LabVIEW?

I found this comment in the LabVIEW instrument driver guidelines (section 6.2): If you need more terminals than the recommended patterns, reconsider the grouping of the controls and indicators on the VI. Except for error in and error out, avoid using clusters to minimize the number of terminals. Clusters often require the user to unb...

Best way to integrate StyleCop with TFS CI

I've been doing research on how to enable source analysis for the project I'm working on and plan to use StyleCop. The setup I have is a TFS Server for source control, using TFS Continuous Integration. I want to enable source analysis for CI builds and daily builds run on the build machine, and not only for those run on developers' machi...

How to justify to your colleagues that they produce crappy code?

I am finding somewhat difficult to carry on working in my current job. The codebase has become a bit wild lately (but definitely not the worse I've seen), and I'm having a hard time dealing with some parts of the code. I could be stupid, but most likely it's just that it demotivates me a lot to start working on something that is hard to...

Coding standards and line length

Every coding standard I've ever seen has a recommended or absolute limit on number of characters in a line. There are various ways of working within this limitation, but I've not seen any specific guidance in this regard. Obviously, if possible, don't write excessively long lines. But what if that's not practical? How should long lines...

C++ code in header files

My personal style with C++ has always to put class declarations in an include file, and definitions in a .cpp file, very much like stipulated in Martin York's answer to C++ Header Files, Code Separation. Admittedly, part of the reason I like this style probably has to do with all the years I spent coding Modula-2 and Ada, both of which h...

What is the best way to print a table with delimiters in Python

I want to print a table mixed with string and float values, as tab delimited output printout. Sure I can get the job done: >>> tab = [['a', 1], ['b', 2]] >>> for row in tab: ... out = "" ... for col in row: ... out = out + str(col) + "\t" ... print out.rstrip() ... a 1 b 2 But I have a feeling there is a b...

How bad is it to put javascript outside of the header?

The question pretty much already says everything. I'm starting to add some features to my weekend project. It's a small app for me and a couple of friends, as we're exchange students it's kinda useful for us. But the thing is the following, I'm doing this in php and structuring everything with includes so i can get the code separated. ...

Java PMD warning on non-transient class member

On line: private boolean someFlag; I get the following PMD warning: Found non-transient, non-static member. Please mark as transient or provide accessors. Can someone please explain why this warning is there and what it means? (I understand how to fix it, I don't understand why it's there...) I'm getting this on many other memb...

Best way to run a PHP script conditionally on user login

I might be nitpicking, but is it better to do this: if ($loggedin) { // normal process } else { header('Location: login.php'); } Or this: if (!$loggedin) { header('Location: login.php'); exit(); } // normal process Or does it just not matter? ...

Lance Hunt's C# Coding Standards - enum confusion

My team has recently started using Lance Hunt's C# Coding Standards document as a starting point for consolidating our coding standards. There is one item that we just don't understand the point of, can anyone here shed any light on it? The item is number 77: Always validate an enumeration variable or parameter value before con...

How to concisely cascade through multiple regex statements in Python

My dilemma: I'm passing my function a string that I need to then perform numerous regex manipulations on. The logic is if there's a match in the first regex, do one thing. If no match, check for a match with the second and do something else, if not check the third, and so forth. I could do something like this: if re.match('regex1', st...

How do you structure your comparison functions?

I frequently encounter situations, especially with sorting in C++, where I am comparing a series of fields in order to compare a larger structure. A simplified example: struct Car{ Manufacturer make; ModelName model; Year year; }; bool carLessThanComparator( const Car & car1, const Car & car2 ){ if( car1.make < car2.ma...

What's the best way to layout a C# class?

Is there a standard way of laying out a C# file? As in, Fields, then Properties, then Constructors, etc? Here's what I normally do, but I'm wondering if there's a standard way? Nested Classes or Enums Fields Properties Events Constructors Public Methods Private Methods Do people group their fields together, or do they put them with...

Make ReSharper respect your preference for code order.

Related to my other question: What's the best way to layout a C# class? Is there a way in ReSharper to define the order you want your members to be in, so that ReSharper will maintain it? ...

How many lines of code should a function/procedure/method have?

I've recently been given the unenviable task of reviewing poor code written by another developer and documenting the bad practices. (This is all for the purposes of getting out of paying for the developer's work rather than any altruistic reason, of course!) The reviewed code has several procedures that are many lines of code - the long...

Silverlight layout Best Practices

I'm writing a fairly big interface using Silverlight. As I progress, the xaml file is getting fairly big and is becoming proportionally uglier. Questions Are there any resources out there to make the xaml more readable? For example, how would I display the order of attributes (e.g. height and Width first) so that it looks the most ti...