|
PIEBALDconsult wrote: I tried that, briefly.
..a good idea, if you are paid per line written.
It should feel comparably to explicitly stating that a member is "private", and prefixing everything possible with "this".
Bastard Programmer from Hell
|
|
|
|
|
Eddy Vluggen wrote: explicitly stating that a member is "private",
Which everyone should do. There should be no default access modifiers.
Eddy Vluggen wrote: prefixing everything possible with "this".
Which I also do. Just because.
|
|
|
|
|
PIEBALDconsult wrote: Which everyone should do. There should be no default access modifiers.
Why?
PIEBALDconsult wrote: Which I also do. Just because.
The amount of symbols that you use to convey an idea would best be kept to a minimum.
Bastard Programmer from Hell
|
|
|
|
|
No, you prefix the member if a class with this to prevent any ambiguity. The method or property on it's own only implies where it is. If I have this code:
class Thingy {
private int majig = 27;
void summit() {
if (majig > 42) {
}
}
}
And I copy the test to another method, all bad things could happen:
void nuThang() {
int majig = 69;
if (majig > 42) {
}
}
oops.
Panic, Chaos, Destruction. My work here is done.
Drink. Get drunk. Fall over - P O'H
OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre
I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer
Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett
|
|
|
|
|
Copy/paste is often an invitation not to check what you just pasted. Your example might be a nice example on why we introduced the DRY principle, but being this abstract, one can't be sure.
But no, I'd not be bloating my code for the sake of "easy copies". If it's that re-usable, I'll take the time to make it a snippet.
--edit;
Nagy Vilmos wrote: No, you prefix the member if a class with this to prevent any ambiguity
..the same kind of ambiguity that you have if you don't use the full name against a type, including it's namespace. Do you have global variables that clash with the names of the property/method that you're calling from the current object that you need to specify explicitly that you need the property/method from "this" object?
Bastard Programmer from Hell
modified 15-Jun-12 11:22am.
|
|
|
|
|
Nagy Vilmos wrote: you prefix the member if a class with this to prevent any ambiguity
Correct.
|
|
|
|
|
I disagree, it's just code, the compiler doesn't care* and it's better to give the next developer too much information rather than too little.
But could they have all shared one static instance?
How often are these methods called? Are there threading issues? How big do the StringBuilders become? Can you estimate how big? If large, then pre-allocating enough memory or re-using existing instances can eliminate a lot of needless re-allocation.
* However, I suspect that when you use the using directive** the compiler must have to spend some additional time looking them up.
** Qualification added for clarification.
modified 12-Jun-12 14:23pm.
|
|
|
|
|
Actually, it's less time.
As the compiler already knows in what namespace the StringBuilder is, he doesn't need to search all the used namespaces...
But this difference is imperceptible.
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Sentenryu wrote: the compiler already knows in what namespace the StringBuilder is
How?
|
|
|
|
|
You specified it when you wrote System.Text.StringBuilder ...
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
Ah, so perhaps you were agreeing with me. I see now that my statement was unclear and I have added clarification.
|
|
|
|
|
Yes yes, I really have bad time expressing this things on languages other than my own...
(actually, even in my own language sometimes I've bad times trying to express my self... )
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
There I mentioned StringBuilder is just an example. I found many similar things like below.
System.Diagnostics.Trace.Write
System.Diagnostics.Trace.WriteLine
System.Drawing.Color
System.Configuration.Configuration
System.IO.StreamWriter
System.IO.StreamReader
System.IO.FileInfo
System.IO.FileStream
System.IO.StringReader
System.IO.StringWriter
System.IO.TextReader
System.IO.TextWriter
System.Xml.XmlDocument
etc.,
....
...
..
. Why so much repetitions? After that some more 100s of replacements done. Namespace at top.
thatrajaNobody remains a virgin, Life screws everyone
|
|
|
|
|
Not that it's a good excuse, but it makes copy/pasting the code easier
If you'd import the System.Text Namespace and copy/pasted the entire thing to another file you'd get an error for every StringBuilder .
StringBuilder is a common class that should be known to all programmers, but for some of the more 'obscure' classes I prefer to use the entire namespace.
Something like: SomeCompany.ThirdPartyTool.Library.PartINeed.SubPart.TheActualClass .
At least now everyone who reads the code knows where this TheActualClass comes from, even if they didn't know the third party component.
What I find even more annoying than having System.Text.StringBuilder 100+ times in your code is having lots of imports/using statements at the top of every code file. Especially when half of them aren't used. In C# you can right-click and remove unused imports. VB (unfortunately) doesn't have this option (and please save me the C# vs. VB discussion).
I wouldn't call this a code horror, but in this case an import of System.Text does seem logical.
The real horror might be that you use 100+ StringBuilders instead of re-use one... But I'll leave that to you
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}
|
|
|
|
|
"VB (unfortunately) doesn't have this option (and please save me the C# vs. VB discussion)."
It does have that option, at least in the version I've used (VB2010)
|
|
|
|
|
Really? I was never able to find it. Just right click and...?
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}
|
|
|
|
|
Here is a situation: we have few customer who take international calling service from us. well, for all the customer there is a common rate table. but there is other 10 + customer who has their won rate table, they got cheaper service. so the genius engineer developed that code in VBA: the code is something like below:
SELECT * FROM commontimetable WHERE CustomerId <> 'Customer one' AND CustomerId <> 'Customer tow' AND .... CustomerId <> 'Customer ***'
and then for all the customer with own time table get their own query
and when a new customer get added with different rate table I had to add another AND + condition
I wish I was smart like him
|
|
|
|
|
I totally agree but I've worked in environments where that is the best option.
1. KPI is based on the number of tasks completed. In which case this would have been the quickest resolution.
2. Only implement the requirements. Nothing more. Nothing less.
It's sad but true that in some environments, adding a flag to the database schema and a check box to the UI, can lead to problems. Unless that was specifically requested.
"You get that on the big jobs."
|
|
|
|
|
Isn't there a table, where you can get all the Customest with own timetable from? Then you could use a sub-select instead of coding it hard.
When not: bad database-draft, when yes: bad select-query.
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
That's what I did, I updated the system. Where each user has a timetable name. if there is different then I put the different table name, I put this on customer basic info table.
|
|
|
|
|
------------------------------
Author of Primary ROleplaying SysTem
How do I take my coffee? Black as midnight on a moonless night.
War doesn't determine who's right. War determines who's left.
|
|
|
|
|
I am/we are moving from our 6 years old home grown entity framework towards .NET 4.0 Entity Framework and LINQ to Entities - I like it!
At a first glance it looked like I had to manually code LINQ queries into every Business-Object, even for standard operations (CRUD).
Let me show you a tiny generic BusinessObject, that fits perfectly into ObjectDataSource:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Data.Objects.DataClasses;
using System.Data.Objects;
using System.Data;
using System.Reflection;
namespace DataObjectDemo {
[DataObject]
public abstract class BasicWebBO<TEntity, TKey, TContext>
where TEntity : EntityObject,new()
where TContext : ObjectContext, new() {
private TContext _context;
protected TContext Context {
get {if (_context == null) {
_context = new TContext();
}
return _context;
}
}
protected virtual string EntitySetName {
get {
return typeof(TEntity).Name;
}
}
[DataObjectMethod(DataObjectMethodType.Insert)]
public virtual void Insert(TEntity entity) {
Context.AddObject(typeof(TEntity).Name,entity);
Context.SaveChanges();
}
[DataObjectMethod(DataObjectMethodType.Update)]
public virtual void Update(TEntity entity) {
Context.AttachTo(EntitySetName, entity);
Context.ObjectStateManager.
ChangeObjectState(
entity, System.Data.EntityState.Modified);
Context.ApplyCurrentValues(
entity.EntityKey.EntitySetName, entity);
Context.SaveChanges();
}
[DataObjectMethod(DataObjectMethodType.Delete)]
public virtual void Delete(TEntity entity) {
Context.AttachTo(EntitySetName, entity);
Context.DeleteObject(entity);
Context.SaveChanges();
}
[DataObjectMethod(DataObjectMethodType.Select)]
public virtual TEntity Select(TKey key) {
TEntity template = new TEntity();
PropertyInfo pi = typeof(TEntity).GetProperties()[0];
pi.SetValue(template, key, null);
EntityKey entityKey =
Context.CreateEntityKey(EntitySetName, template);
object entity;
if (Context.TryGetObjectByKey(entityKey, out entity)) {
return entity as TEntity;
}
return null;
}
Now you can easily derive a BusinessObject, eg. PersonBO:
public class PersonBO : BasicWebBO<Person, int, DemoEntities> {
}
PersonBO is now ready to use and provides a standard type-save implementation for ObjectDataSource-CRUD-Methods along with designer support
You can also extend PersonBO with any additional business logic and Custom-CRUD-Methods
[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Person> SelectAll() {
return from person in Context.Person select person;
}
[DataObjectMethod(DataObjectMethodType.Update)]
public void UpdateImage(int PNR, byte[] image) {
}
You may have noticed the hack around the primary key value. It would also make sense, to declare this method abstract.
wish you well
modified 1-Jul-12 20:14pm.
|
|
|
|
|
Hi,
Woppelmann P wrote: Now you can easily derive a BusinessObject
This idea of deriving business object is fantastic.
You should use this idea as a new article. The user's will come to know about this. This is not the right place of this kind of ideas. You can refer Here[^] for articles and tips.
Warm Regards.
--Amit
|
|
|
|
|
Try
Dim objReader As New System.IO.StreamReader(Application.StartupPath & "\" & TextBox3.Text & "_OAuthtk.dat")
My.Settings.m_strToken = objReader.ReadLine()
My.Settings.Save()
objReader.Close()
Dim objReader1 As New System.IO.StreamReader(Application.StartupPath & "\" & TextBox3.Text & "_OAuthtkST.dat")
My.Settings.m_strTokenSecret = objReader1.ReadLine()
My.Settings.Save()
objReader1.Close()
Me.Hide()
Form1.Show()
Catch ex As Exception
Me.Hide()
Form3.Show()
End Try
I used that code in my application called TRocket to check if the files are present or not :P *The lazy way to check/read.
|
|
|
|
|
You call it lazy I call it simple.
You could check for the file existence
if (File.Exist(...)) {...}
But what if the file exist but is not readable?
I think your code is fine.
I also often hear you should not catch all.
In some other languages, the compiler know what exception is raised by a block of code. Catching the required exception in that scenario is easy.
In C#, it is harder. If you catch the exceptions you can guess, you might miss the rare exceptions like disk disconnected, out of memory or god knows.
I am not a programmer that put program elegance or programmer state of mind before the end-user satisfaction.
I think catching all is safer.
In one word, I think your code if FINE.
modified 8-Jun-12 18:19pm.
|
|
|
|