coding-style interview questions
Top coding-style frequently asked interview questions
Should methods in a Java interface be declared with or without the public
access modifier?
Technically it doesn't matter, of course. A class method that implements an interface
is always public
. But what is a better convention?
Java itself is not consistent in this. See for instance Collection
vs. Comparable
, or Future
vs. ScriptEngine
.
Source: (StackOverflow)
I have always used tabs for indentation when I do Python programming. But then I came across a question here on SO where someone pointed out that most Python programmers use spaces instead of tabs to minimize editor-to-editor mistakes.
How does that make a difference? Are there other reasons why one would use spaces instead of tabs for Python? Or is it simply not true?
Should I switch my editor to insert spaces instead of tabs right away or keep on going like I used to?
Source: (StackOverflow)
I'm writing some JavaScript to parse user-entered functions (for spreadsheet-like functionality). Having parsed the formula I could convert it into JavaScript and run eval()
on it to yield the result. However, I've always shied away from using eval()
if I can avoid it because it's evil (and, rightly or wrongly, I've always thought it is even more evil in JavaScript because the code to be evaluated might be changed by the user).
So, when it is OK to use it?
Source: (StackOverflow)
I was curious about how other people use the this keyword. I tend to use it in constructors, but I may also use it throughout the class in other methods. Some examples:
In a constructor:
public Light(Vector v)
{
this.dir = new Vector(v);
}
Elsewhere
public void SomeMethod()
{
Vector vec = new Vector();
double d = (vec * vec) - (this.radius * this.radius);
}
Source: (StackOverflow)
The canonical way to return multiple values in languages that support it is often tupling.
Option: Using a tuple
Consider this trivial example:
def f(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** y3
return (y0,y1,y2)
However, this quickly gets problematic as the number of values returned increases. What if you want to return four or five values? Sure, you could keep tupling them, but it gets easy to forget which value is where. It's also rather ugly to unpack them wherever you want to receive them.
Option: Using a dictionary
The next logical step seems to be to introduce some sort of 'record notation'. In python, the obvious way to do this is by means of a dict
.
Consider the following:
def g(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** y3
return {'y0':y0, 'y1':y1 ,'y2':y2 }
(edit- Just to be clear, y0, y1 and y2 are just meant as abstract identifiers. As pointed out, in practice you'd use meaningful identifiers)
Now, we have a mechanism whereby we can project out a particular member of the returned object. For example,
result['y0']
Option: Using a class
However, there is another option. We could instead return a specialized structure. I've framed this in the context of Python, but I'm sure it applies to other languages as well. Indeed, if you were working in C this might very well be your only option. Here goes:
class ReturnValue(object):
def __init__(self, y0, y1, y2):
self.y0 = y0
self.y1 = y1
self.y2 = y2
def g(x):
y0 = x + 1
y1 = x * 3
y2 = y0 ** y3
return ReturnValue(y0, y1, y2)
In python the previous two are perhaps very similar in terms of plumbing- After all { y0, y1, y2 }
just end up being entries in the internal __dict__
of the ReturnValue
.
There is one additional feature provided by Python though for tiny objects, the __slots__
attribute. The class could be expressed as:
class ReturnValue(object):
__slots__ = ["y0", "y1", "y2"]
def __init__(y0, y1, y2):
self.y0 = y0
self.y1 = y1
self.y2 = y2
From the Python Reference Manual:
The __slots__
declaration takes a sequence of instance variables and reserves just enough space in each instance to hold a value for each variable. Space is saved because __dict__
is not created for each instance.
Option: Using a list
Another suggestion which I'd overlooked comes from Bill the Lizard:
def h(x):
result = [x + 1]
result.append(x * 3)
result.append(y0 ** y3)
return result
This is my least favorite method though. I suppose I'm tainted by exposure to Haskell, but the idea of mixed-type lists has always felt uncomfortable to me. In this particular example the list is -not- mixed type, but it conceivably could be. A list used in this way really doesn't gain anything with respect to the tuple as far as I can tell. The only real difference between lists and tuples in Python is that lists are mutable, wheras tuples are not. I personally tend to carry over the conventions from functional programming: use lists for any number of elements of the same type, and tuples for a fixed number of elements of predetermined types.
Question
After the lengthy preamble, comes the inevitable question. Which method (do you think) is best?
I've typically found myself going the dictionary route because it involves less set-up work. From a types perspective however, you might be better off going the class route, since that may help you avoid confusing what a dictionary represents. On the other hand, there are some in the Python community that feel implied interfaces should be preferred to explicit interfaces, at which point the type of the object really isn't relevant, since you're basically relying on the convention that the same attribute will always have the same meaning.
So, how do -you- return multiple values in Python?
Source: (StackOverflow)
What are some open source programs that use Haskell and can be considered to be good quality modern Haskell? The larger the code base, the better.
I want to learn from their source code. I feel I'm past the point of learning from small code examples, which are often to esoteric and small-world. I want to see how code is structured, how monads interact when you have a lot of things going on (logging, I/O, configuration, etc.).
Source: (StackOverflow)
I'm trying to use the TextView constructor with style like this:
TextView myText = new TextView(MyActivity.this, null, R.style.my_style );
however, when i do this, the text view does not appear to take the style (I verified the style by setting it on a static object).
I've also tried using myText.setTextAppearance(MyActivity.this, R.style.my_style)
but it also doesn't work
Source: (StackOverflow)
Here's the information according to the official documentation:
There are four different pairs of
opening and closing tags which can be
used in PHP. Two of those, <?php ?>
and <script language="php"> </script>
,
are always available. The other two
are short tags and ASP style tags, and
can be turned on and off from the
php.ini configuration file. As such,
while some people find short tags and
ASP style tags convenient, they are
less portable, and generally not
recommended.
In my experience most servers do have short tags enabled. Typing
<?=
is far more convenient than typing
<?php echo
The programmers convenience is an important factor, so why are they not recommended?
Source: (StackOverflow)
I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported.
(function() {
...
code
...
})();
What is the reason for doing this rather than a simple set of constructor functions?
Source: (StackOverflow)
How do I convert tabs to spaces in Notepad++?
I found a webpage that suggests it's possible, but I couldn't find any information about how to do it.
I would like to be able to do that, because some web forms don't respect code with tabs in them.
Source: (StackOverflow)
Is there a C# official guideline for the order of items in terms of class structure?
Does it go:
- Public Fields
- Private Fields
- Properties
- Constructors
- Methods
?
I'm curious if there is a hard and fast rule about the order of items? I'm kind of all over the place. I want to stick with a particular standard so I can do it everywhere.
The real problem is my more complex properties end up looking a lot like methods and they feel out of place at the top before the constructor.
Any tips/suggestions?
Source: (StackOverflow)
In JavaScript, it is possible to declare multiple variables like this:
var variable1 = "Hello World!";
var variable2 = "Testing...";
var variable3 = 42;
...or like this:
var variable1 = "Hello World!",
variable2 = "Testing...",
variable3 = 42;
Is one method better/faster than the other?
Source: (StackOverflow)
I've always thought of the if not x is None
version to be more clear, but Google's style guide implies (based on this excerpt) that they use if x is not None
. Is there any minor performance difference (I'm assuming not), and is there any case where one really doesn't fit (making the other a clear winner for my convention)?*
*I'm referring to any singleton, rather than just None
.
...to compare singletons like
None. Use is or is not.
Source: (StackOverflow)
Many C++ books contain example code like this...
std::cout << "Test line" << std::endl;
...so I've always done that too. But I've seen a lot of code from working developers like this instead:
std::cout << "Test line\n";
Is there a technical reason to prefer one over the other, or is it just a matter of coding style?
Source: (StackOverflow)