69
votes
16answers
3k views

Is continue considered bad style?

I feel myself tempted to write the following nested loop: for (int i = 0; i < N; ++i) { for (int k = 0; k < N; ++k) { if (i == k) continue; // ... other stuff ... } ...
64
votes
8answers
3k views

Is it better practice to return if false or execute an if statement if true?

Often, while writing code, I find myself presented with two options. Either I can do this: public void aThing(boolean cond) { if (cond) { // Some code } } Or this: public void ...
60
votes
9answers
11k views

Implementing a Singleton pattern in C#?

Is this the best way to implement this pattern in C#? public sealed class Singleton { private static readonly Singleton instance = new Singleton(); public static Singleton Instance { get ...
51
votes
16answers
2k views

Searching an element in a sorted array

I was applying for a position, and they asked me to complete a coding problem for them. I did so and submitted it, but I later found out I was rejected from the position. Anyways, I have an eclectic ...
42
votes
16answers
9k views

Calculating entropy of a string

We're calculating entropy of a string a few places in Stack Overflow as a signifier of low quality. I whipped up this simple method which counts unique characters in a string, but it is quite ...
39
votes
15answers
5k views

Critique requested on the program to output the names without repetitions with the number of occurrences in order of decreasing repetitions.

A short while ago, I have submitted a coding exercise to a potential employer. The response came back the next morning and you can guess what it was from the subject of this post. I am not ...
34
votes
11answers
3k views

Remove-Last-Comma-Problem

is there a more elegant way to solve this problem? List<String> paramList = new ArrayList<String>( ); paramList.add( "param1" ); paramList.add( "param2" ); StringBuilder result = new ...
34
votes
14answers
1k views

Cleaner way to code BOOL×BOOL→ENUM mapping?

Does this part look clean enough? Any suggestions on how to make it cleaner? if (isCheck) { if (isStuck) { return GameState.Mate; } else { return GameState.Check; ...
30
votes
4answers
7k views

File Browser GUI

FileBro is a basic GUI based File Browser. FileBro Functionality Directory tree - shows the file system roots at start-up, but is otherwise built lazily as the user browses around the file ...
30
votes
7answers
4k views

Fastest way to clamp an integer to the range 0..255

I'm working on some image processing code that can generate pixel values outside of the normal range of 0 to 255, and I'd like to clamp them back into the valid range. I know that there are saturating ...
28
votes
7answers
3k views

Is using the ternary operator like this considered less readable?

I often like to do things like this: return (aCondition == aThing) ? someLongExpression.getAThing() : somethingElse; Is this practice considered more or less readable than using a ...
27
votes
7answers
2k views

Is this too fancy?

I just rewrote this: if (budgetRemaining != 0 || totalOpenInvoices != 0) { } Like this: if (new[] { budgetRemaining, totalOpenInvoices }.Any(c => c != 0)) { } If I had seen that before I ...
25
votes
10answers
2k views

Little python script: Use separate functions?

I started programming with Java and C++, so I'm used to having a 'main' function that calls other functions that do the actual work. At university I was always told that doing actual computation in ...
24
votes
6answers
2k views

I wrote a program in C# to calculate prime numbers making use of Parallel.ForEach, please critique

In spare time I decided to write a program that would systematically identify prime numbers from 2 to 18,446,744,073,709,551,615. This is for fun and learning, as I know it will take too long to ...
24
votes
1answer
325 views

Tonelli-Shanks algorithm in Python

I am trying to implement quadratic sieve factorization algorithm, but got stuck at finding modular squares with Tonelli-Shanks algorithm. For example, if n = 87463, output of Tonelli-Shanks should be: ...
23
votes
8answers
48k views

Getting/setting default values from my App.config.

Imagine that I need a color palette form my Winforms application to have a consistent look. What I did was create a static helper class, and helper methods that I can call from anywhere in my code ...
22
votes
7answers
2k views

The same IF block over and over again, oh my!

Not a huge fan of this code: std::wstring LinkResolve::ResolveLink( const std::wstring& source ) const { HRESULT errorCheck; wchar_t linkTarget[MAX_PATH]; wchar_t expandedTarget[MAX_PATH]; ...
22
votes
5answers
14k views

Comma delimited string from list of items

Is there a simple way to create a comma delimited string from a list of items without adding an extra ", " to the end of the string? I frequently need to take an ASP.NET CheckBoxList and format the ...
22
votes
5answers
770 views

Instantiating objects with many attributes

I have a class with quite a few attributes, most of which are known when I create an instance of the object. So I pass all the values in the constructor: $op = new OpenIdProvider($imgPath . $name . ...
22
votes
5answers
3k views

Is this (Lock-Free) Queue Implementation Thread-Safe?

I was trying to create a lock-free queue implementation in Java, mainly for personal learning. The queue should be a general one, allowing any number of readers and/or writers concurrently. Would you ...
21
votes
7answers
655 views

What is better: Calling a chain of objects or passing a lot of objects?

Recently I saw the following line of code in one of my projects: ...
21
votes
4answers
2k views

Ternary operation in Java - Isn't this abuse?

I think that below code makes it difficult to understand and also I feel it's abuse of ternary operator in Java. Code snippet: String name = ...
20
votes
7answers
2k views

Project Euler problem 1 in python

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to isMultiple ...
19
votes
7answers
1k views

How many conditions in an “if clause” is acceptable?

I'm trying to validate an HTTP Request, and first I want to get sure that all of the information has been posted (and then I'll check the correct format of each incoming data), so to get sure that no ...
19
votes
2answers
1k views

Interview screw up after this piece of code

I was screwed up after submitting this piece of work. But I have no feedback to know what "BAD" things inside this block of code. The requirements are like this: Connect to the server on a ...
19
votes
5answers
1k views

DisposableObject base class for C#

I commonly run into the need to implement IDisposable in my code. To correctly dispose of both managed and unmanaged resources requires a reasonable amount of boilerplate code. Based on the ...
18
votes
8answers
987 views

Is it bad to use a ternary inside of an if condition?

Let's suppose I have some sort of form that automatically performs a copy procedure when the user either focuses on a textbox, or highlights some text. Let's suppose that the user has a preference of ...
18
votes
7answers
999 views

Is my code a 'safe' singleton?

I was wondering if my code will produce a true singleton. I am creating an Android app, and all activities should access my API through one instance of the SyncApi class. public class Api { ...
18
votes
3answers
654 views

Better separation of programming and presentation logic

I'm struggling for a while now with the readability of my code, I after I tried to get as much insight as possible (for my standards). On my level, I think I understand and use it all right for my ...
17
votes
7answers
1k views

Is_numeric_array() missing!

I found that in PHP (or i probably can't find it) a proper is_numeric_array($array) function is missing. So i created one. The problem is that i don't think it's great and i don't know how to improve ...
17
votes
4answers
3k views

Better/more efficient way of writing this JavaScript binary search?

I wrote an implementation of binary search in JavaScript earlier for kicks, but I noticed my version was significantly different than those found on Google. Here is an example of binary search I found ...
17
votes
7answers
1k views

Can you peer review my database design for a school system?

This system has to manage students, teachers, staff and grading. This is for production and is not a school assignment. As such, please let me know if I can improve on any aspect. :) My main concern ...
17
votes
4answers
2k views

Best way to pass parameters to Factory class?

So I have a series of objects, which I will call Impl1, Impl2, and Impl3. They each implement an interface, called IImpl. I have a Factory class who's task is to retrieve the ImplX which is ...
17
votes
6answers
643 views

Solution to facilitate code reviews [closed]

What solution do you use to facilitate code reviews in your organization? We/I use Code Collaborator from Smart Bear at my day job. It is privately hosted on our domain with LDAP authentication.
17
votes
4answers
14k views

EF Code First with Repository, UnitOfWork and DbContextFactory

I am about to >explode< :) due to amount of reading on this subject... My head hurts, and I need some honest opinions... There is a similar question/review that I noticed, but I believe my approach ...
16
votes
12answers
2k views

How to improve very loopy method?

I have a method that has a lot of loops: private void update(double depth) { Console.WriteLine("update with level " + depth); ...
16
votes
13answers
4k views

Using return value vs out parameters

It's very popular to see codes like this : bool DoSomething([some arguments]); I mean you can't understand what's the result of this method by looking at its name.(actually it could be a void) ...
16
votes
15answers
397 views

Essential reading / learning resources for the code reviewer? [closed]

The title says it all really. What materials have you read (or in another way learned from) that make you a better reviewer? 1 item per answer please - let the good ones get the votes ;-)
16
votes
5answers
3k views

Database connection in constructor and destructor

I am playing with different ways to do database interaction in PHP, and one of the ideas I have been playing with is connecting to the DB in the constructor and disconnecting in the destructor. This ...
16
votes
7answers
5k views

Is there a better way to parse console application arguments?

This is how it's done in our console app: using System.Linq; namespace Generator { internal class Program { public static void Main(string[] args) { var param1 = ...
16
votes
8answers
1k views

Long function as a lambda, right or wrong?

I have been reviewing the code of a new co-worker and, although in general I think it's OK, there's a thing that causes me mixed feelings: private void Method(...) { Thread t = new ...
16
votes
6answers
1k views

Does My Class Introduce Undefined Behavior?

My question: Does my Formatter class (or the intended use of it) introcue any UB? In our production code, we cannot use Boost or C++0x. Formatting strings using sprintf or stringstream is annoying ...
16
votes
3answers
2k views

Cleaner usage of the ternary “?” operator

I've recently been doing some mods to some old code I've been maintaining for a couple of years now. As part of a wider set of scripts using YAHOO YUI 2.2 (yes, that old) for dialog-style panels, I ...
16
votes
2answers
545 views

Cleaning up class creation / extension

I was wondering for quite some time how to clean up the below code without blowing it up any further. The extension of the classes is the main concern here, it looks a bit too much like magic. That's ...
15
votes
13answers
2k views

Bad Practice to have Long If-Else statements?

EDIT: One of the bigger questions I have is that, is using switch-case more efficient then using this? Right now in my application (a weather app), I have one section of code in my View Controllers ...
15
votes
7answers
721 views

A do while loop where its body breaks it?

I was trying to write a program that will read a collection of strings from the user, and then end the moment it encounters a ".". So then I write a do-loop. I came across a something like this: ...
15
votes
4answers
1k views

My C++ code involving an fstream failed review

I have a small 10-liner function that writes some data to a file using an std::ofstream. I did not explicitly call .close() at the end of my function, but it failed code review with the reason that it ...
15
votes
4answers
653 views

Functional Python

Python newbie here. Trying to port this little F# snippet while staying pythonic: ["something"; "something else"; "blah"; "a string"] |> List.map (fun p -> p, p.Length) |> List.sortBy snd ...
15
votes
4answers
1k views

Law of Demeter and data models?

Inspired by this question, but hopefully not a duplicate. I understand that the Law of Demeter is very useful in case of services interacting with other services, for example it's much easier to mock ...
15
votes
8answers
2k views

Is this good Ruby? (Ruby Koans' Greed Task)

Having coded in Java and C# for quite some years, I'm currently learning Ruby. I'm working my way through the Ruby Koans tutorial. At some point, you are to implement a method that calculates the ...

15 30 50 per page
1 2 3 4 5 158