Questions tagged [coding-style]

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

Filter by
Sorted by
Tagged with
0 votes
2 answers
155 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 ...
user avatar
0 votes
2 answers
422 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) => ...
user avatar
11 votes
4 answers
3k 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 ...
user avatar
  • 350
-3 votes
2 answers
72 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 ...
user avatar
-1 votes
1 answer
54 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 ...
user avatar
  • 11
12 votes
3 answers
2k 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 ...
user avatar
2 votes
3 answers
667 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 ...
user avatar
0 votes
1 answer
82 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 ...
user avatar
  • 11
-1 votes
3 answers
180 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 ...
user avatar
  • 117
0 votes
1 answer
281 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 ...
user avatar
  • 197
2 votes
1 answer
197 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. ...
user avatar
  • 515
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 ...
user avatar
2 votes
2 answers
295 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 ...
user avatar
3 votes
1 answer
507 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 ...
user avatar
  • 149
4 votes
5 answers
1k 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 ...
user avatar
4 votes
2 answers
289 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 ...
user avatar
  • 167
0 votes
2 answers
382 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: ...
user avatar
  • 369
1 vote
3 answers
278 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 ...
user avatar
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: ...
user avatar
1 vote
2 answers
148 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 ...
user avatar
  • 41
2 votes
1 answer
325 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 ...
user avatar
2 votes
1 answer
239 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'...
user avatar
-3 votes
1 answer
103 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 ...
user avatar
0 votes
0 answers
16 views

Defining static data structures at top level vs inside accessor function

I have a constant private data structure which has a public accessor. I've seen two styles for doing this: Style A: Define private data structure at top level of file static const char *days[] = {&...
user avatar
6 votes
1 answer
374 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; ...
user avatar
  • 222
0 votes
2 answers
77 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 ...
user avatar
0 votes
1 answer
50 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 ...
user avatar
  • 253
1 vote
3 answers
169 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 ...
user avatar
0 votes
2 answers
971 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 ...
user avatar
  • 195
2 votes
5 answers
1k 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 ...
user avatar
  • 217
49 votes
6 answers
12k 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 ...
user avatar
  • 1,541
2 votes
3 answers
501 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 = ...
user avatar
  • 211
4 votes
1 answer
82 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 ...
user avatar
  • 151
37 votes
17 answers
9k 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 ...
user avatar
  • 520
4 votes
5 answers
338 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 ...
user avatar
-3 votes
1 answer
170 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 ...
user avatar
0 votes
2 answers
157 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 ...
user avatar
2 votes
4 answers
474 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 ...
user avatar
-5 votes
1 answer
431 views

How do I manage multiple nested for-loops without using multiple variables?

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(); } } ...
user avatar
3 votes
4 answers
299 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 ...
user avatar
  • 173
1 vote
3 answers
328 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 ...
user avatar
  • 175
1 vote
3 answers
159 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 ...
user avatar
  • 109
2 votes
2 answers
299 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 ...
user avatar
25 votes
9 answers
4k views

Naming of "safe" versus "fast" functions

Say that I have two functions that are essentially identical, where one validates its arguments while the other doesn't. The rationale: sometimes you want to be safe and sometimes you want to go fast....
user avatar
-2 votes
1 answer
129 views

When writing code to consume an external API, should you default to production or testing?

I'm currently writing some code to interact with an API that doesn't have any official libraries. The provider has given me two sets of credentials - one for testing and one for production. I'm ...
user avatar
0 votes
2 answers
144 views

Which approaches are exists for using of possessive case in variables/classes/methods naming?

In variables/classes/methods naming, sometimes "of" preposition is omitted and word order changing, for example: "Absolute path of source file of entry point" -> "...
user avatar
1 vote
1 answer
915 views

Python: Class vs NamedTuple vs Hybrid vs DataClass

So all four of these approaches to structure data on their surface work more or less the same to keep data well structured. Are there any reasons, be they hidden performance issues/enhancements, ...
user avatar
  • 160
0 votes
5 answers
234 views

The cases where returning Bools as literal expressions is a better option

Most people would return Bools as follows: bananas(color) { return (color = "yellow") } and not as literal expressions; that is, not this way: bananas(color) { if (color = "...
user avatar
1 vote
1 answer
173 views

counting identifiers and operators as code size metric

I'm looking for a code metric for monitor and track over time the size of several projects and their components. Also, I would like to use it for: evaluate size reduction after refactoring compare ...
user avatar
  • 23
18 votes
7 answers
7k views

Should one prefer a generic version of a function, even if it's not re-used (yet)?

YAGNI might tell us, that in the below implementation the generic version is not needed, as long as the function is only used once. But to me personally, it seems, the generic version is more readable ...
user avatar

1
2 3 4 5
21