Coding style is a set of guidelines that helps readability and understanding of the source code.
1
vote
0answers
24 views
Objective-c anonymous property coding style
If I have an interface defined like
@interface MyClass
@property (nonatomic, copy, readonly) NSString *myString;
@end
so that myString is externally visible but can't be written, what would be ...
1
vote
1answer
171 views
I have a particular coding style, does it have a name? [closed]
I don't know if it is just what my first Java lecturer taught me or if it is based on something. It has parts of it that are the same as the Oracle Java style (naming) but differs in other ways, as ...
1
vote
1answer
87 views
Checking for valid state inside function or outside [closed]
This is a common occurrence in programming and is language agnostic, you have a function that needs to do something but in only in some cases. Maybe it's a feature-toggle, maybe it's a function that ...
-1
votes
1answer
98 views
Why does C++11 developers prefer space before colon in range-based for loops? [closed]
In most of the C++11 codes, I see:
for (auto i : list) { // a space before colon
...
}
However, usually C++ developers do not prefer:
for (int i = 0 ; i < list.size() ; ++i) { // spaces ...
0
votes
2answers
274 views
Is it ok to break the “is a” relationship if I just want part of a class' functionality?
I asked a question about extending TreeMaps into "sort by value" TreeMaps on the "code review" site on stackoverflow. Based on the response, I re-wrote the code. I like it. But, it feels like I might ...
1
vote
1answer
35 views
How to handle type conversion of a constant?
Consider the following (imaginary) code extract:
class X {
private static String IS_PAYLOAD_REQUIRED = "4001";
[....]
checkPayloadRequired(String input) {
if ...
5
votes
5answers
311 views
Should the method describe its side effects? [duplicate]
I was reading Clean Code by Bob Martin and there's one particular code smell, related to naming, that looks interesting to me:
N7: Names Should Describe Side-Effects
Names should describe ...
0
votes
7answers
200 views
In ifs inside for loops, prefer checking for true, or for false and continue?
I'm discussing this with a work colleague.
Say we want to sum the numbers from 0 to 9 skipping 5.
He prefers this:
int sum = 0;
for(int i = 0; i < 10; ++i)
{
if(i == 5)
{
continue;
...
4
votes
3answers
161 views
Is it good Python style to write a function that has no effect other than potentially raise exceptions?
Sometimes I find myself writing Python code that looks like this:
def check_stuff(param):
if condition1(param):
return "condition1" # These might be enum values, etc., instead of strings
...
2
votes
3answers
198 views
The recommended Way to exit a Loop
Occasionally - but recurringly - I face the following loop pattern problem:
CodeSnippet1
DO WHILE LoopCondition //LoopCondition depends on some pre-calculation from CodeSnippet1
CodeSnippet2 ...
0
votes
1answer
71 views
Return values and exceptions [closed]
I wrote simple function that returns a string depending on which condition is TRUE. Here is my code:
private String getMyString() {
if(!mStrigMember.isEmpty()) {
return mStrigMember;
...
0
votes
0answers
70 views
Is there any necessity to pass a variable parameter to a method while the variable declared global? [duplicate]
I am writing a class in java of Monte-Carlo algorithm. Here is the written code -
public class MonteCarlo {
int[][] matrix;
public void monteCarlo(List<Node> nodeList) {
matrix ...
0
votes
2answers
69 views
Why should a HashMap be used(in functions) to determine which value to return(for a key) when an if else construct can do the job in better time?
While I was recently working at a big company, I noticed that the programmers there followed this coding style:
Suppose I have a function that returns 12 if the input is A, 21 if the input is B, and ...
28
votes
8answers
4k views
Is throwing an exception an anti-pattern here?
I just had a discussion over a design choice after a code review. I wonder what your opinions are.
There's this Preferences class, which is a bucket for key-value pairs. Null values are legal ...
1
vote
3answers
135 views
Is it better to call a function multiple times, or to assign a variable multiple times and call the function once? [duplicate]
Is it better to write
if (condition) {
do_something(0);
} else if (other_condition) {
do_something(1);
} else {
do_something(2);
}
or
int variable;
if (condition) {
variable = 0;
} ...
0
votes
1answer
229 views
Understanding the solution of Exercise 1.12 of K&R's book as given in the book “The C Answer Book”
I am reading the solution of "The C Answer Book". I think that the solution of Ex. 1.12 can be shortened. The Exercise is as,
Write a program that prints its input one word per line.
The ...
2
votes
1answer
119 views
Functions returning strings, good style?
In my C programs I often need a way to make a string representation of my ADTs. Even if I don't need to print the string to screen in any way, it is neat to have such method for debugging. So this ...
1
vote
6answers
193 views
What to use instead of IDs in selectors in CSS
I recently installed a csslint package for my Atom text editor. I keep getting warnings saying "Don't use IDs in selectors." I found this weird since I've always been using IDs in selectors in CSS, ...
-2
votes
2answers
114 views
which is a better practice one method that does everything or a series of different methods?
I'm working with asp.net and c#
Lets say I have a bunch of drop-downs and I want to bind data from a database, is it better to make a master-bind method that loops each one and gets the parameters to ...
1
vote
2answers
137 views
Is it better to perform a calculation in the field's setter or have a different method?
I'm implementing a simple Quota object which determines a usage percentage based on the maximum and the used.
private int maximum;
private int used;
public Quota(int used, int maximum) {
...
102
votes
15answers
16k views
Is it always a best practice to write a function for anything that needs to repeat twice?
For myself, I can't wait to write a function when I need to do something more than twice. But when it comes to the things that only appear twice, it's a bit more tricky.
For code that needs more than ...
11
votes
4answers
414 views
When should a private method take the public route to access private data?
When should a private method take the public route to access private data?
For example, if I had this immutable 'multiplier' class (a bit contrived, I know):
class Multiplier {
public:
...
3
votes
4answers
364 views
int * vs int [N] vs int (*)[N] in functions parameters. Which one do you think is better?
When programming in C (or C++) there are three different ways to specify the parameter in a function that takes an array.
Here is an example (implementing std::accumulate from C++ in C) that shows ...
8
votes
5answers
1k views
How to structure a loop that repeats until success and handles failures
I am a self-taught programmer. I started programming about 1.5 years ago. Now I have started to have programming classes in school. We have had programming classes for 1/2 year and will have another ...
2
votes
4answers
275 views
How does a developer code in anticipation of change? [closed]
I ask this question based on the fact that currently my environment is under constant change due to the type of work we do. We do not always work on a project bases we often have smaller changes that ...
0
votes
2answers
32 views
Are there established guidelines for code formatting in included files with references to other files?
When developing for the web you will inevitably end up referencing a lot of files that eventually all get included and combined into a finished product. I specifically develop a lot of Wordpress sites ...
1
vote
3answers
108 views
Nullable enumeration values vs. “NoValue” or “Undefined”, etc
I often write code which translates entities in the database to domain objects. These entities often have fields which are constrained and translate to enumerations in the domain objects. In some ...
3
votes
3answers
335 views
Where should I place a typedef when used in method signatures in C++?
I'm using an Optional class quite similar to that of boost. For semantic reasons, I switched an attribute of the same (structured) type in some class definitions (and therefore also in method ...
2
votes
2answers
325 views
Should we refactor our existing codebase to use functional programming, especially streams? [closed]
We have a large project written in PHP. It almost exclusively uses imperative operations, as in example 1. Should we refactor our existing code to use functional operations? Do you think that the code ...
3
votes
1answer
183 views
Redundant ElseIf-Else Blocks [duplicate]
These types of if-elseif-else blocks appear all over the place, and in no small number (so the less the better). Every time I have to think and decide: Do I want the simpler or the more thorough of ...
0
votes
2answers
383 views
Using the optional 'self' reference in instance methods in Swift as a matter of style
Swift allows optional prefixing of method calls and property references to the current object instance via self. Removing these prefixes can declutter code but, depending on the length of the method ...
0
votes
2answers
79 views
PHP conditional test func call against two values
I was wondering if there's a one liner for PHP that would allow me test multiple values against a function call. As example, say I want to test if foo() returns either 1 or 2, (in pseudo code)
if( ...
-1
votes
3answers
179 views
Cleaner C# without unneeded indents [closed]
In OO languages, at least C#, everything has to be in a class. Sometimes, everything is in a namespace as well.
Just about literally all the code in one class is going to be automatically indented ...
5
votes
1answer
207 views
Why is the use of JavaScript in HREF attributes discouraged?
Disclaimer: I came to Programmers.SE to ask this question because I understand this is the place to ask this type of question, and not necessarily stackoverflow. If I am wrong, please close the ...
1
vote
0answers
51 views
type name for state machine state (as opposed to other state variables)
I need to create a typedef for a state machine state, e.g.
enum ToasterStateMachineState
{
TSM_READY,
TSM_TOASTING,
TSM_DONE
};
But ToasterStateMachineState seems verbose and redundant; is ...
3
votes
1answer
124 views
Should a class explicitly implement interface if its superclass also implements it?
Given an interface
interface I {
one();
two();
}
An abstract base class partially implementing I
abstract class A {
@Override
void one() {
//something
}
}
And lastly ...
7
votes
5answers
776 views
Coding style issue: Should we have functions which take a parameter, modify it, and then RETURN that parameter?
I'm having a bit of a debate with my friend over whether these two practices are merely two sides of the same coin, or whether one is genuinely better.
We have a function which takes a parameter, ...
0
votes
2answers
110 views
Print Statements Inside Function Or Before Calling It? [closed]
I have a script which different people may use. I have print statements so people can follow along what the script is doing, and if it breaks where it went wrong, and if a certain step takes a long ...
2
votes
2answers
205 views
At what point should you collapse many parameters into (e.g.) struct to improve readability in function headers?
While making a struct creates some overhead at run-time, packaging a bunch of frequently-used-together variables can dramatically increase code readability. How do you balance the two? I was just ...
2
votes
3answers
423 views
Should one value simpler code over performance when returning multiple values?
I'm too often facing situations where I need to get several types of information from a method. I usually think long and hard to circumvent these situations but I'm thinking it's pointless work that ...
0
votes
0answers
78 views
Improving the speed of coding in Fortran [duplicate]
I've got about 1.5 years of experience in writing Fortran 2003 codes for scientific applications (duh), and I'm finding myself now in the situation in which I need to write working code as fast as ...
6
votes
2answers
587 views
Which is a better pattern (coding style) for validating arguments - hurdle (barrier) or fence? [duplicate]
I don't know if there are any accepted names for these patterns (or anti-patterns), but I like to call them what I call them here. Actually, that would be Question 1: What are accepted names for these ...
16
votes
16answers
1k views
Do else blocks increase code complexity? [closed]
Here is a very simplified example. This isn't necessarily a language-specific question, and I ask that you ignore the many other ways the function can be written, and changes that can be made to it.. ...
2
votes
1answer
125 views
RefactorException: Good idea or bad idea?
When I'm doing large scale refactors I'm often commenting out the contents of methods and using NotImplementedExceptions for stuff that I still need to refactor. Problem is that this is interfering ...
0
votes
1answer
158 views
if else brackets on same line, best comment style? [closed]
I used to do this:
// If the cat is black
if ( $catColor == 'black' )
{
...
}
// Otherwise eat a taco
else
{
...
}
But I've more recently started to move toward this style of conditional ...
0
votes
1answer
140 views
Checking negative of a condition
What is the (slightly pejorative) term for checking the negative of a condition (rather than the positive which is often more readable):
e.g.
if(!someVar) {
return null;
} else {
return ...
1
vote
2answers
150 views
Should I sacrifice code succintness to ensure the narrowest variable scope? [duplicate]
In many languages (e.g. both Perl and Java - which are the two languages I work most with) it is possible to narrow the scope of local variables by declaring them within a block.
Although it adds ...
0
votes
4answers
304 views
How can if (sscanf(buf, “%i”, &mode) != 1 || TRUE) be rewritten to if (TRUE)?
I got lost in the opening of this post on reddit.
How can if (sscanf(buf, "%i", &mode) != 1 || TRUE) be rewritten to if (TRUE)? Does this assume that the sscanf never fails?
0
votes
5answers
300 views
Should we only catch in exceptional circumstances?
Whether error handling by throwing exceptions is good or bad is contentious.
Are exceptions as control flow considered a serious antipattern? If so, Why?
The common line is that exceptions are for ...
2
votes
1answer
117 views
MVC Controller - keeping methods small
I'm reading uncle Bob's Clean Code and it completely revolutionizes my programming style. In this book author claims that best methods are small methods. What about controller's action methods in ...