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

learn more… | top users | synonyms (2)

2
votes
2answers
452 views

Is it good practice to have your C++/Qt functions always check all its arguments for null values?

Backstory While developing with Qt Signal/Slots, I came across a few segmentation faults that had me puzzled as to what was causing it. Eventually I figured out that you could actually pass a slot ...
2
votes
1answer
164 views

Javascript Closure Style Similar to Java Class Structure

PROBLEM: There is a coding imperative (S. McConnel, Code Complete) that one shouldn't code on language, but by means of it, e.g. doing right style things even if language doesn't have some ...
25
votes
7answers
5k views

When should I use a 2-property class over a pre-built structure like a KeyValuePair?

When should you put Key/Value type of data in it's own class instead of using a pre-built generic structure, such as a KeyValuePair or a Tuple? For example, most ComboBoxes I create contain a ...
4
votes
3answers
481 views

Is it ok to have multiple classes in the same file in Python?

I'm freshly coming to the Python world after years of Java and PHP. While the language itself is pretty much straightforward, I'm struggling with some 'minor' issues that I can't wrap my head around — ...
6
votes
4answers
211 views

What is the best way to split up large methods where each subtask depends on previous tasks?

According to this question and just about every style guide I've ever read, large methods are bad. However, suppose I have a method that does the following: Receive a JSON String from some service ...
45
votes
6answers
9k views

Is it better to check `c >= '0'` or `c >= 48`?

After a discussion with some my colleagues, I've a 'philosophical' question about how treat the char data type in Java, following the best practices. Suppose a simple scenario (obviously this is only ...
82
votes
20answers
14k views

How important is it to reduce the number of lines in code?

I am a Software developer who works on J2SE (core java). Often during our code reviews we are asked to reduce the number of lines in our code. It's not about removing redundant code, it's about ...
4
votes
5answers
3k views

Should I use C style in C++?

As I've been developing my position on how software should be developed at the company I work for, I've come to a certain conclusion that I'm not entirely sure of. It seems to me that if you are ...
1
vote
2answers
144 views

python - differences between reusable code vs. code for solving specific tasks

