Coding style is a set of guidelines that helps readability and understanding of the source code.
0
votes
1answer
43 views
Is avoiding having the fields representing the same object in different communicating classes reasonable?
I'm developing a program which does communication to different types of devices (with respective protocols). It should concurrently acquire messages from devices and write them to a file with specific ...
1
vote
2answers
134 views
Can conditional break in a loop be rewritten for easier understanding?
while cond1
...
if cond2
...
else
...
break
The while loop above has two termination conditions,
cond2 and !cond1
!cond2
When the commands that are represented ...
5
votes
2answers
166 views
In Java 8, is it stylistically better to use method reference expressions or methods returning an implementation of the functional interface?
Java 8 added the concept of functional interfaces, as well as numerous new methods that are designed to take functional interfaces. Instances of these interfaces can be succinctly created using ...
4
votes
4answers
356 views
Should I be using const more in C++?
There are some things I am pretty strict about, but const has not been one of them.
For example, I might make a local variable that I use three times in a function, that does not get changed, and yet ...
29
votes
8answers
4k views
Are assignments in the condition part of conditionals a bad practice?
Let's assume I want to write a function that concatenates two strings in C.
The way I would write it is:
void concat(char s[], char t[]){
int i = 0;
int j = 0;
while (s[i] != '\0'){
...
2
votes
0answers
54 views
When should bool.boolValue be used?
Recently a co-worker has taken to checking boolean values in the following manner:
if boolVar.boolValue {
...
}
These variable are generally declared explicitly as boolean types either using:
...
1
vote
5answers
154 views
Where to declare a variable and define a function in Javascript?
I'm reading the book JavaScript: The Good Parts.
On page 113 it recommends function expressions instead of function statements, because statements are subject to hoisting:
The statement:
function ...
93
votes
10answers
15k views
What kind of bugs do “goto” statements lead to? Are there any historically significant examples?
I understand that save for breaking out of loops nested in loops; the goto statement is evaded and reviled as a bug prone style of programming, to never be used.
Alt Text: "Neal Stephenson thinks it'...
0
votes
0answers
56 views
Adding a prefix identifier to a function name in order to keep code better organized - Book reference
I usually write a name for a function, using strong a verb followed by an object, example report.printSummary() (Code convention from Book: Code Complete: A Practical Handbook of Software Construction)...
0
votes
0answers
32 views
Qt Naming Schemes for returns of boolean member variables: enabled(); vs isEnabled(); ?
In building my classes, I have noticed that I have not been very consistent about naming my boolean returns. In Qt; I notice that many of their classes use the is prefix, but checking their coding ...
3
votes
1answer
94 views
Are there any scenarios where a JS function expressions would need to refer to its “name”?
Anonymous function expressions in JS bug me as they make stack traces and Chrome Dev Tools profiler output harder to use, so I've decided to name ALL my function expressions from here on in. The ...
3
votes
1answer
304 views
Why are pure functions easier to reason about?
In computer programming (I code in c#), why are pure functions easier to reason about?
From my own experience, I find that pure functions are easier to reason about because they lack side effects, ...
4
votes
2answers
282 views
Practice for returning a value or equivalent variable?
I think it would be easiest to explain what I'm asking with an example.
function getLastNode() {
let current = this.head;
if (current == null) {
// Here, we could either return ...
5
votes
4answers
413 views
In C/C++, should I use 'const' in parameters and local variables when possible?
This question is inspired by a question about final in
java.
In C/C++, should I use const whenever possible?
I know there is already a related question about using const in parameters. ...
4
votes
3answers
206 views
Naming convention for functions which have side effects? [closed]
I heard somebody say their language has a convention where the names of functions which mutate state must end with an exclamation point. I'm trying to write more functional code and I like the idea of ...
2
votes
2answers
252 views
Using common language features only among my working environment, is it a good habit?
Now I'm working in a mobile apps company, and Objective-c ,c++ (for iOS) and Java (for android) are my most frequently used language. Is it a good habit to just use common language features among them?...
0
votes
2answers
104 views
SonarQube is complaining about: “Use isEmpty() to check whether the collection is empty or not.”
So as my the title says, SonarQube is complaining whenever you use
list.size() == 0
or
list.size > 0
However I started changing to isEmpty() and !is.Empty() and noticed the code becomes way ...
1
vote
3answers
121 views
Equivalent to “Principle of Least Astonishment” for code style?
The Principle of Least Astonishment is a design guideline that you should make your behavior match what people would expect. Don't redefine equality, behave consistently, choose sane defaults, etc.
...
2
votes
4answers
174 views
If a function contains only a switch, is it bad practise to replace the break; statements with return; statements?
Lets say I have a function that takes an argument, does some action based on the value of that argument and returns false if there is no action for that value. (pseudo-code):
bool executeSomeAction(...
1
vote
1answer
118 views
C Programming Guide Question: How to track error codes?
I have a project in visual studio that contain more than 10 header and source files. I start to realize I did not assign return error codes for each header correctly (i.e I found out that there are ...
21
votes
6answers
7k views
Is readability a valid reason to not use const in (reference) parameters?
When writing some functions, I found a const keyword in parameters like this:
void MyClass::myFunction(const MyObject& obj,const string& s1,const string& s2,const string& s3){
}
...
3
votes
3answers
322 views
When declaring an array in Java, what is the conventional location for the square brackets?
I've seen two methods of declaring an array, such as the String[] args portion of the main method:
public static void main(String args[]){
or
public static void main(String[] args){
The textbook ...
5
votes
3answers
186 views
Why use Either over (checked) Exception?
Not too long ago I started using Scala instead of Java. Part of the "conversion" process between the languages for me was learning to use Eithers instead of (checked) Exceptions. I've been coding this ...
1
vote
3answers
337 views
Combining logical statements onto 1 line in C#?
A simple question as to whether or not this would be a proper way to combine logical statements for readability and organization purposes.
The 'standard' way I was taught to write code:
...
2
votes
3answers
175 views
Approach for refactoring a function that has lots of special braching
I am writing a function that operates slightly different depending on the data passed in. The common and unique code is mingled. I don't like what I have and am looking for a better way. Hopefully, ...
7
votes
2answers
193 views
Is it wise to use Clang for personal code analysis in a project that builds with gcc?
I started to work on several C projects that are building using gcc. I believe this choice was made for several reasons:
Had to cross-compile for arm very early on (I think).
Performance is the first ...
0
votes
0answers
82 views
How to clarify the control flow when dealing with many callbacks?
Traditional programming could be done quite readable. Like this:
FUNCTION do_HTTP_request(url) {
if(!ask_user_if_he_wants_to_connect()) return;
if(!network_is_enabled()){
...
4
votes
2answers
210 views
Is it important to have a consistent commit message style on a team?
On my team of 5 coders, we've traditionally adhered to a common git commit message standard stating (only) two rules: commit messages should include the why rather than just the what and they should ...
3
votes
5answers
264 views
C++ auto-indentation (auto-style) in a multi IDE team
Is there a convenient and sustainable way to handle code indentation and style in a team in which multiple IDE's (Emacs, XCode, VS) are used by different programmers?
We are using git, so, should we ...
1
vote
1answer
237 views
Windows batch files (.bat) coding style standard
Is there any standard/encouraged Windows batch file coding style?
I would think that if I want my batch files to be easy to read/mantain, I should care about this, but searching the web I found very ...
2
votes
2answers
114 views
Using single-character variable names for function parameters? [duplicate]
Can using single-character variable names as parameters in a function okay?
For example, look at the code below:
public static int factorial (int x) {
if (x == 0) {
return 1;
}
...
-2
votes
4answers
73 views
Multiline formatting of long function signatures/calls [closed]
Let's say you have a signature like so:
public void MyFooBar(IExtraSuperLongTypeName param1, TypeA param2 ISomeotherReallyLongTypeName param3, TypeB param4, TypeC param5)
Formatting in on one line ...
0
votes
0answers
36 views
Functions inside conditionals [duplicate]
Is it a poor idea for readability or maintainability to use functions in conditionals as in the following example?
if (move_uploaded_file($file['tmp_name'], $destinationFile)) {
// Do work
}
...
4
votes
1answer
84 views
Is it good form to use an alphanumeric unique ID?
There is a company that I am pulling data from which has a unique ID for each record. However, the ID is alphanumeric (even contains a special character too). When I asked about this I was told that ...
1
vote
2answers
212 views
In Qt or C++, how should I check whether my `int` variable has been defined?
Short Problem:
How should I check if integers are undefined, just as I can check QStrings for having NULL values?
Backstory:
This is my coding style when I am trying to avoid overloading my ...
0
votes
4answers
267 views
How deal with negative feedback on code style from senior developer? [closed]
Background
I have been programming mainly in Python in the past years and mostly on my own projects. I have developed some of my own small stilistic conventions like leaving 4 numbers of empty lines ...
3
votes
1answer
182 views
Is it considered a bad practice to oversize an array so that negative indices behave well?
I have been practicing implementing some classic dynamic programming algorithms and was wondering if my implementation of the longest common subsequence search has a significant code smell.
In python,...
7
votes
1answer
208 views
Now that not all method declarations in a Java Interface are public abstract, should the methods be declared with these modifiers?
Starting with Java 8, default methods were introduced into interfaces. Effectively, this means that not all methods in an interface are abstract.
Starting with Java 9 (maybe), private methods will be ...
3
votes
2answers
199 views
Does breaking rules in the (ANSI C) standard lead to or equate with buggy code?
To my understanding, standards are mainly for code portability between compilers, but I'm curious if some things in the standard that are only treated as warnings when not followed can cause software ...
-3
votes
2answers
172 views
What is well written code? [closed]
Is code that runs fast but written with a bad and hard to understand syntax, good code?
Is code that runs slowly but written with a good and easy to understand syntax, good code?
71
votes
10answers
13k views
Why are “if elif else” statements virtually never in table format?
if i>0 : return sqrt(i)
elif i==0: return 0
else : return 1j * sqrt(-i)
VS
if i>0:
return sqrt(i)
elif i==0:
return 0
else:
return 1j * sqrt(-i)
Given the above ...
4
votes
2answers
163 views
C header file order
I see that in many projects and examples that local includes are listed before external libs and before header files for built in compiler functionality.
Is there any advantage here that I am missing?...
0
votes
2answers
106 views
When to use classes, and when to use POD (PDS) + functions
Recently, I've read a blog post, that I can't find back, about how we should "free the data". The main point of the post was that we use classes and encapsulation too much since a lot of problems can ...
3
votes
1answer
55 views
Reraising exception explicitly or passing it on?
This question is about coding style.
Say I have a function, f. f depends on a condition C. It calls another function g as a subroutine. g also depends on condition C. If it finds condition C is not ...
5
votes
4answers
460 views
Magic numbers, locality and readability
Lately I've found myself inserting magic numbers into code to make it more readable. I've done this in situations where the magic number is only used once and its purpose is obvious from the context. ...
1
vote
1answer
66 views
Coding guidlines for Controller and Dao?
My Controller code:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
ObjectReader objectReader = objectMapper.reader(...
0
votes
1answer
162 views
Are all boolean arguments flag arguments, and thus a code smell?
Martin Fowler describes the Flag Argument code smell as a "kind of function argument that tells the function to carry out a different operation depending on its value".
Answers to Is it wrong to use ...
0
votes
1answer
120 views
What scientific support does some coding standards have?
Let's consider the rule in python PEP8, coding standard for the linux kernel etc saying that a line of code are not allowed to be longer than N characters.
To this rule there is normally a few ...
5
votes
2answers
143 views
Create a new variable to shorten code
I have often wondered about the implications of shortening code by assigning temporary variables with shorter names to data accesses with long names. It's best illustrated by an example (in Ruby, but ...
0
votes
3answers
297 views
If of loops or loop with if
Assuming the compiler is smart enough to only evaluate the if in the second example once, which version should one (semantically) use and why?
if (b) {
for (auto a : as) {
foo(a);
}
} else {
...