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
1
vote
2answers
81 views

const function parameters and default behavior

Say I have a C++ function /** * @param path If empty, the system default is used */ void foo(const std::string& path); And in my implementation I have a default handling for empty paths void ...
6
votes
7answers
505 views

Relevance of optimization techniques

I've heard of some techniques to optimize code and make it faster: On one side are clearly relevant optimization: use of better algorithms, benchmarking, etc. On the other side are techniques with a ...
-4
votes
1answer
69 views

Java Class Structure

I have a problem during a project of mine using Java, as usual my java classes have the structure: import ...; import ...; ... private Type0 myVariable0; private Type1 myVariable1; ... ... private ...
50
votes
11answers
5k views

How do you normalize coding style among multiple isolated developers?

We're a small/medium sized company with a dozen or so software developers, developing our own in-house software for in-house use. Given that there's so few of us and there's just so much work to be ...
1
vote
1answer
64 views

Write complex query OR write simpler queries and combine the results in Backend service?

Take the following statement for example: 'Get results for ALL Active students of a Course'. Naturally, this query has 2 parts, Get all active students of the course. Get results of those ...
4
votes
5answers
383 views

What are the benefits of sorting imports and properties alphabetically?

In my company, the style guide requires imports, class properties, enumerations, and consecutive variable declarations to be sorted alphabetically. I find this unnecessarily tedious. What are the ...
51
votes
6answers
20k views

Functions that simply call another function, bad design choice?

I have a setup of a class that represents a building. This building has a floor plan, which has bounds. The way I have it setup is like this: public struct Bounds {} // AABB bounding box stuff //...
5
votes
3answers
3k views

Significant difference between functional and procedural collection handling [closed]

I'm planning an empirical study on function passing - specifically lambdas aka anonymous functions aka arrow functions. Now, although functional or even object-oriented approaches are highly favored ...
2
votes
1answer
82 views

Better way to implement a feature with turn on/off based on a flag

