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
Join us in building a kind, collaborative learning community via our updated Code of Conduct.

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

16
votes
6answers
2k 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
291 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
votes
0answers
34 views

Automatic code editing to obey SA1201 StyleCop Rule

I added StyleCop to my project, and started editing it to make it more readable, but now I realized that there are about 1000 warnings about SA1200. Is there any way to automatically edit it in whole ...
0
votes
2answers
200 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
2answers
356 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
79 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
224 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
44 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
1answer
31 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
272 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 ...
-2
votes
0answers
48 views

How to combine a bunch of get statements into a method? [migrated]

I have the following code and I don't like how get statements looks like. Is it possible to somehow move it out into a separate method? I want to make a method more readable. params = ...
0
votes
2answers
220 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
109 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
80 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
229 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
178 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 ...
152
votes
23answers
21k 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
127 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
234 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
48 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
135 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
343 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
103 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
419 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 ...
3
votes
2answers
369 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<...
2
votes
4answers
676 views

Is “Let more people able to review” a reason to “write c++ as if Java” at some degree?

According to Should we avoid language features that C++ has but Java doesn't?, I know it is horrible to write C++ as if Java, mostly because it drops the beneficial features of C++ languages. But ...
-2
votes
1answer
108 views

Is there a name for negating a condition in an if-else block?

I recently reviewed code that read as follows: if (!condition) { do_something(); } else { do_something_else(); } In other words, in the if statement we checked that a condition was false, and ...
2
votes
3answers
471 views

Is it good practice to eliminate zero in a statement if possible (e.g.:rewrite a-b>0 into a>b)?

Sometimes, I would write if-statements with conditions like it: if(a-b>0){ } but in fact, I can move 'b' from left hand side to right hand side: if(a>b){ } Similar cases like 'a-b!=0','a-b&...
0
votes
1answer
123 views

Representing Java Object Hierarchy With Nested Classes

I was working with some API which was used internally, and while looking at their code I found something like this: public class Parent { @Data public static class Child { private ...
1
vote
2answers
145 views

HTML and JS code structure

I am relatively new to HTML/JS and am very much an amateur programmer. I have created a web app that works but I can't help but feel that the code is a spaghetti mess! I have been looking for JS/HTML ...
-2
votes
1answer
85 views

For use once only functions extracted from a longer function, should the name be xxx1(),xxx2,… or relate to there task?

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 can split long functions into smaller functions even if they are ...
11
votes
5answers
713 views

Is modifying an object passed by reference a bad practice?

In the past, I've typically done most of my manipulation of an object within the primary method it's being created/updated, but I've found myself taking a different approach lately, and I'm curious if ...
5
votes
4answers
604 views

Refactoring many else if, else if, else if, etc. statements

I'm trying to parse readable data from PDFs and I keep ending up writing code like the following: if (IsDob(line)) { dob = ParseDob(line); } else if (IsGender(line)) { gender = ParseGender(...
2
votes
2answers
245 views

(Style) “Good” way of dealing with flags/options

So I've seen many different methods used for this in different librarys, and I want to get a sense of which one may be preferred (or perhaps it's strictly personal preference/case by case). Just as an ...
10
votes
4answers
610 views

Good code style to introduce data checks everywhere?

I have a project that is sufficiently large in size that I can't keep every aspect in my head any more. I'm dealing with a number of classes and functions in it, and I'm passing data around. With ...
-2
votes
1answer
64 views

What to name my config classes

What name should I give classes which are just for configuration? For example, when using JAX-RS, every application is required to have an Application class which extends the superclass javax.ws.rs....
0
votes
1answer
55 views

Should I load appropriate locale text at start, or get the locale text according to locale preference each time?

For example, suppose my app have some localization texts. My question is, should I load appropriate locale at start: var locale=userPrefs.getLocale(); localeStringMap=getLocaleTextFromFile("...
-5
votes
1answer
103 views

Display or Sketch a graphical representation of a large code

At my new work I am dealing with a quite big C/C++ project (50k lines in multiple files). I usually "draw" what the code is doing to understand its functioning but this time I am struggling a bit ...
0
votes
1answer
88 views

Is there a coding style for Blade templates?

Since when I used smarty I always tried to separate logic statements from html tags, so my approach to blade would be to write: template.blade.php <div> <div> @if ($something == true)...
1
vote
0answers
98 views

What is the best way to manage a class with lots of properties?

We have one table meta where there is key and value so each of the row represents one component or service and then we have api system which manages the metas database. And it was going pretty well ...
4
votes
5answers
434 views

Is it a bad practice to use conditionals with functions that change program's state?

The title might be a bit vague, so let me explain. Let's assume we have a function that does something (changes state of the program), for example a function that creates a file. That function returns ...
7
votes
2answers
552 views

Is “too many parameters” a visual or a logical issue?

according to Are there guidelines on how many parameters a function should accept?, a method should not have too many parameters. However, some answers suggest this issue can be solved by builder ...
0
votes
1answer
96 views

PHP: Does comma-first code style conflict with PSR2?

I have a developer who wants to use comma-first code style in a PHP codebase to make it easier to comment out variables without having to move the comma around. PHPStorm, configured to conform to PSR-...
1
vote
3answers
229 views

How to define sound playing function? one single function for all sounds? or each sound has its own function?

After many years, I still unable to determine the correct way to define a class/function to play sound. Suppose my app may need to play some sounds, for example: button click, error popup,... How ...
5
votes
1answer
235 views

How to break down complex if conditions that include optional binding?

I've recently been reading Clean Code and there was a really nice example in java of when it is beneficial to break down a complex if statement condition in to a function rather than use a comment. //...
2
votes
4answers
286 views

Why chain assignments to clear variables?

Why do some developers in C for example chain together assignments of variables like this: i = j = k = 0; or sometimes formatted like i = j = k = 0; Is there some underlying complier optimization ...
-1
votes
3answers
119 views

How to build your class in a readable way [closed]

When developping a project I follow the OOP principles and break my code into classes and most of the time I go for one file = one class. Now I dont know how to organize my code into the class and ...
2
votes
3answers
156 views

When defining constants, which is more important? Easier to find? Or narrower scope?

For example, consider I have constants VOL_MIN and VOL_MAX, which is used inside 1 function only: public void setVolume(int val){ final int VOL_MIN = 1; final int VOL_MAX = 10; val=Math....
0
votes
2answers
138 views

When is it not Pythonic to use properties instead of a method that doesn't take arguments?

It's widely agreed that Python's properties are not merely a kludge for working around the past mistake of exposing of publicly exposing data members. But then when is it not Pythonic to use a ...
-1
votes
2answers
300 views

Should I use parenthesis around every operators?

The facts This is a very similar question to this one, but here I am talking about a more general case the MISRA-C3 Rule 5.0.2 or the SEI CERT C EXP00-C rule (more permissive). Within MISRA-C3 I ...