Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [coding-style]

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

6
votes
1answer
120 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; } ...
-5
votes
0answers
43 views

“# empty” comment in blank __init__.py [on hold]

This may be part of a coding style. What are the benefits to having __init__.py like this # empty as opposed to blank __init__.py?
2
votes
2answers
59 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 ...
-2
votes
0answers
22 views

How to create website design in Mobirise? [closed]

I wanted to ask how to change the project created in the app. Precisely, in Mobirise. I made a cool project but I want to bring some manual change to the code. Do you know if the application has some ...
0
votes
1answer
119 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 ...
-1
votes
0answers
65 views

Problem with ruby code readability

I am writing some method in ruby on rails, I tried to follow the rult of 80 column per line, but I ended up with some code that may seem unreadable, I tried some other alternatives There is an ...
0
votes
2answers
170 views

Uncle Bob's clean architecture refactoring techniques for data objects / entities / LINQ queries

I have relatively simple controller logic with ugly parts of object creation. Some of the objects are really huge and even though all the controller does is to return the object, it looks messy and ...
3
votes
3answers
122 views

One try/catch for each error, or one global try catch and switch error?

I have multiple methods that could fail, let's say : readFile() parseFile() compute() Each of them could fail (e.g file not found, wrong file type, wrong data), should I put each of them in their ...
1
vote
2answers
106 views

Many simple ternary operator and one if-else, which one should I use?

