Coding style is a set of guidelines that helps readability and understanding of the source code.
-1
votes
1answer
26 views
Coding style - return object or set exposed properties
I have two coding styles:
public class Age
{
public double AgeYear { get;}
public double AgeInDays {get;}
public Person GetAge()
{
int x;
//implementation that calculates ...
3
votes
1answer
121 views
Is there a programming language which requires argument qualifiers (reference/value) to be specified at the call point?
For quite a long time now, I have been using a calling convention from C++ google style guide, which boils down to the following: "[for a function] arguments are values or const references while ...
9
votes
5answers
355 views
Is scientific code a different enough realm to ignore common coding standards?
Lately I've been trying to wrap my mind about the following fact.
On one hand, there is a host of coding guidelines and standards for what is considered to be "healthy", "clean", "well-written" and ...
4
votes
3answers
302 views
Are for loops allowed in the “Clean Code” set of rules?
Given the set of rules explained in "Clean Code", can one really use for loops? My confusion stems form the fact that a for loop is a low-level construct per se, thus it can only be used at the very ...
6
votes
6answers
306 views
How to follow the 80 character limit best practice while writing source code?
So as you know there is a best practice saying
Limit a row of source code in 80 characters.
Here are 2 links:
Why is 80 characters the 'standard' limit for code width?
Is the 80 ...
4
votes
1answer
172 views
When designing a directory structure should filenames include folder names?
Suppose I have two directories: house-1 and house-2. Each house directory needs to have files describing its windows and doors.
I am trying to understand how to name the files and directories. My ...
2
votes
1answer
49 views
Inferring system configuration using the existence of a file? any good?
This is a generic question about the idea of inferring some of the system configurations from the existence of a file or the lacking of it.
For example, we have a module of the system which is ...
1
vote
0answers
53 views
SQL Query and Java Constant Abuse?
I am currently charged with taking over a lot of code that is written with fields names placed into Java constant at the top of the file, and then the SQL queries constructed using string ...
3
votes
1answer
296 views
Bad c++ code design? [closed]
This is bothering me a long time. I feel like I am doing mistakes on the code design relative to the performance. I never had any teacher to indicate my mistakes so its hard to me to make it right in ...
1
vote
3answers
179 views
Is it considered good practice to always have methods return a value?
Sorry for the terrible title but hopefully these snippets will give you the gist.
Method 1:
class Person:
def __init__(self, name):
self.name = name
def set_name(self, new_name):
...
5
votes
1answer
146 views
Importing namespaces inside another namespace
I generally like to organize classes I make into modules by using namespaces, and I also don't go more than 2 namespaces deep but it's still painstakingly hard to fully qualify everything.
I've ...
0
votes
0answers
52 views
Where do you put your profiling code?
I just developed an algorithm and additional to the usual unit tests I wrote a profiling "test" that I was using to measure and optimize its performance. It is structured similar to a test (arrange: ...
0
votes
2answers
90 views
Should I log errors in the function they occur? or pass them back and log them when control returns?
One of the things I love about Go is how they encourage passing errors as return values, but when it comes to logging what is the most maintainable solution: passing the error as far back down the ...
1
vote
1answer
113 views
Python file naming convention?
I've seen this part of PEP-8 https://www.python.org/dev/peps/pep-0008/#package-and-module-names
I'm not clear on whether this refers to the file name of a module/class/package.
If I had one example ...
6
votes
3answers
980 views
Is break a code smell?
I'm asking in terms of a loop, obviously break is important in switch statements. Whether or not switch statements themselves are code smells is a separate issue.
So consider the following use cases ...
41
votes
8answers
4k views
When is it appropriate to make a separate function when there will only ever be a single call to said function? [duplicate]
We are designing coding standards, and are having disagreements as to if it is ever appropriate to break code out into separate functions within a class, when those functions will only ever be called ...
22
votes
7answers
2k views
Specify optional parameter names even though not required?
Consider the following method:
public List<Guid> ReturnEmployeeIds(bool includeManagement = false)
{
}
And the following call:
var ids = ReturnEmployeeIds(true);
For a developer new to ...
1
vote
2answers
191 views
How to deal with Classes having the same name (different packages)
My R&D team have Java packages containing classes with the same name. For instance:
com.myapp.model
Device
...
com.myapp.data
Device
...
We had a discussion, the names conflict when both ...
3
votes
3answers
232 views
I want to overload a function with the same type parameter; what should I do?
I'm creating an API, and I want to overload a function for strip:
QString MyClass::strip();
QString MyClass::strip(QRegularExpression open);
QString MyClass::strip(QRegularExpression close);
QString ...
2
votes
2answers
324 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
138 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 ...
6
votes
4answers
187 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 ...
4
votes
3answers
316 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 — ...
0
votes
1answer
59 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 ...
1
vote
2answers
119 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 ...
20
votes
8answers
3k 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 ...
5
votes
1answer
74 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 ~> ...
6
votes
1answer
138 views
Is there a standardized practice for ordering attributes in HTML tags?
Thanks for looking.
I am working on an AngularJS project and the attributes are numerous in many of my HTML elements:
<button type="submit" ng-click="Page.UI.DetailView.ExecuteFunction()" ...
2
votes
3answers
168 views
Angular JS style guide when declaring variables
I'm new to Angular JS and working on a project with "legacy" Angular JS code. The code is about a year or half a year old. I often see stuff like this:
var _link;
_link = "http://localhost";
Or:
...
1
vote
1answer
121 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 != ...
1
vote
1answer
81 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(
...
-3
votes
2answers
169 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, ...
43
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 ...
1
vote
1answer
79 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
118 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 ...
2
votes
1answer
133 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
168 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 ...
0
votes
2answers
69 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 ...
5
votes
4answers
191 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 ...
2
votes
1answer
414 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 ...
0
votes
0answers
36 views
How to extend code which is without documentation and very difficult to decipher? [duplicate]
I'm a junior developer and I'm coming across code which was written in a way which can only be extended with very hacky code, time constraints don't allow me to spend weeks trying to decipher the ...
0
votes
3answers
145 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 ...
8
votes
4answers
669 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 ...
1
vote
0answers
48 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
113 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 ...
0
votes
4answers
233 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)
{
...
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 ...
2
votes
3answers
213 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 ...
1
vote
1answer
66 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 ...
0
votes
2answers
73 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 ...