I'm trying to implement a feature with option to turn it on/off based on a flag. I'm doing it in the following way which I feel like a overkill, public interface Feature { public interface ...
12
votes
5answers
1k views

How to introduce new language features in a legacy source code?

I have a coding style related question. In my example it is java, but I think this can be a generic question regarding languages that are changing rapidly. I'm working on a java code base which was ...
2
votes
6answers
275 views

Java (or other Java-like languages): Best practice for many optional parameters?

Code design question for Java or Java-like languages (excluding languages which support default values for method parameters such as Javascript flavors): You would like to implement a method with the ...
3
votes
1answer
237 views

Avoiding Arrow Anti-Pattern in MISRA-C

For a safety-critical domain, with a code standard including the MISRA C 2004 guidelines, a "simple" piece of code becomes obtuse, chiefly because of the requirements both that all if() statements ...
7
votes
4answers
496 views

C# declaration redundancy

When declaring a member class MyClass { AnyClass<WithLong<Generic,Declaration>> myProp = new AnyClass<WithLong<Generic,Declaration>>(); } is quite redundant. ...
2
votes
2answers
137 views

Design pattern for static fields of a subclass

I'm creating a library, and I want there to be an abstract class called Optimizer. The user should be able to choose from a set of predefined Optimizers and if desired, use fluent methods to change ...
0
votes
2answers
79 views

Code style to keep track of nested objects and data types?

In untyped languages (Python, Javascript specifically) find myself making a lot of bugs / wasting a lot of time because I forget what's in the objects I'm passing around. For example, I forget things ...
1
vote
1answer
92 views

Public class members in PIMPL

I'm attempting to use the PIMPL idiom. This is my public header file in include/foo.h: class FooPrivate; class Foo { public: Foo(); ~Foo(); private: FooPrivate* p_impl; }; ...
4
votes
4answers
484 views

How do you get everyone on the same page in terms of coding preferences on a smallish team?

Say you have 10-50 engineers working on a codebase. Everyone has different preferences for these types of things: Folder Structure Naming Conventions (CSS Class names, function names, variable names, ...
3
votes
4answers
458 views

How to use Macros in Programming to make code faster, efficient and compact

Recently I was going through some of the solutions of the best competitive programmers in the world. I found out that those people use a template while writing the programs, preferably in C++. I ...
1
vote
1answer
99 views

named fields vs compactness [closed]

Are their any objective guiding priciples to choose between named fields over compactness? const replacements = [ { from: 'apple', to: 'manzana' }, { from: 'banana', to: 'plátano' }, ]; ...
-2
votes
1answer
87 views

Keyword args vs position only args

I really like keyword arguments because it helps me see what the variable actually is... on the other hand, often these variables are not optional, and kwargs are by default optional. How do use ...
0
votes
2answers
97 views

Negation of sameness is confirmation of difference [closed]

De Morgan's laws: the negation of a disjunction is the conjunction of the negations; and the negation of a conjunction is the disjunction of the negations; or, the same: not (A or B) = not A and ...
-2
votes
1answer
49 views

Property file, annotations or database tables for storing configurations

Which one is better property file, annotations or database tables for storing configurations related to a program? What are the advantages/disadvantages and what use cases are best suited for each ...
4
votes
2answers
351 views

How to avoid duplication in a for loop when “initialization step” is identical to “update step”?

I often find a situation where I need to write duplicate codes in a for loop, where the "init step" to identical to the "update step": // duplicate `next()` for (let x = next(); p(x); x = next()) { ...
10
votes
5answers
491 views

If-else ladder that is supposed to catch all conditions - should a redundant final clause be added?

This is a thing I'm doing a lot lately. Example: setCircle(circle, i, { current }) { if (i == current) { circle.src = 'images/25CE.svg' circle.alt = 'Now picking' } else if (...
2
votes
1answer
256 views

Guard block moved to the bottom [closed]

Sometimes it is a good idea to have a guard block: https://softwareengineering.stackexchange.com/a/157413 https://en.wikipedia.org/wiki/Guard_(computer_science) Guard block guards against special ...
0
votes
3answers
223 views

Is using pytruth considered Pythonic?

I recently found Googles pytruth package. I am very used to pytest and I am wondering what the value of the way less commonly used pytruth is. The only reason why I didn't directly discard it, is that ...
1
vote
3answers
297 views

Is it good practice to make everything internal in C#?

In our solution we have a couple of projects, a project for data layer, service layer, business layer. etc. Inside the business layer, we use models to transfer data from classes to classes. Is it ...
2
votes
4answers
288 views

Does linting without auto-correction make sense?

In our Ruby on Rails projects we use a linter for our slim templates: slim-lint. It doesn't support any automatic fixes, not even the easiest ones. So I'd like to abandon it because I don't like to go ...
2
votes
2answers
230 views

Introduce code standard into old code

Our main product is written in C++ MFC and follows the same code standard as MFC. Now we will start to develop new components, and think about whether we should continue to use the same outdated ...
2
votes
1answer
178 views

Using Null/None/Nil vs empty data types

This question is more coding style related which is why this question is asked here. I'm new to Python, coming from PHP. I've been looking through Python style guides & code examples, I've ...
4
votes
6answers
723 views

Should software engineers write code for people to read, rather than for a computer to read?

I have heard of: “Programs must be written for people to read, and only incidentally for machines to execute.” ― Harold Abelson, Structure and Interpretation of Computer Programs and Donald ...
4
votes
2answers
408 views

Am I Over-Engineering if I add some code for the unknown future? [duplicate]

Look at this code: Console.WriteLine(user.name); Console.WriteLine("**********"); // 10 stars //... Tomorrow, I needed to use 20 stars instead 10 stars. Okay I would change it: Console.WriteLine(...
0
votes
3answers
141 views

Should the attributes of a class be part of the public interface even when they could break its methods?

As a minimal example, consider the following (in Python): class Foo: def __init__(self, bar): # Assume bar is a valid at this point self.bar = bar def divide_by_bar(self, num)...
51
votes
7answers
11k views

Managing and organizing the massively increased number of classes after switching to SOLID?

Over the last few years, we have been slowly making the switch over to progressively better written code, a few baby steps at a time. We are finally starting to make the switch over to something that ...
2
votes
3answers
362 views

How to deal with long URLs in comments that exceed the 80 characters limit?

I generally try to adhere to the 80 character limit for each line of code to satisfy my colleagues, to satisfy the linting tools, and to maintain an image of professionalism when random people look at ...
4
votes
7answers
1k views

How do you know where you stopped in your codes after a 2-week break? [closed]

I just had a more than 2-week long vacation/business trip and I couldn't remember actually what was I working in my coding and where I stopped. Could someone recommend a best practice to solve this?
11
votes
4answers
2k views

Which is the better way to call a method that is only available to one class that implements an interface but not the other one?

Basically I need to execute different actions given a certain condition. The existing code is written this way Base interface // DoSomething.java interface DoSomething { void letDoIt(String info)...
2
votes
2answers
207 views

What is wrong with writing web applications in a way that merges repository layer with view and with controller in the same file?

is there anything wrong with writing code in a way that merges view, repository, controller in the same file? i.e. global $db; // or $db = DBSingleton::get_db_instance(); // or global $container; $...
7
votes
4answers
281 views

Should I use assertions in a function that calls a function that have already them?

Let F(x) be a function that calls G(x), in which x must be greater than 0. If G already does assert(x > 0), should F do it as well?
1
vote
1answer
72 views

HashMap versus Object ? Which one is better to finally generate JSON output with KEY and VALUE

I do have this use case. callers(APIs in same microservice or different microservices) wants to report different KEYS and VALUES as an audit as part of their methods. There is a high possibilities ...
5
votes
1answer
1k views

Is there any reason *not* to forward declare all forward declarable function parameter/return types?

I ran into a situation where my build speeds have started to become large and have affected productivity. I had already minimized header dependencies before using forward declarations. Now I've ...
1
vote
3answers
229 views

What is a good method/practice I can employ to keep identical code snippits in two places in sync? Also, help documenting functionals

If I could get some input on the design of this, I would be grateful as well. Note that I'm programming in python. There's a function F that takes lots of data, runs some analysis on it (taking ...
3
votes
4answers
317 views

Is a constant name related to its current value an anti-pattern? [closed]

For example, suppose I have a string constant like this: const TITLEBAR_MESSAGE="Welcome back, %USERNAME%!"; I think it is more readable when it is named as const WELCOME_BACK_USERNAME="Welcome ...
1
vote
1answer
119 views

Are “I'm writing android applications” and “follow open-closed principles” reasons to allow global state?

For example, suppose I have an android application, in simple, there are 3 activities : MainActivity -> Main2Activity -> Main3Activity ,which user can visit them in sequence (eg: by button ...
3
votes
3answers
256 views

Structuring an if-else clause inside a for loop [closed]

Suppose I am doing an if-else clause inside a for loop. I like to structure it as for (...) if (...) // (part A) continue; //(part B) rather than for (...) if (...) ...
2
votes
2answers
129 views

How should I call splitted functions? centralize them? or call it one next to one?

According to Is it OK to split long functions and methods into smaller ones even though they won't be called by anything else?, I should split long functions into smaller functions even if they ...
4
votes
2answers
202 views

Should I define more methods to update necessary UI only, or less methods but update some UI unnecessarily?

For example, suppose my input data and UI is not in 1 to 1 relationship: html: <script> aChanged=function(){ }; bChanged=function(){ }; cChanged=function(){ }; </script> a:<input id=...
0
votes
0answers
130 views

How to handle error responses in a chain of CompletableFutures?

I have a long chain of completable futures in my project, with each step calling a backend API, which can give multiple error responses and one success response. Now, after parsing the response, I ...
0
votes
1answer
101 views

Chain function calls which return error codes or 0 on success in C [closed]

I used to call functions which returns int error code or 0 on success like this: int tmp = function_a() ?: function_b() ?: function_c(); if (tmp) handle_error(); Now I'm ...
42
votes
9answers
12k views

Is “avoid the yo-yo problem” a valid reason to allow the “primitive obsession”?

According to When is primitive obsession not a code smell?, I should create a ZipCode object to represent a zip code instead of a String object. However, in my experience, I prefer to see public ...