For example, suppose I have a toggle button listener, and it has some tasks to do according to the state of toggle: style 1 : use many ternary operator: tutorialModeToggle(isTutorialOn){ ...
8
votes
2answers
146 views

fall-through switch for executing a sequence of steps

My program needs to execute a sequence of steps from start to end. But based on different input the start point will vary, e.g, some will run from the first step to end, some will run from the 2nd ...
1
vote
1answer
125 views

Using “On” at the start of a method name

From what I've observed, methods that start with the word "On" fall into two categories: A base class that defines an event will expose a protected method whose name starts with "On" followed by the ...
2
votes
1answer
69 views

How should I define hardcoded strings with some variable parts? Reuse more characters? Or keep the whole sentence?

for example, sometimes I need to define a hardcoded string with some variable parts, I often have trouble to choose the style: style 1 : reuse every characters when possible showMessage(num){ let ...
1
vote
3answers
90 views

Python - Proper way to call a function within a class based on a certain argument passed on

Let me illustrate with an example. Say I want to create a class Returns that generates the returns of a stock for example. Returns can be arithmetic or logarithmic, and I want to be able to choose at ...
1
vote
2answers
113 views

Why should imports be made at the beginning

Why is it considered good style to do all imports in a python application at the beginning, even if what is imported is used only once? I have been programming only shorter pieces of software for a ...
1
vote
1answer
52 views

Should I group all sound file definitions together? Or define some of them locally, if possible?

For example, suppose my application have some sound files, now I define all sound file references in a single class: public class SoundHelper{ public static Sound buttonSound=new Sound("(some url)...
2
votes
2answers
284 views

Is the meaning of `const` still thread-safe in C++11?

I recently came across Herb Sutter's video from about how the meaning of const and mutable has changed in C++11 to mean bitwise const (and thread-safe, as a consequence) instead of the traditional ...
1
vote
1answer
94 views

Making type conversions better readable

If you create a function, which should make the use of typeconversions more easy, but it maybe leads to more errors, would implement it globally, and what may speaks against it - when the readability ...
2
votes
2answers
338 views

Should I use Array or Set if both can be used to finish my task?

for example, Suppose I have a 2d array: let filterArr=[ [1,1,0,1,1], [0,1,1,1,0], [1,1,0,1,0] ]; I want to find and store the index of column that all are 1, i.e.:position 1 and 3, and the ...
2
votes
0answers
62 views

What're the necessary considerations when developing Cron Jobs (Scheduled Tasks)

I'm working on a website that has a section that shows the company's stock details and investor-related data. This data is retrieved through scheduled SOAP API calls. I created two scheduled tasks ...
0
votes
1answer
143 views

For arrays, should I use the first position to store the selected element, instead of variable like “selectedIndex”, if possible?

For example, I have a toggle button, which would show the current selected element, and would show next element when it is clicked, and it can loop back to first element. I have 2 ways to store the ...
2
votes
1answer
107 views

How hard should I try to write idiomatic code in a polyglot environment?

I work on a small team (2 primaries with one or two other occasional contributors) on a project/product that spans a number of separate platforms. We do embedded firmware in straight C on Arm0 chips. ...
2
votes
1answer
88 views

Should special case be inside or outside the for loop here?

For example, suppose I have 2 arrays: let arr1=[5,2,1]; let arr2=["abcde","ab","a"]; my work is simple : to check if length of strings in arr2 are larger than corresponding element with same index ...
3
votes
6answers
601 views

Is it better to write an efficient algorithm or code that is easier to understand?

So I was recently given a coding assignment from a large financial firm, and I thought of two ways to solve the problem. One of the ways involved 1 outer for loop and 1 inner for loop. In this case, ...
14
votes
6answers
1k views

Clean Code - Should I change the literal 1 to a constant?

To avoid magic numbers, we often hear that we should give a literal a meaningful name. Such as: //THIS CODE COMES FROM THE CLEAN CODE BOOK for (int j = 0; j < 34; j++) { s += (t[j] * 4) / 5; } ...
0
votes
3answers
162 views

PHP How can I get rid of redundant if statements

I have a bunch of if statements written in PHP for a project I'm working on. The pattern is that when the $price increases by 5 cents every time magazines are increased by 10. When the amount of ...
0
votes
2answers
122 views

Nomenclature for object inspection functionality

I'm adding mod support for a game I'm working on. I parse a data file that looks something like this: "requirements": { "is_class": { "class": "warrior" }, "is_in_faction": { "faction": "...
2
votes
2answers
113 views

Should I move tasks which is just for a specific element only out of for loop?

For example, I have a for loop, which element 0 has additional function to run compared with other elements, my question is, should the additional function be: 1.place inside for loop for(int i=0;i&...
17
votes
6answers
3k views

Foreach-loop with break/return vs. while-loop with explicit invariant and post-condition

This is the most popular way (it seems to me) of checking if a value is in an array: for (int x : array) { if (x == value) return true; } return false; However, in a book I’ve ...
4
votes
2answers
337 views

Why do I need a game loop with states, instead of init the next state in current state directly?

For example, it is common to see games with game loop and states: stateChanged(){ switch(state){ STATE.PLAYER_SELECT_CHARACTER: this.currentController=new PlayerSelectCharacterController()...
1
vote
2answers
226 views

If a instance method is used in one place only, should I move that method to that place?

For example, suppose I have a ZipCode class: public class ZipCode{ public value String value; public boolean validateFormat(){ ... } public otherMethod1(){ ... }...
3
votes
3answers
535 views

Is 2 methods calling each other code smell?

For example, if 2 classes depend on each other, it is a kind of circular dependency and should be avoided. How about methods? for example, if I have 2 methods which call each other: public void ...
3
votes
1answer
90 views

Should I replace a constant with static methods, if that constant usually 'cooperate' with a specific operator?

For example, to convert between g and kg, I have a constant 1000: public static final float G_TO_KG=1000; . . . this.result = someResult*1000; I found G_TO_KG always bind to operator '*'. So my ...
0
votes
2answers
246 views

Is it necessary for a boolean to be “false” by default?

For example, to store whether sound is on, I have a boolean originally named "isSoundOn": private boolean isSoundOn=true; however, the default value of boolean is false, but I want my application to ...
1
vote
2answers
50 views

When define locale text map, language key or text key, which one should be the first key?

When define a locale text map, I can use: 1.locale key first { "en":{ "WELCOME_TO_USE_THIS_APP":"Welcome to use this app,...", "SOME_OTHER_TEXT":"fsdfsf..." }, "es":{ "...
0
votes
2answers
87 views

Would it be OK if I mention actor in the command/query name when I use CQS pattern

I'm applying Clean Architecture + DDD (maybe) in my project (web Application + REST API). The good thing I learned from the Clean Architecture is using "Interactors" to show business use cases in my ...
1
vote
3answers
293 views

Easy to maintain ways that helps code readability in C#?

Say we start with a base class that states if an object is valid or not (included one interface and two child classes for discussion purpose), like: interface IValid { bool IsValid(); } public ...
0
votes
2answers
243 views

Why do Python developers pass special strings to functions instead of defined symbols? [closed]

Is there a reason why Python developers (both maintainers of Python itself and authors of modules) tend to pass special strings as arguments to functions instead of defining symbols for the same ...
0
votes
0answers
117 views

Elegant ways to handle if(bool1 or bool2): expr if (bool1) if (bool2)

Is there any way to handle the following situation more elegantly: if sources or statuses: must_bool_query = [] bool_query['filter'] = { 'bool': { 'must': must_bool_query ...
2
votes
1answer
86 views

Is it considered bad form to call `next` on the input sequence of a list comprehension?

I want to extract lines from a stream that are preceded by the character L. The list comprehension below does the job, but calls next on the stream inside the comprehension in a way I've never seen ...
2
votes
1answer
239 views

Is 'Keep it looks similar to sample code' a valid reason to prefer WET over DRY principle?

For example, for oauth, I may need to copy and paste oauth and oauth callback code like it (assume Client is the framework I use, I don't use real framework like google because I want to simplify the ...
3
votes
4answers
200 views

C++ Preferred method of dealing with implementation for large templates

Typically when declaring a C++ class, it is best practice to put only the declaration in the header file and put the implementation in a source file. However, it seems that this design model does not ...
162
votes
23answers
22k views

Programming cleanly when writing scientific code

I don't really write large projects. I'm not maintaining a huge database or dealing with millions of lines of code. My code is primarily "scripting" type stuff - things to test mathematical ...
1
vote
2answers
169 views

Using C coding conventions in C++ code [duplicate]

I'm working on a large codebase whose core modules are in C and extension modules are in C++. We have a coding convention for C code but I'm being told that we need to enforce the same coding ...
2
votes
3answers
288 views

In languages without exception-handling, should error codes be returned from the function or in function parameters?

I'm writing a CSV parsing library in C and am considering whether to express error codes as return values or as parameters passed by reference. For example, here are the signatures for the function ...
-3
votes
1answer
51 views

How to make a configurable design in restaurant delivery problem? [closed]

The problem is like there are few restaurants and few delivery guys available. Each restaurant has a meal order and the delivery guys are to be assigned to the restaurant. Now there may be many ...
-2
votes
1answer
215 views

How to clean a refactor Java for-if-try-catch-else kind of messy code [duplicate]

Often I came across situations like this, how to write this code in a neat and clean way. One more issue I find here is performance as I am iteration a list and then it's properties. Edit : - while ...
6
votes
1answer
379 views

Is method with “ByXXX” suffix (eg:findUserById) redundant?

Sometimes I can see method definition like: public User findUserById(int id){ } But my justification is, isn't "(int id)" already include the information of "ById"? is it better to have naming just ...
0
votes
2answers
113 views

Using the same name for setter and gettter methods for a boolean member variable

Say I have class with a boolean member variable fancy: public class MyClass { private boolean fancy; } Case 1. I could the define the setter and getter as follows: // getter public boolean ...
2
votes
6answers
532 views

Should I still prefer composition over inheritance if I need both the interface and class members be generic?

According to Why should I prefer composition over inheritance?, I should prefer composition over inheritance. But what if I need to access the interface and class member in generic way? For example, I ...
5
votes
2answers
1k views

readonly vs. private getter-only property in C# 6

C# 6 added auto-property initializers and so we can do private List<LinearLayout> layouts1 { get; } = new List<LinearLayout>(); Is this better or worse than private readonly List<...