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
4
votes
4answers
313 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
243 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
194 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 ...
6
votes
4answers
453 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
129 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
72 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
85 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
471 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
368 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
96 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
86 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
88 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
45 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
339 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
483 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
243 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
214 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
286 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
283 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 ...
1
vote
2answers
222 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
163 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
704 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
401 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
138 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
330 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
206 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
280 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
57 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
932 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
228 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
305 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
110 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
123 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
94 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
11k 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 ...
2
votes
1answer
132 views

Should I use complex expressions as function arguments?

I recently started working as a programmer and I am currently going through some codes (in C#) written by my colleagues. I have noticed that they tend to include logic within function calls within ...
1
vote
3answers
219 views

how to name two methods returning collection/single item

I want to have two methods, one which returns all items and another, which returns specific Item by name (assume Item has name field): Item getItem(String itemName) List<Item> getItems() How ...
1
vote
1answer
169 views

Should “Impl” naming always be avoided?

When implementing interfaces, as a general rule, Impl is evil. Ok, but is it evil in the following case? I've a service that has (and probably will have) only one implementation. In such case, ...
-1
votes
1answer
58 views

Storing average value in database or global scope?

I have a ratings web app where I want to output the average of all the ratings. How I'm currently doing it is recalculating the average every time a new review is posted and storing that in a database ...
1
vote
3answers
79 views

Class methods that are never called and everything handled by the internals of the class itself

I have a class in my code and I'm not sure if the design I've implemented is correct or if there is a better way to do it. The class I have created have important useful methods that is used by each ...
4
votes
5answers
499 views

Is copy and paste the head of for-loop (e.g.:for(let i=0;i<something.length;i++)) violating DRY principle?

For example, in my project, I often found some head of for-loop appears many times, eg: for(let i=0;i<SharedData.students.length;i++){ SharedData.students[i].something=..... } if(isReset){ ...
8
votes
2answers
389 views

Is it a good practice to create new variable to see the output of a function?

Consider these 2 types of implementation: public int add(int x, int y) { return mysteriousAdd(x, y); } public int add(int x, int y) { int output = mysteriousAdd(x, y); return output; } ...
3
votes
2answers
111 views

How thoroughly should you document business flows for a product or system you are learning?

I work on applications that have user documentation but it is not organized or updated to the current release. Is it worth my time to create my own documentation with screenshots and notes on all the ...
0
votes
1answer
152 views

Is Java-like restrictiveness OK Python style?

Good Pythonic style is much looser than Java's uptight restrictiveness: In Java, good style means private on methods wherever possible, all fields encapsulated, type declarations, defined ...