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

learn more… | top users | synonyms (1)

2
votes
4answers
307 views

what is the difference between overengineering, underengineering and rightengineering? [on hold]

What is the difference between over-engineering, under-engineering and right-engineering at coding/design perspective? I found over-engineering: Coding too much like doing future requirement code ...
0
votes
2answers
71 views

guideline on void methods that do not throw exceptions

In our codebase I saw a method that is similar to following: public void doSomething() { try { ... } catch (Exception e) { ... //log something ... } } ...
1
vote
0answers
46 views

Async library guidance

I'm creating a library that contains a class that exposes several Async methods: public class MyClass { public async Task<Foo> DoFooAsync() { /*...*/ } public async Task<Bar> ...
4
votes
4answers
160 views

Using a one-shot class to store not-quite-global variables

I sometimes find myself writing classes (in Python, in my case) that are used like this: MyClass(some_args).go() In other words, there's only one method in the class that any external code would ...
1
vote
2answers
56 views

Use of “this” in Golang

On the closest thing Golang has to a style guide found here, under Receiver Names this is written: The name of a method's receiver should be a reflection of its identity; often a one or two letter ...
-1
votes
2answers
55 views

python code style for long lists [closed]

when coding a very long list in python, is it better to fit several items on each line or should I limit it to 1 per line? 99% of the time I would go with style2 below but I have 5 lists each about ...
0
votes
2answers
207 views

Way to organize repetitive text in Java

Some times I have texts that appears in different places, like comments, messages... For example if I have a custom Exception, probably class doc, messages that passes as parameter to its constructor ...
6
votes
3answers
188 views

Member: Use unique IDs vs domain object

After a couple of useful answers on whether I should use domain object or a unique id as method/function parameter here Identifier vs domain object as a method parameter , I have a similar question ...
0
votes
0answers
31 views

Defensive Programming - “Return” placement [duplicate]

Sometimes I see myself writing code in the following way: if(value == null) return null; //... I will have here as many defensive conditions (and returns) as I need //... Continue to execute method ...
1
vote
2answers
105 views

Non-printable characters and editors

Today I was presented with a very nasty problem and I do not quite know where to ask this but hope here is ok. So I have the following string: "\0" (at least that's how it looks in visual studio). If ...
3
votes
4answers
217 views

Function wrappers with no args: bad practice?

My colleague likes to write classes containing methods looking like this: public function doTaskA() { return $this->doTask('A'); } public function doTaskB() { return $this->doTask('B'); ...
1
vote
4answers
85 views

Use the company style guide or try to match the incorrect source files? [duplicate]

I'm currently working on a large project in C++. The style guide for this language has been well defined by my company and is available for everyone to see. This particular code-base is being ...
5
votes
2answers
119 views

Identifier vs domain object as a method parameter

Are there any objective arguments for or against using objects vs unique ID as method/function parameters? (and members of other objects?). Specially in the context of statically typed languages ...
0
votes
0answers
78 views

Addicted to Interfaces - Good or bad? [duplicate]

After 2 years of programming, I usually found myself using the following design pattern: public interface ISomeInterface { void SomeMethod(); void OtherMethod(); } public abstract class ...
2
votes
4answers
66 views

If I have a method that needs verification in order to proceed, do I call it inside the method or before? [duplicate]

This has been bothering me. This is more of a pragmatic problem than technical. Imagine I have a SaveOrderChanges method which, as its name implies, will save the order changes when the user submits ...
1
vote
1answer
92 views

When the result set doesn't matter should Left Join or Inner Join be used

When you have a query where you're joining from a non-null foreign key to the foreign table it doesn't matter if you use LEFT JOIN or INNER JOIN from a result standpoint (since there won't be any ...
0
votes
1answer
57 views

Getters with data conversion in Java VOs

I am working on a standard Spring application where DAO layer returns entities to service layer and service layer returns VOs to other services and controllers. In a certain scenario, we have a VO ...
5
votes
2answers
108 views

What is a reasonable level of detail for a style guide? [closed]

We are developing embedded software for an automotive product in ANSI-C in a large team. Yesterday, in our regular code review, we had a general discussion about the style of our code. We have a ...
1
vote
3answers
183 views

Using prefix/postfix increment operator for simple incremental array access rather than actual index numbers

I cannot decide which method is better with consideration of both readability and fault-tolerance. Method #1: Object[] objects = getArrayOfKnownLength(); int i = -1; someMethod(objects[++i], ...
-2
votes
1answer
77 views

How to name variables without plural in a for-each loop? [closed]

How do you name loop variables when the list item is named after something without a plural? For instance (in python): [x for x in sheep]. x is not a great name, but sheep have/has no plural that ...
0
votes
3answers
243 views

What arguments are there to use a coding style for each distinct language? [duplicate]

I recently had a discussion about our coding style for C# projects. Two things in particular were very hard to agree upon. Method Naming C# has the de-facto standard of naming (at least public, ...
0
votes
3answers
195 views

Is there a use for non-const reference parameters?

Given a function like: void do_stuff( Thing & thing ) { // at this point, I can inadvertently or purposefully change thing } Is there a reason to purposefully change thing? Changing thing ...
8
votes
1answer
182 views

Why create a Global-ish Object.create function?

I'm a fairly experienced programmer in the .NET and Java realms, and I've started reading up on JavaScript. I bought Douglas Crockford's "The Good Parts" book, and I'm immediately put off by a few ...
0
votes
0answers
44 views

Using regions in C# applications [duplicate]

What is cons and pros using #region - #endregion tags in a C# application? Generally people are asking as disadvantages of using #regions. But I am asking if there is some advantages of #regions. I ...
0
votes
2answers
141 views

Style question: To use overloaded version or not?

Let's say I had an operator() overload that made temp_.matrix[k][j] and temp_(k+1, j+1) equivalent. In my class functions (where matrix belongs), would it be better to have if (temp_.matrix[k][j] == ...
2
votes
1answer
87 views

Should I consider uncertain future requirement while development? [duplicate]

I have one text box across multiple pages which is used to enter multiple customerids (as comma separated) . But in current page due to some subsequent implementation difficulties we make it as ...
0
votes
0answers
24 views

Using self.method vs the scope :method DSL

According to the Thoughbot Rails Style Guide, we should Use def self.method, not the scope :method DSL. My question is: is this simply for the sake of choosing one of these styles and sticking ...
0
votes
3answers
73 views

Handling source code table alignment

Sometimes there is need to have tables (big or small) in source code. ItemType const SomeTable[] = { // id name min max ITEM( 3, "Foo", 70, 180), ITEM(13, "Bar", 30, 50), ...
1
vote
1answer
80 views

Objective-c anonymous property coding style

If I have an interface defined like @interface MyClass @property (nonatomic, copy, readonly) NSString *myString; @end so that myString is externally visible but can't be written, what would be ...
1
vote
1answer
185 views

I have a particular coding style, does it have a name? [closed]

I don't know if it is just what my first Java lecturer taught me or if it is based on something. It has parts of it that are the same as the Oracle Java style (naming) but differs in other ways, as ...
1
vote
1answer
103 views

Checking for valid state inside function or outside [closed]

This is a common occurrence in programming and is language agnostic, you have a function that needs to do something but in only in some cases. Maybe it's a feature-toggle, maybe it's a function that ...
-1
votes
1answer
121 views

Why does C++11 developers prefer space before colon in range-based for loops? [closed]

In most of the C++11 codes, I see: for (auto i : list) { // a space before colon ... } However, usually C++ developers do not prefer: for (int i = 0 ; i < list.size() ; ++i) { // spaces ...
0
votes
2answers
285 views

Is it ok to break the “is a” relationship if I just want part of a class' functionality?

I asked a question about extending TreeMaps into "sort by value" TreeMaps on the "code review" site on stackoverflow. Based on the response, I re-wrote the code. I like it. But, it feels like I might ...
1
vote
1answer
38 views

How to handle type conversion of a constant?

Consider the following (imaginary) code extract: class X { private static String IS_PAYLOAD_REQUIRED = "4001"; [....] checkPayloadRequired(String input) { if ...
5
votes
5answers
324 views

Should the method describe its side effects? [duplicate]

I was reading Clean Code by Bob Martin and there's one particular code smell, related to naming, that looks interesting to me: N7: Names Should Describe Side-Effects Names should describe ...
0
votes
7answers
210 views

In ifs inside for loops, prefer checking for true, or for false and continue?

I'm discussing this with a work colleague. Say we want to sum the numbers from 0 to 9 skipping 5. He prefers this: int sum = 0; for(int i = 0; i < 10; ++i) { if(i == 5) { continue; ...
4
votes
3answers
171 views

Is it good Python style to write a function that has no effect other than potentially raise exceptions?

Sometimes I find myself writing Python code that looks like this: def check_stuff(param): if condition1(param): return "condition1" # These might be enum values, etc., instead of strings ...
2
votes
3answers
213 views

The recommended Way to exit a Loop

Occasionally - but recurringly - I face the following loop pattern problem: CodeSnippet1 DO WHILE LoopCondition //LoopCondition depends on some pre-calculation from CodeSnippet1 CodeSnippet2 ...
0
votes
1answer
74 views

Return values and exceptions [closed]

I wrote simple function that returns a string depending on which condition is TRUE. Here is my code: private String getMyString() { if(!mStrigMember.isEmpty()) { return mStrigMember; ...
0
votes
0answers
71 views

Is there any necessity to pass a variable parameter to a method while the variable declared global? [duplicate]

I am writing a class in java of Monte-Carlo algorithm. Here is the written code - public class MonteCarlo { int[][] matrix; public void monteCarlo(List<Node> nodeList) { matrix ...
0
votes
2answers
79 views

Why should a HashMap be used(in functions) to determine which value to return(for a key) when an if else construct can do the job in better time?

While I was recently working at a big company, I noticed that the programmers there followed this coding style: Suppose I have a function that returns 12 if the input is A, 21 if the input is B, and ...
29
votes
8answers
4k views

Is throwing an exception an anti-pattern here?

I just had a discussion over a design choice after a code review. I wonder what your opinions are. There's this Preferences class, which is a bucket for key-value pairs. Null values are legal ...
1
vote
3answers
144 views

Is it better to call a function multiple times, or to assign a variable multiple times and call the function once? [duplicate]

Is it better to write if (condition) { do_something(0); } else if (other_condition) { do_something(1); } else { do_something(2); } or int variable; if (condition) { variable = 0; } ...
2
votes
1answer
151 views

Functions returning strings, good style?

In my C programs I often need a way to make a string representation of my ADTs. Even if I don't need to print the string to screen in any way, it is neat to have such method for debugging. So this ...
1
vote
6answers
306 views

What to use instead of IDs in selectors in CSS

I recently installed a csslint package for my Atom text editor. I keep getting warnings saying "Don't use IDs in selectors." I found this weird since I've always been using IDs in selectors in CSS, ...
-2
votes
2answers
122 views

which is a better practice one method that does everything or a series of different methods?

I'm working with asp.net and c# Lets say I have a bunch of drop-downs and I want to bind data from a database, is it better to make a master-bind method that loops each one and gets the parameters to ...
1
vote
2answers
150 views

Is it better to perform a calculation in the field's setter or have a different method?

I'm implementing a simple Quota object which determines a usage percentage based on the maximum and the used. private int maximum; private int used; public Quota(int used, int maximum) { ...
106
votes
15answers
16k views

Is it always a best practice to write a function for anything that needs to repeat twice?

For myself, I can't wait to write a function when I need to do something more than twice. But when it comes to the things that only appear twice, it's a bit more tricky. For code that needs more than ...
11
votes
4answers
426 views

When should a private method take the public route to access private data?

When should a private method take the public route to access private data? For example, if I had this immutable 'multiplier' class (a bit contrived, I know): class Multiplier { public: ...
3
votes
4answers
541 views

int * vs int [N] vs int (*)[N] in functions parameters. Which one do you think is better?

When programming in C (or C++) there are three different ways to specify the parameter in a function that takes an array. Here is an example (implementing std::accumulate from C++ in C) that shows ...