Coding style is a set of guidelines that helps readability and understanding of the source code.

learn more… | top users | synonyms (1)

2
votes
1answer
71 views

Should I consider uncertain future requirement while development? [duplicate]

I have one text box across multiple pages which is used to enter multiple customerids (as comma separated) . But in current page due to some subsequent implementation difficulties we make it as ...
0
votes
0answers
49 views

what are the most important coding standards in c# [on hold]

I am a college student creating a C# program for the 3rd year project. In my project coding standard is a main concern. But I do not have a better understanding about C# coding standards to follow ...
-3
votes
0answers
48 views

Replacing condition with regex [on hold]

this is my code match = IPL India.second.match = pakistan India.third.match = bangalesh India.fourth.match = australia this is customized code: \bmatch**\s*\=.*=match=india.** this code is ...
-4
votes
1answer
67 views

Should I use spaces around if else control structure and curly braces? [on hold]

Should I use spaces around if else control structure and curly braces? What do you think? if ($a === $b) { foo(); } else { $bar(); } or if($a === $b){ foo(); }else{ bar(); } I ...
0
votes
0answers
18 views

Using self.method vs the scope :method DSL

According to the Thoughbot Rails Style Guide, we should Use def self.method, not the scope :method DSL. My question is: is this simply for the sake of choosing one of these styles and sticking ...
0
votes
0answers
10 views

PHP Netbeans coding style configuration, where is it? [migrated]

I want to standardize coding style accross all the developers computers in my office, I want to export my netbeans configuration and ask the other programmers to import it on their IDEs, do you know ...
0
votes
3answers
60 views

Handling source code table alignment

Sometimes there is need to have tables (big or small) in source code. ItemType const SomeTable[] = { // id name min max ITEM( 3, "Foo", 70, 180), ITEM(13, "Bar", 30, 50), ...
1
vote
0answers
41 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
172 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
88 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
101 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
276 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
37 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
313 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
202 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
199 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
136 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; } ...
2
votes
1answer
122 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
200 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
140 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) { ...
103
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
415 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
394 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
109 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
337 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
328 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
451 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
182 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
211 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
128 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
788 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
111 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
424 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
598 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
127 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 ...