Reusable code (ex. libraries and frameworks) and code written to solve a specific task and not meant to be reused as a general tool (for example, code being used only by my 6 person team in a private ...
14
votes
4answers
16k views

Throwing an exception inside finally

Static code analyzers like Fortify "complain" when an exception might be thrown inside a finally block, saying that Using a throw statement inside a finally block breaks the logical progression ...
0
votes
1answer
68 views

How confusing is `new SomeCollection(values…)::contains` as a Predicate? [closed]

Traditionally, a function that want to skip certain items during processing will accept a Collection or var-args argument, but in the Java 8 world I think I should switch to Predicates. Now, since ...
21
votes
8answers
4k views

At what point is it taboo to have loops within loops?

Just curious. The most I have ever had was a for loop within a for loop, because after reading this from Linus Torvalds: Tabs are 8 characters, and thus indentations are also 8 characters. There ...
1
vote
2answers
114 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 ...
5
votes
1answer
76 views

Why would you document a return type using scaladoc?

The following is snippet of an example from page 240 of the book Programming Scala (Wampler & Payne, 2009; O'Reilly Media): /** @return Parser[Money] */ def deductItem = deductKind ~> ...
36
votes
10answers
12k views

Does the usage of LINQ and Lambda Expressions lead to less readable code? [closed]

I'm having a discussion with a co-worker on Linq, I'll copy here: Co-Worker: Lets be honest here. Linq syntax sucks. It's confusing and non-intuitive. Me: oh come on, more confusing ...
9
votes
7answers
2k views

Checking if a method returns false: assign result to temporary variable, or put method invocation directly in conditional?

Is it a good practice to call a method that returns true or false values in an if statement? Something like this: private void VerifyAccount() { if (!ValidateCredentials(txtUser.Text, ...
1
vote
1answer
136 views

Sorting Array before looping : best practice

I was going through JBAKE code at https://github.com/jbake-org/jbake/blob/master/src/main/java/org/jbake/app/Asset.java : 58 PFB the code. Why are we sorting the array here? if (assets != ...
54
votes
5answers
22k views

In Java, should I use “final” for parameters and locals even when I don't have to?

Java allows marking variables (fields / locals / parameters) as final, to prevent re-assigning into them. I find it very useful with fields, as it helps me quickly see whether some attributes - or an ...
1
vote
1answer
186 views

flatMap with if else vs combine with filter

When using reactive frameworks I have seen both solutions below to make a mutually exclusive selection of which stream will be forwarded (rxjava in this case) Observable.merge( Observable.just(...
-3
votes
2answers
232 views

Why ever use exception throw (in C#) except for Class Library development? [duplicate]

Why would I ever throw exceptions in code? (except for one specific scenario where I am developing a class library which the consumers of it, will not want / be able to change). To be more specific, ...
38
votes
12answers
3k views

Intentional misspellings to avoid reserved words

I often see code that include intentional misspellings of common words that for better or worse have become reserved words: klass or clazz for class: Class clazz = ThisClass.class kount for count in ...
1
vote
1answer
83 views

Possible design patterns for a service that synchronizes two third party services [closed]

I am currently in the process of refactoring a web service that synchronizes two third party services. The two services do not really interact well with each other, and there is quite a bit of ...
0
votes
2answers
177 views

What are good ways to measure project size? How do I compare the size of my project to other projects? [closed]

Background I am currently working for a quite large (relative to what I have done before) hobby project. I would like to compare the size of this project to other open-sourced projects but I cannot ...
5
votes
4answers
307 views

Ruby: if variable vs if variable.nil?

I'm new to Ruby and I was surprised when I found out that all objects are true apart from nil and false. Even 0 is true. A nice thing about that property of the language is that you can write: if !...
-1
votes
2answers
161 views

Getters with data conversion in Java VOs [closed]

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 ...
2
votes
1answer
152 views

implementing an extended interface on a concrete class

I don't know if this is a best practices or not. If I have a interface that extends another interface such as : public interface A extends B{ public void something(); }; and a concrete class ...
2
votes
2answers
208 views

Passing fields (instance variables) by arguments (parameters) inside an object - does it make sense? [duplicate]

I believe this is language agnostic question - if it's not then please correct me. Let's say I have a class (code snippet is a kind of 'pseudo code') class Car: private steering_wheel private ...
7
votes
4answers
858 views

Is the usage of internal scope blocks within a function bad style?

There are some (quite rare) cases where there is a risk of: reusing a variable which is not intended to be reused (see example 1), or using a variable instead of another, semantically close (see ...
120
votes
15answers
18k views

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

Myself, I can't wait to write a function when I need to do something more than twice. But when it comes to things that only appear twice, it's a bit more tricky. For code that needs more than two ...
0
votes
2answers
70 views

Why don't style-guides include longer explanations why something should be done in a particular way? [closed]

When I read coding style guides, such as the AirBnB Javascript Style Guide I just realized that these often do not provide deeper explanations why something should be done in a particular way. At ...
3
votes
1answer
551 views

Why do Android programmers use Hungarian notation? [duplicate]

I see Hungarian notation in almost all Android code I see. It is used in the source code, in the samples, in code from other programmers I get to see, in code that presenters show at Android ...
38
votes
4answers
37k views

Why is #include <iostream.h> bad?

I was reading another thread where a guy asked about C++ books for beginners, and one of the programmers answering wrote this: Some warnings: avoid all books that present an "hello world" stating ...
0
votes
3answers
150 views

Eliminating the bad cases in if to get a nicer code

I have read this link Should I return from a function early or use an if statement? and it triggered a conflict in my head. I agree that it looks nicer and cleaner and I guess that would be the way I ...
2
votes
2answers
134 views

Should a parent CSS class be added to the markup or @extended?

Reading SASS basic features on their website, I stumbled upon the @extend feature. The example they give is the following: .message { border: 1px solid #ccc; padding: 10px; color: #333; }...
7
votes
7answers
2k views

Prevent developers from using constants

I have one one software system which allows developers to specify an ID or name to create NodeReferences. Both work fine, but ID's are not guaranteed to be the same across different environments. I've ...
8
votes
4answers
697 views

Is this code structure beneficial in any way?

I was recently thrown into a Java web application project, and I’ve come across a number of classes that follow this type of format: public class MyThingy { private final int p1; private final ...
23
votes
7answers
10k views

How far should 'var' and null coalescing operator '??' be entertained without hampering readability?

I know the title of the question is very subjective, but I was confronted with usage of ?? operator by my peers, where at the same time I was not very happy/comfortable with applying var in new up-...
1
vote
0answers
52 views

Is marking members as static against object-orientated programming? [duplicate]

Me and my co-workers are having a bit of a discussion about one of the code analysis issues thrown by visual studio: CA1822 Mark members as static The 'this' parameter (or 'Me' in Visual Basic) of '...
-1
votes
1answer
117 views

Drawing the Line between Coding Style and Algorithm [closed]

Formatting with Indentations, White spaces, and New Lines obviously fit into the coding style category. if (a == b) { foo(); } // vs if (a == b) { foo(); } On the other hand, something ...
2
votes
1answer
168 views

Type checking as opposed to multiple functions?

In statically-typed languages such as Java, code such as the following is common (not much of a realistic example, I know): public String flip(String text) { String result = ""; for (int i = ...
53
votes
6answers
3k views

Illusory code duplication

The usual instinct is to remove any code duplication that you see in the code. However, I found myself in a situation where the duplication is illusory. To describe the situation in more details: I ...
0
votes
4answers
254 views

Where to put try-catch statements [closed]

I have the following code statement that calls a separate class. if (newOrEdit.Equals("New")) { try { BusinessLayer.InsertOperator(operatorDetails); } catch (Exception) { ...
19
votes
5answers
1k views

Are there any scientifically rigorous studies of coding style principles? [closed]

Is a coding style principle - e.g. the single-exit principle - really a good thing? Always, or just sometimes? How much difference does it really make? Whatever your opinions, these are obviously ...
2
votes
3answers
217 views

What is the most readable way of passing arguments to the function? [closed]

I'm using JavaScript but the question can be generalized to all the languages. In a nutshell - I'm checking if a browser connecting to my site is a microwave and cater for that accordingly. What ...
2
votes
1answer
69 views

Exception to the 'Nested types should not be visible' rule?

Is the following an acceptable breach of the 'Nested types should not be visible' (Microsoft .NET code analysis CA1034) rule? I have a have a class, let's call it 'TandAOLL', this is tree and an ...
2
votes
6answers
6k views

PHP: brackets or no brackets with if/for/while/…?

Both these syntaxes are correct and equivalent: With brackets: foreach ($listOfThings as $thing) { echo $thing; } Without brackets: foreach ($listOfThings as $thing): echo $thing; endforeach; ...
0
votes
2answers
96 views

What's the use of lint warnings vs errors? [closed]

I'm hoping someone who's given this some thought can help me understand. Most style-checking tools seem to distinguish between warnings and errors. To me, the point of a lint error is that a style ...
15
votes
5answers
8k views

Style guide for C++ [closed]

Right now I am using Google C++ Style Guide in my C++ code and I was pretty happy with it. Recently I was told that this guide is very bad: it is used internally by Google (I knew that), is outdated, ...
5
votes
3answers
515 views

non-optional pointers vs. non-const references in C++

In Other C++ Features, Reference Arguments of the Google C++ Style Guide, I read that non-const references must not be used. All parameters passed by reference must be labeled const. It is clear ...
72
votes
12answers
6k views

What are the drawbacks of elastic tabstops? [closed]

Look here: a typical holy war on tabs vs spaces. Now look here: elastic tabstops. All problems solved, and a bunch of very useful new behaviours added. Are elastic tabstops even mentioned in that ...