Questions tagged [coding-style]
Coding style is a set of guidelines that helps readability and understanding of the source code.
1,064 questions
4
votes
4
answers
1k
views
Is the combination of state machine and pure function a programming style?
As a university student who just have been learning programming for a year. After I learned about the concept of state machine and the pure function in functional programming, I suddenly got an idea ...
0
votes
2
answers
1k
views
Reassign parameter to local variable
On Stack Overflow I frequently see questions with code in the following style:
function funcName(parameter) {
let variable = parameter;
// rest of function uses variable rather than parameter
}
...
-1
votes
2
answers
158
views
Any established convention on symbols to use as multi-level separators? [closed]
Let’s say that we want to encode recipes or dishes for x days in the form of a string. There can be an arbitrary number of days. Each day, we can have 0–3 of the following: breakfast, lunch, dinner. ...
36
votes
11
answers
9k
views
Best practices: Clarity vs. confidence in code behavior
When reviewing code, I sometimes see blocks like this (I happen to be using a JavaScript since it is widely understood):
if (!myVar || myVar === null || myVar === undefined) {
...
}
Those who know ...
12
votes
5
answers
7k
views
Should an optional boolean function argument rather default to true or false?
In many cases I can formulate some boolean parameter equally well positively as negatively, e.g. isOn or isOff. Which one should I pick then, in case I want the argument to have a default value? Is ...
4
votes
5
answers
2k
views
Alternatives to if-else on data reading
I have some code on Arduino (so, written in C++) that receives a String through the UART terminal, reads the String, then decides what String to print back and how many things to print depending on ...
0
votes
1
answer
120
views
Should a class be an attribute for another class if all I need is the data from it?
Title sucks, I know, but I'm having trouble describing my question.
Say I have a class Three_Eyed_Robot that sees the world in RGB through three eyes. Lets call them Red_Eye, Green_Eye, Blue_Eye, each ...
1
vote
5
answers
167
views
Should docs of extended methods include documentation of the base method?
Let's say some method of a parent class is reimplemented in a child class.
This child method is intended to do the same that the parent method, with a minor change.
In this case, in the documentation ...
0
votes
2
answers
1k
views
Best style practice: avoid negation in conditionals? [closed]
Basic question: I am wondering if there are any industry practices or style guides that mention which of these two styles is better:
if x == 0 //or nullptr
or
if !x
(I am thinking of programming ...
0
votes
2
answers
807
views
Should I use unnecessary function for readability sake
I am implementing if/else statement, by using function "inCase" to make it more readable:
const size = 'small'
const equals = str1 => str2 => str2 === str1
const inCase = (obj) => ...
12
votes
4
answers
7k
views
Should we abandon the "if not null" pattern?
I've seen that with the Dictionary API now has methods with bool TryGetValue<TKey, TValue>(TKey key, out TValue value) and I like this kind of methods because they're signaling to the other devs ...
-2
votes
2
answers
192
views
best practice to recover or handle requests that is half completed or partially failed [closed]
Assume I have a request:
app.post('/order', ()=>{
// task 1
// task 2
// task 3
})
and task1 and task2 is completed but task3 failed.
for example a user paid order successfully but saving order ...
-1
votes
1
answer
3k
views
Best practices for naming python utils / extending core modules?
So a lot of the time my utils end up with a structure that mirrors the core library. I might end up writing a multiline version of str.center, an itertools-y function that returns the first or last ...
14
votes
3
answers
3k
views
Should I choose repeated code in unit test or test logic? Can I avoid both?
When writing unit tests, I feel that there is a trade-off between code repetition and test logic.
Example of my current (likely flawed) approach:
To test this function (overly simple function for ...
8
votes
3
answers
14k
views
Are 10,000 lines of code in a file "too much"? [closed]
I am a junior iOS developer and here at work I find huge files with sometimes over 10,000 lines of code. Is it a good practice to put this many lines in a single file? Or is it too much. Is there any ...
0
votes
1
answer
308
views
Should I use SCSS mixins as shortcuts for default CSS syntax?
This question is mainly about readability and understanding of the code. Im am also in the process of creating a SCSS framework like Compass and Bourbon.
I struggle to write SCSS because I like to see ...
-1
votes
3
answers
273
views
How to improve coding while/though working? [closed]
How could I improve my coding skills aka skills in coding while being in a job? My job is mainly coding and so, my coding should be better and better over time. But the struggle is: There are always ...
0
votes
1
answer
351
views
Is there a reason why you shouldn't use 'integration variables'?
I tend to create a variable whenever I need an uncircumstantially same value as to that of an earlier value more than once (the values are not happening to be the same by chance rather they actually ...
2
votes
1
answer
2k
views
What are the best practices to follow when using global variables when you must use them?
What are the best practices when using global variables?
Normally, the common answer to this is to avoid using global variables and use local variables, properties and arguments to pass data around. ...
1
vote
0
answers
93
views
How to improve the coding quality in a problematic company [duplicate]
Due to circumstances I'm now working in a small software company. We make e-mail marketing management software and serve some big customers as our national railways and a big bank.
The software has ...
2
votes
3
answers
2k
views
Should you manually generate UUIDs / GUIDs by modifying an existing UUID / GUID?
As an example imagine I generate a UUID/GUID for an ID in a json file.
{
"25d01302-2558-4c44-bf9d-385b1cc51377": ["somevalues"]
}
is it ok or generally frowned upon to do ...
4
votes
1
answer
2k
views
Duplicate code for imports Python
In a project I have the same imports in multiple files. For example:
import os
import logging
import json
import time
import pathlib
and pylint will tell me I have duplicate code. I know that there ...
6
votes
5
answers
8k
views
Assertion statements: Remove or retain in production code
I'm working in a large company and am doing my first Java project here. We have certain code style guidelines. In the Java guideline, it is said that "assertions are only intended for debugging ...
9
votes
2
answers
1k
views
Should function names describe their parameter types?
If you wish to perform the same action using different parameters, you can either make differently named functions:
public Apple findAppleById(long id){
return repo.findById(id);
}
public Apple ...
0
votes
2
answers
2k
views
Is "error first" better than "if-else", why? [duplicate]
I have an eternal discussion in my work about why "error first" is "worng".
In order to ensure what I try to tell with error first is the following code pattern:
if condition:
...
1
vote
3
answers
1k
views
Debug statements in production quality code?
Would anyone here recommend using debug statements such as the following in production quality code?
I think these are personally one of the easiest to include or exclude, but they make the code hard ...
24
votes
8
answers
7k
views
Is using lambdas to express intent not pythonic?
PEP 8 states the following about using anonymous functions (lambdas)
Always use a def statement instead of an assignment statement that
binds a lambda expression directly to an identifier:
# Correct: ...
1
vote
2
answers
318
views
Granularity of a Method
I have a general design question. Suppose I have a List:
List<String> list = new ArrayList<>();
list.add("Str 1");
list.add("Str 2");
list.add("Str 3");
I ...
2
votes
1
answer
485
views
How to balance 'efficient' vs 'clean' code? [closed]
I have been coding in python for a little over a year, and I have learned a lot and developed quite a few applications, in the process.
I do not program for my profession, I simply program ...
2
votes
1
answer
347
views
Is it possible to restructure code to avoid this copy-paste bug? [closed]
I came across quite a subtle bug the other day, where there are two sections of similar code that were supposed to use different variables, but copy-pasting had lead them to use the same variable. I'...
-2
votes
1
answer
563
views
Assumptions versus no assumptions - where do you draw the line? [closed]
In our line of work as software engineers, we can write code that assumes various things outside its scope or architectural boundary, in order to save performance, time, and on defensive coding ...
6
votes
1
answer
843
views
How does Dependency Inversion solve switch cases?
I want to understand how Dependency Inversion gets rid of switch cases.
Consider a service that needs to communicate with several databases.
class StockUpdater {
private final DataStore datastore;
...
0
votes
2
answers
299
views
Should we put behaviors (method) in constant class in Java?
Should we put behaviors (method) in constant class in Java? If not then why? Which clean code practice/principle I am breaking while doing that?
public class TagConstants {
public static final ...
0
votes
1
answer
88
views
"Subtractive" behaviour extension vs overriding a function completely - what are the pros/cons?
Lets imagine some vendor code that we want to extend on our project level
protected function getDefaultFormClasses() {
return [
new FormClassA(),
new FormClassB(),
new ...
2
votes
3
answers
706
views
How to track temporary fixes
Whenever I do temporary fix I want it to really be temporary.
However, there is no way to come back to it later, as the fix might be a part of a bigger issue.
Let's imagine that we have for some ...
0
votes
2
answers
8k
views
Defining default values for Boolean arguments in JavaScript
Is it usually recommended to define default values for Boolean arguments?
I mean, is it usually recommended to define a function like this
someFunction(a, b, x) {
// a and b are strings, x is true ...
7
votes
9
answers
6k
views
Why would you ever want WET code?
We have all heard of Don't Repeat Yourself and Write Everything Twice, but I've never understood why anyone would prefer the latter of those. Obviously you can go overkill with DRY, with the code ...
50
votes
6
answers
13k
views
Is a comment aligned with the element being commented a good practice?
I am a home, amateur developer for 25 years and I just had a bright idea regarding comments. Like all such novel bright ideas, someone has probably already done it and there is probably a consensus on ...
2
votes
3
answers
2k
views
Error handling. Is it always necessary?
So, today I was reading a piece of code I found this function:
Public Function FolderExists(sPath As String) As Boolean
Dim FSO As New FileSystemObject
On Error GoTo errHandler
sPath = ...
4
votes
1
answer
93
views
Download and apply or apply as they download, is there a best practice?
I am writing a function that will download an HTML post.
Having downloaded the string I will:
strip off the html tags
remove special encoding characters like \n
remove trailing white spaces
My ...
39
votes
17
answers
10k
views
How can I defend reducing the strength of code reviews?
I have started in a new team. I have 20 years experience as a developer, and I have been in the role of a team lead in several projects.
Normally I am very much pro code reviews, but I ended up in a ...
4
votes
5
answers
408
views
Two different styles of functions: does either have an advantage?
In the past few months that I've been learning Javascript, I've wondered which is the better usage of functions:
Functions that perform actions based on their arguments with no return value:
const ...
-3
votes
1
answer
619
views
Standarized prefixes for naming REST POJO (Bad or good practice)? [closed]
We are building a software application for a client with a particular naming convention for REST services.
For example if you use a POJO for your request or response in a REST service something like ...
0
votes
2
answers
200
views
Simple vs bracketed usings C#
Visual Studio often recommends the 'simple using' statement rather than brackets. However, which is actually better?
I quite like the simple using statement, but I can see it potentially causing ...
2
votes
4
answers
1k
views
Guard clauses in void methods
The consensus seems to be that it's better to put an if statement guard clause at the top of a method rather than using an if else statement.
However, how can this be done with a void method and ...
-5
votes
1
answer
1k
views
How do I manage multiple nested for-loops without using multiple variables? [closed]
If I have code that looks like this:
int i;
void functionA (){
for (i=0; i<10; i++){
functionB();
}
}
void functionB (){
for (i=0; i<20; i++){
doSomething();
}
}
...
3
votes
4
answers
365
views
Elegant way to check return of getXY() for multiple values in conditional check
When I want to execute some code under the condition that a variable has one of two (or more) values I can use the OR operator:
if (var == x || var == y) {
DoSomething();
}
But I'm not sure ...
1
vote
3
answers
2k
views
Should simple helper functions be used to consolidate function calls in test code?
We have test code like:
clickElement(a);
clickElement(b);
clickElement(c);
that's repeated in many places, where clickElement is called 1 to 5 times in a row.
Should we make a helper function to ...
1
vote
3
answers
187
views
Weighting guidelines to choose between a method and a function
Quite often I find difficult to decide between implementing operations as functions or as methods because I am not sure how to weight various well-known guidelines for this problem. I would like to ...
3
votes
2
answers
1k
views
Python import order, mixing from ... import ... and import ... (and import ... as ...)
This is the mess of imports currently at the top of my file:
import argparse
from copy import deepcopy
from functools import cmp_to_key, partial
from itertools import chain
import math
from ...