Programming style is a set of rules or guidelines used when writing the source code for a computer program.
3
votes
2answers
44 views
Default value or conditional?
Which of the following two is preferred:
var somevar = valueA;
if(condition) {
somevar = valueB;
}
or:
var somevar;
if(condition) {
somevar = valueB;
} else {
somevar = valueA;
}
?
...
2
votes
1answer
50 views
Performance and design question
I'm working on a web application using bottle, and it is, at this point, functional.
The gist of it is that i have a database of drinks, with associated IDs, and associated sets of ingredients. the ...
2
votes
1answer
71 views
How to design interface of deep-copy behaving pointer containers?
I want to make a container which manages big objects. which performs deep copies on copy construction and copy assignment. I also like the interface of the std containers, so I wanted to implement it ...
6
votes
1answer
157 views
Haskell Particle Simulation
I recently started learning Haskell and as my first project I decided to port a particle simulation I had written in C.
The simulation is pretty simple. We have a cubic box with particles (spheres) ...
5
votes
2answers
413 views
Is my C style good? 100 line timer program
This code works exactly as the prompt and the code predict.
Is my style good, my implementations, or what should I change, or what? I'm trying to improve my code, and writing more of it helps... Any ...
1
vote
1answer
50 views
Recursive Determinant
The following code I devised to compute a determinant:
module MatrixOps where
determinant :: (Num a, Fractional a) => [[a]] -> a
determinant [[x]] = x
determinant mat =
sum [s*x*(determinant ...
1
vote
2answers
47 views
How can I make an easy to understand subtraction accumulator?
I'm currently following the tutorials over at RubyMonk, and one of the problems I need to solve is to write a subtract function that would meet these conditions:
invoking subtract(4, 5) should ...
1
vote
4answers
129 views
Messy Control Flow: How to Improve?
Please review and recommend improvements. It looks horrible to me, difficult to read, too lengthy, and just plain old inelegant. But I don't see the potential improvements.
private boolean ...
0
votes
2answers
63 views
Improving readability of non-recursive depth first search function in Lisp
As a free-time activity in order to learn some Lisp I had to implement depth first directed graph search. Due to large graph size (800K nodes, 5M arcs) recursion-based approach I could devise didn't ...
1
vote
1answer
77 views
Wondering about simpler coding style to implement simple Neural Network in Java
I had an assignment some weeks ago that consisted of making a simple McCulloch-Pitts neural network. I ended up coding it in a pretty OO style (or the OO style I've been taught), and I felt that my ...
4
votes
2answers
68 views
Is there a way to simple Haskell class instance?
I define a Pixel data with different size like this.
data Pixel = RGB8 Word8 Word8 Word8
| RGBA8 Word8 Word8 Word8 Word8
| RGB16 Word16 Word16 Word16
| RGBA16 Word16 ...
0
votes
0answers
34 views
Merge all text files in a directory and save a temp file. Suggestion to improve my code
i coded this function where you read all text file in a directory and you save in a temporary file all values. The text files are x,y, and z format. The function returns the name of the temporary ...
0
votes
1answer
28 views
Python right position for a Error message in a class
first of all sorry for this easy question. I have a class
class Grid(object):
__slots__= ("xMin","yMax","nx","ny","xDist","yDist","ncell","IDtiles")
def ...
3
votes
1answer
66 views
Which is a better style to write default return case in if-else
private String GetFacilityName(String facilityHexID) {
if (facilityHexID.equals(FACILITY_AIRCON_HEX_ID)) {
return FACILITY_AIRCON_NAME;
}
else if ...
1
vote
1answer
55 views
Not = or <> preferable in VB.NET
In VB, I believe the following are equivalent:
If Not x = y Then
...
End If
and
If x <> y Then
...
End If
Am I wrong? Is there some scenario in which they are not equivalent? If so, what ...
2
votes
2answers
59 views
How should I present long string literals (URLs) in Python?
I have a web scraping application that contains long string literals for the URLs. What would be the best way to present them (keeping in mind that I would like to adhere to PEP-8.
URL = ...
4
votes
1answer
105 views
Seeking advice on lispiness of style and approach
I'm new to Lisp and I'm yet to wrap my head around the Lisp way of writing programs. Any comments regarding approach, style, missed opportunities appreciated:
In particular, please advice if I build ...
1
vote
3answers
48 views
Function calls as parameters for other functions
I am in two minds about function calls as parameters for other functions. The doctrine that readability is king makes me want to write like this:
br = mechanize.Browser()
raw_html = br.open(__URL)
...
4
votes
6answers
125 views
Declaration of multiple variables in one line
Given these alternatives:
int x, y, z;
or
int x;
int y;
int z;
Is there any real reason to choose one over the other?
1
vote
1answer
986 views
Dynamic programming solution to knapsack problem
I wrote a solution to the Knapsack problem in Python, using a bottom-up dynamic programming algorithm. It correctly computes the optimal value, given a list of items with values and weights, and a ...
1
vote
1answer
57 views
POST data handler
I have written a function to handle post data received from a web page. The Emphasis is on making getting post data easy: using the function allows the coder to specify the required data, type, and ...
1
vote
2answers
54 views
Project Euler problem in ocaml. Looking for idiom feedback rather than algorithmic
I'm currently attempting to learn Ocaml, and I'm working thought the Project Euler problems to do so. Here's some code I knocked together for problem 10
(*
The sum of the primes below 10 is 2 + 3 + ...
3
votes
1answer
69 views
Count comments and lines of code in ruby
Hi I wrote a small script as an answer for a stack overflow question, that counts lines of code and comments (in C and C++ style).
f = File.open("test.txt")
loc = 0
comments = 0
while line = f.gets
...
1
vote
0answers
41 views
Best Practices: Assigning a class to a variable for readability in Ruby
I recently found myself writing the following:
task :spec do |t, args|
require_relative 'spec/helper'
helper = Library::Specs::Helper
helper.start_coverage
if condition
...
0
votes
0answers
79 views
Shuffling algorithms by a Clojure Novice
The shuffle descriptions can be seen here... Shuffling
Am I using Clojure idioms and going with the grain of the language?
A few things that concern me..
Am I doing too much logic in my let ...
3
votes
1answer
109 views
A Simple Unix Filter in Racket - Learning the Racket Way
I've written the following simple filter in Racket as my first Racket program and am wondering if I am writing it in an "idiomatic Racket style".
#! /usr/bin/env racket
#lang racket
(require ...
2
votes
1answer
205 views
PEG parser in Python
Any suggestions to make this code clearer, more Pythonic, or otherwise better? I'm open to changes to the design as well as the code (but probably won't drop features or error checking since ...
5
votes
5answers
409 views
Looping over a list, checking a boolean and return value
public boolean checkNameStartsWith(List<Foo> foos) {
for (Foo foo : foos) {
if (!(foo.getName().startsWith("bar"))) {
return Boolean.FALSE;
}
}
return ...
3
votes
2answers
255 views
C++ and STL - Please Critique
I would like to get some general comments on style and use of STL in particular. This is some code I wrote to do machine learning classification (logistic regression). Any suggestions would be very ...
1
vote
1answer
86 views
Familiar with dynamic languages, new to JavaScript: criticize my style
Preamble
I am trying to learn JavaScript by writing code and looking up documentation, one problem at a time. I am already familiar with several "dynamic" languages, so I'm hoping to be productive ...
2
votes
1answer
125 views
Counting Ways to Make Change — Is this good functional/Lisp style?
I have just started learning some Scheme this weekend. I recently solved a problem that goes something like:
Count the number of ways possible to give out a certain amount of change using
1 5 10 25 ...
0
votes
1answer
64 views
Method Ordering - Readability / Maintainability [closed]
What is your opinion on method ordering?
Should it be public methods at the top followed by private methods?
or
Should it be linear order so that methods calling methods stack on top of each other?
...
3
votes
2answers
153 views
Becoming more pythonic
I started porting an API wrapper from java to python for practice. I am looking for ways to improve the readability/maintainability this code.
I have done some reading about "pythonic" style and I am ...
3
votes
3answers
134 views
Configuring toString via a public static variable
Sometimes I need toString() to be quite verbose, normally I don't. It can't be nicely solved by using other methods as toString() is what gets shown in debugger (and also in the logs unless I call ...
2
votes
1answer
107 views
Making my code look more Python-like
I've written a simple program in Python which does the following:
Input: an integer n
Output: a 2^n x 2^n matrix containing small Ls which fill the entire matrix except one single square. Each L has ...
2
votes
3answers
326 views
Is this use of (let … and …) in OCaml poor style?
In other languages, I prefer to arrange source files so that simpler and more widely useful concepts are introduced before implementation details, and I try where possible to make complex ...
8
votes
1answer
271 views
Help me make my Python code clearer
Here's my solution to Project Euler problem 40.
import operator
import math
def levels(n):
return 9 * n * 10 ** (n - 1)
def face_value(i):
def level_filler(i):
n = 1
yield 1
...
1
vote
2answers
99 views
Style and Best Practices for C99 Packetization Functions
I just spent some time putting together a simple protocol and implementation for packetizing byte streams (on GitHub here). The project is aimed at embedded systems that need a very lightweight way to ...
3
votes
1answer
303 views
Using switch to avoid duplicate code. Is there a better way?
I recently changed one of my methods and used this pattern (names are changed, a dose of humor applied for the exception):
switch (myEnum) {
case e1:
// do stuff
if (!alsoApplyCodeForE2) {
...
2
votes
2answers
171 views
What are the bad habits etc. in this code?
I know this code is pretty awful, but, by any chance, could someone point out all the flaws you can find and tell me them? I think it will help me become a better coder.
# Alien Assualt
# A bullet ...
2
votes
1answer
244 views
Generating words based on a list of letter [Python]
I wrote a program to take a list of letters and from every permutation of them and check that against the dictionary and print out valid words. So constraints are that there is a control letter which ...
3
votes
1answer
36 views
Choice of variable names for paths/filenames [closed]
I give this names such as logFilePath:
C:\Users\Felix\Programming\Logfiles
I call this logFileName:
myprogram01.log
But when I have this:
...
1
vote
1answer
46 views
new Class().Function() pattern
I am maintaining an application and have seen something like this
Permission p = new UserModel().GetPermission(userToTestPerrmission, permissionWeCheck);
if(p.CanChange)
//sometihing
else
...
6
votes
4answers
323 views
Does this code follow standard conventions?
I’m trying to learn as much as I can on my own by reading lots of examples, documentations, and asking here. I would like to improve my style to write efficient code and adhere to Java standards.
In ...
6
votes
2answers
194 views
Expanding a map, style guidance
I have a map that I want to 'expand' into an infinite sequence in the following manner:
{0 'zero 3 'three 10 'ten}
=>
('zero 'zero 'zero 'three 'three 'three 'three 'three 'three 'three 'ten 'ten ...
4
votes
1answer
210 views
Is this code idiomatic powershell?
Function Enumerate-Properties($fileName)
{
$path = (Get-Item $fileName).FullName
$shell = New-Object -COMObject Shell.Application
$folder = Split-Path $path
$file = Split-Path $path ...
1
vote
1answer
106 views
Python class design
I have written a python class which I will use as a Base class and other classes will extend this class.
The code is like below:
import requests
import feedparser
from BeautifulSoup import ...
2
votes
2answers
101 views
Pythonic style guide
Which of these 3 python snippets is more pythonic?
This 1 liner list comprehensions which is a little overcomplex
users_to_sent = [(my_database.get_user_by_id(x), group['num_sent'][x]) for x in ...
1
vote
0answers
195 views
Review ETL code to synchronize external system to internal database
I'm looking to see if my current evolution of ETL logic is getting in the ball park of "how it's done in the real world".
Specifically, do I use the metaphors correctly (Extract, Transform, Load)?
...
3
votes
2answers
117 views
Style comments please on my timer class, which I hope will be a pattern for other classes
I'm a relative noob to Python. On a range of 0 to 10, where 0 is a complete noob and 9 is Guido, I'd put myself as 1 aspiring to 2.
So I wanted a timer, rather like the Visual Basic object, and I ...
