coding-style

When to use log level WARN vs ERROR?

Given you have these levels with increasing severity at hand from your favorite logging tool: TRACE < DEBUG < INFO < WARN < ERROR < FATAL How do you decide, when to use WARN vs ERROR? I can never decide what is appropriate. Do you have a good heuristic to base that decision on? ...

What's the style for immutable set and map in F#

I have just solved problem23 in Project Euler, in which I need a set to store all abundant numbers. F# has a immutable set, I can use Set.empty.Add(i) to create a new set containing number i. But I don't know how to use immutable set to do more complicated things. For example, in the following code, I need to see if a number 'x' could ...

How can I avoid including class implementation files?

Instead of doing #include "MyClass.cpp" I would like to do #include "MyClass.h" I've read online that not doing so is considered bad practice. ...

A method that returns bool, but loads multiple objects

I have a simple method that returns bool, but it internally loads various objects. I check for null for each, but it becomes very ugly. Is there a better way? public bool SomeTest(int id1) { bool result = false; User user = userDao.GetById(id1); if(user != null) { Blah blah = blahDao.GetById(user.BlahId); if...

Should I commit cosmetic changes?

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...

Is it better to wrap code into an 'IF' statement, or is it better to 'short circuit' the function and return?

I'm doing some coding in JavaScript, and I am having a lot of instances where I have to check some stuff before I proceed. I got into the habit of returning early in the function, but I'm not sure if I am doing this right. I am not sure if it have an impact on the complexity of my code as it grows. I want to know from more experience...

C - What does this line mean?

I am trying to understand what the following line of the worst-ever-seen C code (from uboot project) mean: rc = ((ulong (*)(bd_t *, int, char *[]))addr) (bd, --argc, &argv[1]); What is it? A function call? Can it be more readable? Thanks in advance for your help! ...

What is the proper style for listing imports in Java?

Is it better to list each individual piece of a package you're going to need (see #1) or is it better to just import everything from a package (see #2)? 1 import java.awt.image.ColorModel; import java.awt.image.ComponentColorModel; import java.awt.image.ColorConvertOp; 2 import java.awt.*; ...

Should I use early returns in C#?

I've learned Visual Basic and was always taught to keep the flow of the program without interruptions, like Goto, Exit and Return. Using nested ifs instead of one return statement seems very natural to me. Now that I'm partly migrating towards C#, I wonder what the best practice is for C-like languages. I've been working on a C# project...

Casting to a base reference and copying is a dirty hack, but what exactly is dirty about it?

Hi all. Problem: I have a pretty big structure with POD variables, and I need to copy around some fields, but not others. Too lazy to write down a member-by-member copy function. Solution: move the copy-able fields to the base, assign the base. Like this: struct A { int a, b, c; }; struct B : public A { int d, e, f; }; //And ...

Pattern: Elegant way to do something upon function exit?

I have a function with logic that looks like this: doStuff1() try: doStuff2() except type1: error1() return endstuff() except type2: error2() return endstuff() except: error3() return endstuff() if doStuff3(): error4() return endstuff() doStuff4() return endstuff() As you can see, endstuff() is do...

Do you use 1-3 letters variables EVERYWHERE?

I notice, in C# i use very short variable names EVERYWHERE. My code is polluted with foreach(var (v|f|i) in SOMETHING) for(int (i|n|z)=0 var (ret|r) = blah(); ... return ret; var sw = new StringWriter(); using(var r = cmd.ExecuteNonQuery()) { while(r.Read()) { r.GetSomething(3) I dont know if this is bad or ok. I can c...

Is there any web layout paradigm that makes CSS a little bit less... unpredictable?

What motivates me to write this question is that I'm really into making good appearing web-sites but I definetly do not feel confortable with CSS. My feeling is that it's all about trial-and-error. I need to try to do something and test, test and then test it over again in all browsers and after all I still have a feeling that the resul...

Should a method parameter name specify its unit in its name?

Of the following two options for method parameter names that have a unit as well as a value, which do you prefer and why? (I've used Java syntax, but my question would apply to most languages.) public void move(int length) or public void move(int lengthInMetres) Option (1) would seem to be sufficient, but I find that when I'm codin...

C++ namespaces - "using" or explicitly stated?

Possible Duplicates: Why is using namespace std; considered a bad practice in C++? Using std Namespace Is it just a matter of preference? Or is there a valid reason for preferring using namespace std; #include <string> myString string; or #include <string> myString std::string; I suppose that explicitly stating the name...

To camelCase or not to camelCase

What is the standard when coding in Java, when do you use camel case when do you use underscores etc. ...

What's a fluent interface?

I recently came across this expression - but reading up on Wikipedia did not clarify it much for me - I still don't get it: What's the point of it How is it used in practice (i.e. how does it benefit a coder in their day to day work/building systems)? [Edit] The Wikipedia article C++ example is overly long, and conflates the discussi...

naming convention for two interfaces for one object

Hi, I am working on a project to develop a poker bot, I have to store the state of every hand that is played. I wanted to do this via an object - however Players can only read from the state and the Dealer is allowed to write to the state. I thought a good way to solve this would be to make the HandState object implement 2 interfaces (o...

Pimp my Perl code

I'm an experienced developer, but not in Perl. I usually learn Perl to hack a script, then I forget it again until the next time. Hence I'm looking for advice from the pros. This time around I'm building a series of data analysis scripts. Grossly simplified, the program structure is like this: 01 my $config_var = 999; 03 my $result_va...

Why shouldn't I prefix member variables in StyleCop?

StyleCop just informed me that I shouldn't be prefixing member variables with m_. Is that the offical line on c# coding styles? I guess so as its from MS. Does anyone know anything about this?? By default, StyleCop disallows the use of underscores, m_, etc., to mark local class fields, in favor of the ‘this.’ prefix. The advantage of...