coding-style

calling method Versus class.method

I have a class with two methods defined in it. public class Routines { public static method1() { /* set of statements */ } public static method2() { /* another set of statements.*/ } } Now I need to call method1() from method2() Which one the following approaches is better? Or is this qualify as a qu...

String.Format or Not?

Duplicate from : http://stackoverflow.com/questions/16432/c-string-output-format-or-concat Especially in C# world using String.Format for everything is really common, normally as VB.NET developer unless I have to* I don't String.Format, I prefer normal string concatenation, such as: V1 = V2 & "test-x" & V3 & "-;" to me it's better ...

How do you test your Request.QueryString[] variables?

I frequently make use of Request.QueryString[] variables. In my Page_load I often do things like: int id = -1; if (Request.QueryString["id"] != null) { try { id = int.Parse(Request.QueryString["id"]); } catch { // deal with it ...

How do you reconcile common C++ naming conventions with those of the libraries

Most C++ naming conventions dictate the use of camelCaseIdentifiers: names that start with an uppercase letter for classes (Person, Booking) and names that start with a lowercase letter for fields and variables (getPrice(), isValid(), largestValue). These recommendations are completely at odds with the naming conventions of the C++ libr...

Best Practices: When not/to use partial classes.

I have been using the partial class modifier for some time in order to put helper classes in their own file. Today we got a new guy and he said that the last team he worked with didn't allow partial classes for this because modifying a helper class that is in a separate file would cause the main partial class file to get out of whack wi...

Most pythonic form for mapping a series of statements?

This is something that has bugged me for some time. I learnt Haskell before I learnt Python, so I've always been fond of thinking of many computations as a mapping onto a list. This is beautifully expressed by a list comprehension (I'm giving the pythonic version here): result = [ f(x) for x in list ] In many cases though, we want to ...

Why use = to initialise a primitive type in C++?

Where I work, people mostly think that objects are best initialised using C++-style construction (with parentheses), whereas primitive types should be initialised with the = operator: std::string strFoo( "Foo" ); int nBar = 5; Nobody seems to be able to explain why they prefer things this way, though. I can see that std::string = "Fo...

How do you return multiple values in Python?

The canonical way to return multiple values in languages that support it is often tupling. Consider this trivial example: def f(x): y0 = x + 1 y1 = x * 3 y2 = y0 ** y3 return (y0,y1,y2) However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you...

Refactor nested IF statement for clarity

Hi, I want to refactor this mumbo jumbo of a method to make it more readible, it has way to many nested IF's for my liking. How would you refactor this? public static void HandleUploadedFile(string filename) { try { if(IsValidFileFormat(filename) { int folderID = GetFolderIDFromFilename(filename); if(folderID >...

Have you come across any reason for three levels of indirection?

Just flicking through one of my favourite books (Ellen Ullman's The Bug) and there is a small bit where one programmer confronts another over three levels of indirection: ***object_array = ***winarray; I get the idea of double indirection - a way of passing a pointer into a function, and allowing it to point to an object created withi...

Why is it considered a bad practice to omit curly braces?

Why does everyone tell me writing code like this is a bad practice? if (foo) Bar(); //or for(int i = 0 i < count; i++) Bar(i); My biggest argument for omitting the curly braces is that it can sometimes be twice as many lines with them. For example, here is some code to paint a glow effect for a label in C#. using (Brush br ...

Are event callbacks with (object, EventArgs) parameters a holdover from 1.1 and WinForms?

So I have started playing around with FxCop lately and one thing I've noticed is it insists that any method attached to an event should be in the form void Callback(object sender, EventArgs args) { ...} and to be attached with MyObject.Event += new EventHandler(Callback); Now that was all well and good back in the .Net 1.1 days but...

What does a good programmer's code look like?

I am a hobbyist programmer (started with VBA to make excel quicker) and have been working with VB.NET / C#.NET and am trying to learn ADO.NET. This is my first post and I apologise for the subjective nature of the question. A facet of programming that has always frustrated me is what does 'good' look like? I am not a professional so hav...

enums for constants in java - is this good style?

In java <1.5, constants would be implemented like this public class MyClass { public static int VERTICAL = 0; public static int HORIZONTAL = 1; private int orientation; public MyClass(int orientation) { this.orientation = orientation; } ... and you would use it like this: MyClass myClass = new MyClass(My...

Cocoa Interface Style

I'm on a project doing an iPhone application. We had a Cocoa consultant come in for a few weeks. He showed me an interesting idiom of Cocoa, dealing with interfaces, but there was a difficult language barrier between us, and he wasn't really able to explain why this was done or where it was documented so I could learn more on my own. I w...

Do people still live by the 80 column rule?

I have recently been converted, at least on the principal that I can read and edit my code as I left it from any terminal in the world. But I still ask myself, when will I ever need to do that? I am finding in c++, especially modern c++, my lines are longer, with more namespace qualified references, etc. So what do people think, 80 colu...

Should I use get_/set_ prefixes in Python method names?

In Python properties are used instead of the Java-style getters, setters. So one rarely sees get... or set.. methods in the public interfaces of classes. But in cases were a property is not appropriate one might still end up with methods that behave like getters or setters. Now my questions: Should these method names start with get_ / s...

OOP and MVC programming style

I'm writing some data analysis software and decided to use such approach: epn: model/data.py <- Model definition model/reader.py <- How to read data into model view/gui.py <- main gui frame (wx) view/dialogs.py <- different dialogs (wx) epn.py <- controller For communication between gui and data I used wx.lib.pubsub. So when button 'Mo...

Marking-up argument names in comments of functions

Hello, One of the most common dilemmas I have when commenting code is how to mark-up argument names. I'll explain what I mean: def foo(vector, widht, n=0): """ Transmogrify vector to fit into width. No more than n elements will be transmogrified at a time """ Now, my problem with this is that the argument names vector, wid...

How to handle HUGE SQL strings in the source code.

I am working on a project currently where there are SQL strings in the code that are around 3000 lines. The project is a java project, but this question could probably apply for any language. Anyway, this is the first time I have ever seen something this bad. The code base is legacy, so we can suddenly migrate to Hibernate or something...